Android Google map使用
1、使用Android Google Map Api之前必須檢測系統中是否安裝了Google map 應用,檢測方法如下:
protected boolean checkGoogleMap(){lean isInstallGMap = false; List<PackageInfo>packs = getPackageManager().getInstalledPackages(0);for (int i = 0; i < packs.size(); i++) {PackageInfo p = packs.get(i);if (p.versionName == null) { // system packages continue;}if ("com.google.android.apps.maps".equals(p.packageName)) { isInstallGMap = true; break;}}return isInstallGMap;} |
2、當檢測出系統中沒有安裝Google map 應用時,可以轉向Web版的Google map 來訪問,如下:
Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q="+weiduExtra+","+jingduExtra+"")); startActivity(it); 注意:使用此方法需在AndroidManifest.xml中加入網路存取權限<uses-permission android:name="android.permission.INTERNET"></uses-permission> |
3、當檢測出系統中已經安裝Google map 應用時,我們就可以使用Google map api 了,使用方法如下:
1)方法一:
Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"+weiduExtra+","+jingduExtra));startActivity(it);注意:使用此方法需在AndroidManifest.xml中加入相應的存取權限<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> |
2)方法二:
可以建立一個MapActivity的子類,將MapView顯示於其上即可,可以用MapController來控制顯示的座標、地圖模式和視野高度,處理起來非常簡單。
Java代碼 :
public class MapTest extends MapActivity { private MapView mapView; private MapController mc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapview); mapView = (MapView) findViewById(R.id.map); mapView.setTraffic(true); mc = mapView.getController(); GeoPoint gp = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000)); //地理座標 mc.animateTo(gp); mc.setZoom(12); } @Override protected boolean isRouteDisplayed() { return false; } } public class MapTest extends MapActivity {private MapView mapView;private MapController mc;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.mapview); mapView = (MapView) findViewById(R.id.map);mapView.setTraffic(true);mc = mapView.getController();GeoPoint gp = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000)); //地理座標mc.animateTo(gp);mc.setZoom(12);} @Overrideprotected boolean isRouteDisplayed() {return false;}}mapview.xml內容如下: Xml代碼 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="0mHnPl2NS9XPKx6pKwJriV2Wj-mEHSh71yyX_SQ" /> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><com.google.android.maps.MapView android:id="@+id/map"android:layout_width="fill_parent" android:layout_height="fill_parent"android:enabled="true"android:clickable="true"android:apiKey="0mHnPl2NS9XPKx6pKwJriV2Wj-mEHSh71yyX_SQ"/></RelativeLayout> |
注意:
A、使用此方法需在AndroidManifest.xml中加入相應的存取權限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />; |
B、你要申請一個自己的apiKey。