今天按官方教程做了一個(gè)定位+地圖的例子了解一下API。拍照的例子沒(méi)找到官方的例子,瞎搜了一圈例子沒(méi)做完,明天再整理。
定位、地圖相關(guān)的配置:
AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name"><uses-library android:name="com.google.android.maps" /><activity android:name=".LocationMapActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
主要是聲明 需要googlemap的lib和互聯(lián)網(wǎng)和定位的訪(fǎng)問(wèn)權限。
布局中需要聲明MapView
<com.google.android.maps.MapViewandroid:id="@+id/mapview" android:layout_width="fill_parent"android:layout_height="fill_parent" android:clickable="true"android:apiKey="0MhdDulsEpJMaz65SIdleweZoylEaKL11yQc_nQ" />
關(guān)鍵的地方在apiKey,網(wǎng)上一堆因為這個(gè)訪(fǎng)問(wèn)不到地圖,問(wèn)問(wèn)題的。 apiKey是與應用的簽名證書(shū)關(guān)聯(lián)的,使用google地圖需要在,在http://code.google.com/intl/zh-CN/android/maps-api-signup.html 注冊。完整說(shuō)明見(jiàn)http://code.google.com/intl/zh-CN/android/add-ons/google-apis/mapkey.html
這里只是獲得一個(gè)與debug密鑰匹配的簽名。
1 先從debug的密鑰庫取出密鑰
密鑰庫一般在 C:\Documents and Settings\<user>\.android\debug.keystore
執行命令輸入簽名
keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android
結果類(lèi)似
Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98
2 在http://code.google.com/intl/zh-CN/android/maps-api-signup.html進(jìn)行注冊
獲取map api key
主要的代碼:
定位主要的API 是位置服務(wù)的訪(fǎng)問(wèn)入口 LocationManager 和 位置服務(wù)時(shí)間的偵聽(tīng)器LocationListener 。
通過(guò)LocationManager訪(fǎng)問(wèn)服務(wù):
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,10, myLocationListener);locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10,myLocationListener);
位置服務(wù)有多個(gè)Provider,一般是GPS 和 基站定位的,沒(méi)搞清電信服務(wù)商沒(méi)開(kāi)放接口的話(huà),如何通過(guò)API如何能訪(fǎng)問(wèn)得到基站位置數據。。。
通過(guò)LocationListener 接受事件和位置信息
public final LocationListener myLocationListener = new LocationListener() {//位置變化事件@Overridepublic void onLocationChanged(Location location) {GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1000000), (int) (location.getLongitude() * 1000000));OverlayItem overlayitem2 = new OverlayItem(point, "title","snippet2");itemizedOverlay.addOverlay(overlayitem2);mapView.getController().animateTo(point);Log.i(this.getClass().getName(), "---location:"+ location.toString());}//用戶(hù)關(guān)閉位置服務(wù)Provider@Overridepublic void onProviderDisabled(String provider) {Log.i(this.getClass().getName(), "---onProviderDisabled:"+ provider.toString());}//用戶(hù)啟用位置服務(wù)Provider可用,例如連到GPS衛星信號@Overridepublic void onProviderEnabled(String provider) {Log.i(this.getClass().getName(), "---onProviderEnabled:"+ provider.toString());}//位置服務(wù)Provider狀態(tài)變化,例如連到GPS衛星信號@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {Log.i(this.getClass().getName(), "---onStatusChanged:" + status);}};
地圖代碼:
聯(lián)系客服