Google Map API V2

以后如果要开发包含googlemap的程序就更麻烦咯

http://www.cnblogs.com/mengdd/archive/2013/01/01/2841390.html

找到篇不错的文章,大家可以参考一下

http://blog.sina.com.cn/s/blog_c2839d410101jahv.html

1.创建Android工程

由于v2的key需要GooglePlayService的支持,所以创建的工程需要将其引入

2.获得keystore的SHA-1的fingerprint

%JAVA_HOME%\bin\keytool-list-v-keystore.android\debug.keystore

(Release时换成相应的keystore)

3.获得GoogleMapAPIKey

打开https://code.google.com/apis/console并登录(第一次登录会提示创建APIProject)

导航栏点击Services,找到GoogleMapsAndroidAPIv2,设置成ON(设置后左侧导航栏的内容会增加)

导航栏点击APIAccess->CreatenewAndroidKey

输入SHA-1的fingerprint和工程的包名,用分号分隔

点击Create后就可以获得Key了(会获得2个Key,一个是对应Android设备的,另一个是对应Browser的)

4.修改AndroidManifest.xml

添加Permission

<permission
    android:name="<your package name>.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="<your package name>.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

添加APIKey

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

...

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="<your API Key>" />
</application>

5.在layout.xml上添加GoogleMap

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

</RelativeLayout>

6.控制GoogleMap

由于使用了Fragment,所以要继承FragmentActivity

public class MainActivity extends FragmentActivity implements
		OnMapLongClickListener, OnMyLocationChangeListener {

	private GoogleMap map;
	private MarkerOptions destinationMarker;
	private LatLng myLatLng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Fragment fragmentMap = getSupportFragmentManager().findFragmentById(
				R.id.map);
		if (fragmentMap != null)
			map = ((SupportMapFragment) fragmentMap).getMap();

		if (map != null) {
			map.setMyLocationEnabled(true);
			map.setOnMyLocationChangeListener(this);
			map.setOnMapLongClickListener(this);

			// location lat lng
			LatLng latLng = new LatLng(0, 0);
			// add mark
			destinationMarker = new MarkerOptions();
			destinationMarker.position(latLng);
			destinationMarker.title("test");
			map.addMarker(destinationMarker);
			// move camera
			map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12f));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onMyLocationChange(Location loc) {
		myLatLng = new LatLng(loc.getLatitude(), loc.getLongitude());
	}

	@Override
	public void onMapLongClick(LatLng point) {
		map.clear();
		destinationMarker = new MarkerOptions();
		destinationMarker.position(point);
		map.addMarker(destinationMarker);
	}
}

相关推荐