Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 高德地圖自動定位實作

編輯:關於 Android 程式設計

和其他地圖 SDK 一樣,都要先去官網註冊成為開發者,然後取得 Key。下面直接上程式碼。

效果圖:

核心思路是:透過系統的 LocationManager 取得定位資料,再用 CameraUpdateFactory.changeLatLng 把地圖視角移動到當前位置,最後清除舊的覆蓋物並重新加上 Marker。此外還額外做了一個 ToggleButton,用來在普通地圖與衛星地圖之間切換。

 package com.example.gaodemap; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdate; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.ToggleButton; public class MainActivity extends Activity { private MapView mMapView; private AMap aMap; private MapView mapView; private LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); init(); //GPRS提供的定位信息改變 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 8, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // 使用GPRS提供的定位信息來更新位置 updatePosition(locationManager.getLastKnownLocation(provider)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub updatePosition(location); } }); ToggleButton tb = (ToggleButton) findViewById(R.id.tb); tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ aMap.setMapType(AMap.MAP_TYPE_SATELLITE); }else{ aMap.setMapType(AMap.MAP_TYPE_NORMAL); } } }); } //初始化AMap對象 private void init(){ if(aMap == null){ aMap = mMapView.getMap(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mMapView.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mMapView.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mMapView.onResume(); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } private void updatePosition(Location location){ LatLng pos = new LatLng(location.getLatitude(), location.getLongitude()); //創建一個設置經緯度的CameraUpdate CameraUpdate cu = CameraUpdateFactory.changeLatLng(pos); //更新地圖的顯示區域 aMap.moveCamera(cu); //清除所有的Marker等覆蓋物 aMap.clear(); //創建一個MarkerOptions對象 MarkerOptions markOptions = new MarkerOptions(); markOptions.position(pos); //添加MarkerOptions(實際上是添加Marker) Marker marker = aMap.addMarker(markOptions); } } 

對應的版面配置檔如下,注意 MapView 與 ToggleButton 都放在 FrameLayout 中,方便疊放與定位:

 <framelayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps.mapview android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"> </com.amap.api.maps.mapview> <togglebutton android:id="@+id/tb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="top|right" android:textoff="普通地圖" android:texton="衛星地圖" android:checked="false" android:background="@android:color/transparent"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="horizontal"><button android:id="@+id/near" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="附近" android:layout_weight="1" android:background="@android:color/transparent"></button><button android:id="@+id/route" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="路線" android:background="@android:color/transparent" android:layout_weight="1"></button><button android:id="@+id/my" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的" android:background="@android:color/transparent" android:layout_weight="1"></button></linearlayout></togglebutton></framelayout>

記得別忘了在 onResume、onPause、onDestroy 與 onSaveInstanceState 中相應地呼叫 MapView 的生命週期方法,否則地圖可能出現黑屏或狀態錯亂。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved