Google Map V2在Android中的应用

1、安装Google Play services SDK

打开Android SDK Manager,安装和更新Extras下的Google Play services即可。

2、安装Google Play Store和Google Play services

见附件中的com.android.vending.apk和com.google.android.gms.apk。

3、获取API Key

请参考:Google Map Android API V2中API Key的生成

4、修改AndroidManifest.xml

1)在<application>元素中加入子标签:

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

将其中的android:value换成自己申请的API Key。

2)加入许可信息

<permission
        android:name="com.example.demo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.demo.permission.MAPS_RECEIVE" />

将其中的com.example.demo换成自己的包名。

3)加入其它许可设置

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

4)加入OpenGL ES V2特性的支持

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

5、加载Google Play services类库

在Eclipse里选择:File > Import > Android > Existing Android Code Into Workspace,然后点击Next,找到路径下的/Users/jsntghf/Projects/adt-bundle-mac-x86_64-20130219/sdk/extras/google/google_play_services/libproject/google-play-services_lib,最后点击Finish。

6、添加对Google Play services类库的引用

在项目上右键,选择Properties,左边选择Android,然后在下面的Library里添加刚才的google-play-services_lib。

7、显示地图

1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mapview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

2)MainActivity.java

package com.example.demo;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

	private GoogleMap googleMap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		int status = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(getBaseContext());

		if (status != ConnectionResult.SUCCESS) {
			int requestCode = 10;
			Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
					requestCode);
			dialog.show();
		} else {
			SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.mapview);
			googleMap = fm.getMap();
			LatLng sfLatLng = new LatLng(-43.507227, 172.72233);
			googleMap.moveCamera(CameraUpdateFactory.newLatLng(sfLatLng));
			if (googleMap != null) {
				googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
				googleMap.getUiSettings().setCompassEnabled(true);
				googleMap.getUiSettings().setZoomControlsEnabled(true);
				googleMap.getUiSettings().setMyLocationButtonEnabled(true);
				googleMap.addMarker(new MarkerOptions()
				 .position(sfLatLng)
				 .title("New Brighton")
				 .snippet("New Brighton")
				 .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
				googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
						sfLatLng, 15));
				googleMap.animateCamera(CameraUpdateFactory.zoomIn());
				googleMap
						.animateCamera(CameraUpdateFactory.zoomTo(10), 2, null);
			}
		}
	}

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

}

效果图:


猜你喜欢

转载自eric-gao.iteye.com/blog/1882172
今日推荐