Android Development - Google Maps: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 68: Zeile 68:
 
'''Hinweis:''' Fragments gehen erst ab API11 - Honeycomb
 
'''Hinweis:''' Fragments gehen erst ab API11 - Honeycomb
 
<pre>
 
<pre>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
+
android:id="@+id/root"
    android:layout_width="match_parent"
+
android:layout_width="match_parent"
    android:layout_height="match_parent"
+
android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
+
android:orientation="horizontal" >
    android:paddingLeft="@dimen/activity_horizontal_margin"
 
    android:paddingRight="@dimen/activity_horizontal_margin"
 
    android:paddingTop="@dimen/activity_vertical_margin"
 
    tools:context=".MapViewActivity" >
 
  
    <fragment android:id="@+id/map"
+
<fragment android:id="@+id/map"
 
  android:layout_width="match_parent"
 
  android:layout_width="match_parent"
 
  android:layout_height="match_parent"
 
  android:layout_height="match_parent"
 
  android:name="com.google.android.gms.maps.MapFragment"/>
 
  android:name="com.google.android.gms.maps.MapFragment"/>
 
+
</FrameLayout>
</RelativeLayout>
+
</pre>
 
=== Abwärtskompatibilität ===
 
=== Abwärtskompatibilität ===
 
Um die neue Google Maps V2 API auf älteren Geräten zu nutzen muß man 3 Dinge tun:
 
Um die neue Google Maps V2 API auf älteren Geräten zu nutzen muß man 3 Dinge tun:
Zeile 90: Zeile 86:
 
* eine FragmentActivity statt einer Activity nutzen
 
* eine FragmentActivity statt einer Activity nutzen
 
* Den SupportFragmentManager statt des FragmentManagers in der FragmentActivity nutzen:
 
* Den SupportFragmentManager statt des FragmentManagers in der FragmentActivity nutzen:
 +
<pre>
 +
activty_map_view.xml
 +
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 +
android:id="@+id/root"
 +
android:layout_width="match_parent"
 +
android:layout_height="match_parent"
 +
android:orientation="horizontal" >
 +
 +
<fragment
 +
  android:id="@+id/map"
 +
  android:layout_width="match_parent"
 +
  android:layout_height="match_parent"
 +
  class="com.google.android.gms.maps.SupportMapFragment"/>
 +
</FrameLayout>
 
</pre>
 
</pre>
 +
MapViewActivity.java
 +
<pre>
 +
package de.webmynet.android.location;
 +
 +
import com.google.android.gms.maps.GoogleMap;
 +
import com.google.android.gms.maps.SupportMapFragment;
  
<pre>
+
import android.os.Bundle;
 +
import android.support.v4.app.FragmentActivity;
 +
 
 +
 
 +
public class MapViewActivity extends FragmentActivity{
 +
 
 +
private SupportMapFragment mapFragment;
 +
private GoogleMap googleMap;
  
 +
@Override
 +
protected void onCreate(Bundle arg0) {
 +
super.onCreate(arg0);
 +
setContentView(R.layout.activity_map_view);
 +
 +
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
 +
googleMap = mapFragment.getMap();
 +
googleMap.setMyLocationEnabled(true);
 +
 +
}
 +
}
 
</pre>
 
</pre>
  

Version vom 11. Oktober 2013, 16:19 Uhr

Link auf Google Maps als Content Provider

// Possible geo uris:
// geo:lat,long
// geo:lat,long?z=zoom
// geo:0,0?q=AddressOrPlace
double latitude = 48.3895;
double longitude = 9.1869;
int zoom = 13;
String uriBegin = "geo:" + latitude + "," + longitude;
String query = latitude + "," + longitude;
String encodedQuery = Uri.encode(query);
String uriString = uriBegin + "?q=" + encodedQuery + "&z=" + zoom;
Uri uri = Uri.parse(uriString);
i = new Intent(android.content.Intent.ACTION_VIEW, uri);
showActivity(i);

MapView Activity

https://developers.google.com/maps/documentation/android/?hl=de
http://ddewaele.github.io/GoogleMapsV2WithActionBarSherlock/part1

Voraussetzungen

  • Google Play Services SDK muß installiert sein (SDK Manager)
  • In Eclipse muß folgendes Projekt importiert sein: myadtfolder/sdk/extras/google/google_play_services/libproject/google-play-services_lib damit die Packages zur Verfügung stehen.

Projekt und API Key

  • Project Properties > Android > Project Build Targets > Google APIs wählen
  • darunter bei Libaries die google-play-services_lib hinzufügen
  • Fingerprint holen
  • Registrieren auf der google developer console und API Schlüssel holen

Manifest Datei - Permissions und Key registrieren

Im Beispiel sind Permissions für diverse Zwecke rund um Kartendienste vorgesehen (siehe Kommentare)

vor application tag

<uses-library android:name="com.google.android.maps" />

Permissions (die erste nur bei Einsatz der ActionSherlock Toolbar)

<!-- Google Maps related permissions -->
<!-- <permission android:name="com.ecs.google.maps.v2.actionbarsherlock.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> -->
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
 
<!-- Network connectivity permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
 
<!-- Access Google based webservices -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
 
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

Im Application Tag wird der API-Key hinterlegt:

<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="INSERT_YOUR_API_KEY_HERE"/>

Ein MapFragment hinzufügen

Wir fügen ein Map Fragment zum Standard Layout hinzu

Hinweis: Fragments gehen erst ab API11 - Honeycomb

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/root"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="horizontal" >

<fragment android:id="@+id/map"
		  android:layout_width="match_parent"
		  android:layout_height="match_parent"
		  android:name="com.google.android.gms.maps.MapFragment"/>
</FrameLayout> 

Abwärtskompatibilität

Um die neue Google Maps V2 API auf älteren Geräten zu nutzen muß man 3 Dinge tun:

  • Im Layout ein SupportMapFragment statt ein MapFragment nutzen
  • eine FragmentActivity statt einer Activity nutzen
  • Den SupportFragmentManager statt des FragmentManagers in der FragmentActivity nutzen:
activty_map_view.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/root"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="horizontal" >

<fragment
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>
</FrameLayout> 

MapViewActivity.java

package de.webmynet.android.location;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MapViewActivity extends FragmentActivity{

	private SupportMapFragment mapFragment;
	private GoogleMap googleMap;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_map_view);
		
		mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
		googleMap = mapFragment.getMap();
		googleMap.setMyLocationEnabled(true);
		
	}
}

Activity Klasse

extends MapActivity