Android Google map使用心得

來源:互聯網
上載者:User
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。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.