昨天和今天,都在搞Android平台下利用Google maps API实现Google maps。自己搞了一下,中间遇到不少问题:总结记录一下。
(1)要用Google maps的API,就要根据自己的MD5码在Google的官网上申请API key;申请的过程也是从网上搜的;
(2)在下载程序.apk文件的时候总是出现install failed missing shared library的字样。提示缺少库。后来我网上搜了一下,原来是自己并没有下载Google api,原来下载的是Android SDK, 但是现在既然要用到Google api,就必须下载相应的Google api。下载安装完毕后,新建一个Android工程的时候,就会提示选择Target Name:这个时候,就要选择Google APIs而不是Android 2.1(update)。
Manifest文件如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test" android:versionCode="1" android:versionName="1.0"> <!--uses-permission android:name="android.permission.INTERNET" /--> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloGoogleMaps" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
main.xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:enabled="true" android:apiKey="0Xdoh_J_s5_qlAatoG2BIBVXaV8kg9eUQdMJEYQ" /> </RelativeLayout>
工程源文件
package com.test; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import com.google.android.maps.*; public class HelloGoogleMaps extends MapActivity { private MapView map=null; private MapController mapCon; private final int ITEM0 = 0; private final int ITEM1 = 1; private final int ITEM2 = 2; /** Called when the activity is first created. */ @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findviews(); } private void findviews(){ map=(MapView)findViewById(R.id.mapview); map.setBuiltInZoomControls(true); // 设置显示模式 map.setTraffic(true); map.setSatellite(false); map.setStreetView(true); // 设置可以缩放 map.setBuiltInZoomControls(true); // 设置初始地图的中心位置 // GeoPoint geoBeijing=new GeoPoint((int)(39.95*1000000), (int)(116.37*1000000)); GeoPoint geoHz = new GeoPoint((int)(30.30*1000000), (int)(120.20*1000000)); mapCon = map.getController(); mapCon.setZoom(10); mapCon.setCenter(geoHz); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0,ITEM0,0,"卫星"); menu.add(0,ITEM1,1,"交通"); menu.add(0,ITEM2,2,"街景"); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case ITEM0: toggleSatellite(); break; case ITEM1: toggleTraffic(); break; case ITEM2: toggleStreetView(); break; } return super.onOptionsItemSelected(item); } public void toggleSatellite() { map.setSatellite(true); map.setStreetView(false); map.setTraffic(false); } public void toggleTraffic() { map.setTraffic(true); map.setSatellite(false); map.setStreetView(false); } public void toggleStreetView() { map.setStreetView(true); map.setSatellite(false); map.setTraffic(false); } }
运行结果