Google Maps Android API v2 亲测例子

朋友做项目时遇到一个 Google Maps Android API v2的问题找我探讨,才发现Map API 已经升级到v2了。原来的v1的可以似乎已经不能再注册的,但是对于v1还是继续支持。详情请进v1介绍

关于v2:v2

有博客说v2不能在AVD运行,对与厌烦真机测试的懒人来说,这个是不可以接受了,而且网上已经有了解决方法。但是一开始本人搞了好久都没有成功的在AVD显示出地图。主要参考一下几个网页。http://blog.csdn.net/xuyue995277/article/details/8528844

http://blog.csdn.net/ilittleone/article/details/8307980 http://mobile.51cto.com/aprogram-376598.htm

觉得是因为AVD中少了一些 google的 app。试很多方法终于在模拟器上跑出图片。略微兴奋,所以记下来,以备以后查看。同时参考了
http://www.vogella.com/articles/AndroidGoogleMaps/article.html的文章完成测试。

本人使用android2.3.3 AVD也是2.3.3。具体代码如下:

ToMapActivitiy 实现显示地图

public class ToMapActivity extends FragmentActivity {
	static final LatLng HAMBURG = new LatLng(53.558, 9.927);
	static final LatLng KIEL = new LatLng(53.551, 9.993);
	private GoogleMap map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_layout);
		Log.v("test", "map     onCreate");

		map = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();
		Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
				.title("Hamburg"));
		Marker kiel = map.addMarker(new MarkerOptions()
				.position(KIEL)
				.title("Kiel")
				.snippet("Kiel is cool")
				.icon(BitmapDescriptorFactory
						.fromResource(R.drawable.ic_launcher)));

		// Move the camera instantly to hamburg with a zoom of 15.
		map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

		// Zoom in, animating the camera.
		map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

	}

}

main_layout.xml文件如下
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

这里需要说明下 因为MapFragment只在API 12及之后的版本才有,所以对于之前的版本需要使用Support Library来进行辅助。由于使用了api10 所以 Activity 要继承 FragmentActivity。同时xml文件中也是class="com.google.android.gms.maps.SupportMapFragment。


接下来是 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="包名"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

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

    <permission
        android:name=申请key时候的包名.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" >
    </permission>

    <uses-permission android:name="申请key时候的包名.permission.MAPS_RECEIVE" />
    <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" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ToMapActivity"
            android:label="@string/app_name" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="您自己的key" />
    </application>

</manifest>

至于如何申请key 之前的博客都已说得很详细了,我就不复制黏贴了。

在运行前还有两个重要的步骤。

1、Properties-->Android-->Library 中添加google-play-services-lib,同样跟前面提到的博客所说一样(先import 到 workspace)。

2、在AVD中安装GooglePlay GoogleMap。我是在真机上测试成功后,知道这个google play 适合。 所以我把本人手机中/data/app 的所有google的apk都copy出来,装在AVD上面。我在手机中copy 并且安装到AVD的apk具体如下:

6,174,123   com.android.vending-1.apk
7,239,813   com.google.android.apps.maps-2.apk
2,364,762   com.google.android.gm-1.apk
3,818,104   com.google.android.gms-1.apk
265,409      com.google.android.location-2.apk

安装命令如下:

用命令行进入d:\MyDevTools\Android\android-sdk\platform-tools\  输入adb install com.android.vending-1.apk 等等。


Good luck!




评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值