Android开发GoogleMap入门

Android开发要连接GoogelMaps使用MapView时需要,先从Google网站申请一组经过验证的Maps API Key授权码,这个在网上很多。

[代码] [Java]代码

 
 
 
  1. package com.android.antking.map; 
  2.     import com.google.android.maps.MapActivity; 
  3.     import com.google.android.maps.GeoPoint; 
  4.     import com.google.android.maps.MapController; 
  5.     import com.google.android.maps.MapView; 
  6.     import android.os.Bundle; 
  7.     public class MyMain extends MapActivity { 
  8.         //放大倍数 
  9.        static final int  INITIAL_ZOOM_LEVEL=12
  10.        //中心点 
  11.        static final int  INITIAL_LATITUDE=123465465
  12.        static final int  INITIAL_LONGITUDR=15634646
  13.         public void onCreate(Bundle savedInstanceState) { 
  14.             super.onCreate(savedInstanceState); 
  15.             setContentView(R.layout.main); 
  16.             MapView mapView = (MapView) findViewById(R.id.mapView); 
  17.             //enable to change the size of the map 
  18.             mapView.setBuiltInZoomControls(true); 
  19.             //设置Zoom 大小和地图的中心点 
  20.             MapController mc = mapView.getController(); 
  21.             mc.setZoom(INITIAL_ZOOM_LEVEL); 
  22.             mc.setCenter(new GeoPoint(INITIAL_LATITUDE,INITIAL_LONGITUDR)); 
  23.         } 
  24.         @Override 
  25.         protected boolean isRouteDisplayed() { 
  26.             // TODO Auto-generated method stub 
  27.             return false
  28.         } 
  29.     } 

[代码] [XML]代码

 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.         android:orientation="vertical" 
  4.         android:layout_width="fill_parent" 
  5.         android:layout_height="fill_parent" 
  6.         > 
  7.     <TextView  
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="@string/hello" 
  11.         /> 
  12.      <com.google.android.maps.MapView 
  13.           android:id="@+id/mapView" 
  14.           android:layout_width="fill_parent" 
  15.           android:layout_height="fill_parent" 
  16.           android:enabled="true" 
  17.           android:clickable="true" 
  18.           android:apiKey="0FZLYf-YM4SRrJrJum55MeeaO4Gd_IitVFmtUeA"/>这个是自己开发的google mapApi 密钥 
  19.     </LinearLayout> 

[代码] 在这里加上google的地图包,还有Internet权限

 
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.     <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.           package="com.android.antking.map" 
  4.           android:versionCode="1" 
  5.           android:versionName="1.0"
  6.         <uses-SDK android:minSdkVersion="7" /> 
  7.         <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
  8.         <application android:icon="@drawable/icon" android:label="@string/app_name"
  9.             <activity android:name=".MyMain" 
  10.                       android:label="@string/app_name"
  11.                 <intent-filter> 
  12.                     <action android:name="android.intent.action.MAIN" /> 
  13.                     <category android:name="android.intent.category.LAUNCHER" /> 
  14.                 </intent-filter> 
  15.             </activity> 
  16.             <uses-library android:name="com.google.android.maps"></uses-library> 
  17.         </application> 
  18.     </manifest> 

1.创建工程,注意SDK旋转为"Goolge APIs”

2.修改AndroidManifest.xml文件

由于使用Google Map API,所以必须添加<uses-library android:name="com.google.android.maps" />

由于需要从网络获取地图数据,所以需要访问网络的权限<uses-permission android:name="android.permission.INTERNET"/>

可能还需要添加其他权限。

3.创建MapView

4.实现MapActivity

5.MapController的使用

6.Ovelay的使用

THE END