您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Android怎么調用高德地圖定位”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Android怎么調用高德地圖定位”這篇文章吧。
在App中使用地圖定位十分常見,購物功能的可以直接定位當前位置,發動態功能可以定位當前位置發出,社交功能可以定位周邊用戶等等。這里我使用高德地圖定位當前位置并顯示地址和經緯度。
先上效果圖:
實現步驟:
1. 創建應用獲取key:
接入第三方無一例外,去高德地圖官網注冊賬號并創建應用。填入包名和keystore的Sha1。
創建應用
獲取Sha1,輸入命令keytool -v -list -keystore keystore文件路徑,我在這里通過控制臺獲取調試版的:
進入.android目錄
cd .android
輸入命令獲取
keytool -v -list -keystore debug.keystore
過程截圖:
創建完成獲取Key:
2. 接入SDK
添加定位庫
compile 'com.amap.api:location:latest.integration'
添加2D地圖庫
compile 'com.amap.api:map2d:latest.integration'
添加地圖權限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
在<application></application>中設置地圖key
<meta-data android:name="com.amap.api.v2.apikey" android:value="57b811b21061501ee320e171568f0dac" />
3. 代碼實現(帶每一步功能注釋):
實現思路:創建MapView顯示地圖;設置定位定位監聽,設置定位參數,在activate激活后啟動定位;在定位回調中獲取經緯度,在將地圖移動到定位位置,在該經緯度上添加覆蓋圖標。詳細代碼如下:
地圖控件:
<com.amap.api.maps2d.MapView android:id="@+id/mapview" android:layout_width="match_parent" android:layout_height="match_parent"/>
定位類AmapActivity完整代碼:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.amap.api.location.AMapLocation; import com.amap.api.maps2d.AMap; import com.amap.api.maps2d.CameraUpdateFactory; import com.amap.api.maps2d.LocationSource; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.model.LatLng; public class AmapActivity extends AppCompatActivity implements LocationSource { private MapView mapView; private AMap aMap; private LocationSource.OnLocationChangedListener mListener = null;//定位監聽器 private LocationUtil locationUtil; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_amap); mapView = (MapView) findViewById(R.id.mapview); mapView.onCreate(savedInstanceState); init(); } private void init() { if(aMap == null){ aMap = mapView.getMap(); } setLocationCallBack(); //設置定位監聽 aMap.setLocationSource(this); //設置縮放級別 aMap.moveCamera(CameraUpdateFactory.zoomTo(15)); //顯示定位層并可觸發,默認false aMap.setMyLocationEnabled(true); } private void setLocationCallBack(){ locationUtil = new LocationUtil(); locationUtil.setLocationCallBack(new LocationUtil.ILocationCallBack() { @Override public void callBack(String str,double lat,double lgt,AMapLocation aMapLocation) { //根據獲取的經緯度,將地圖移動到定位位置 aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat,lgt))); mListener.onLocationChanged(aMapLocation); //添加定位圖標 aMap.addMarker(locationUtil.getMarkerOption(str,lat,lgt)); } }); } //定位激活回調 @Override public void activate(OnLocationChangedListener onLocationChangedListener) { mListener = onLocationChangedListener; locationUtil.startLocate(getApplicationContext()); } @Override public void deactivate() { mListener = null; } @Override protected void onPause() { super.onPause(); //暫停地圖的繪制 mapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); //銷毀地圖 mapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //重新繪制加載地圖 mapView.onResume(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } }
定位參數設置和定位回調LocationUtil類完整代碼:
import android.content.Context; import android.util.Log; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.MarkerOptions; /** * Created by libo on 2018/2/2. */ public class LocationUtil implements AMapLocationListener { private AMapLocationClient aMapLocationClient; private AMapLocationClientOption clientOption; private ILocationCallBack callBack; public void startLocate(Context context){ aMapLocationClient = new AMapLocationClient(context); //設置監聽回調 aMapLocationClient.setLocationListener(this); //初始化定位參數 clientOption = new AMapLocationClientOption(); clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); clientOption.setNeedAddress(true); clientOption.setOnceLocation(false); //設置是否強制刷新WIFI,默認為強制刷新 clientOption.setWifiActiveScan(true); //設置是否允許模擬位置,默認為false,不允許模擬位置 clientOption.setMockEnable(false); //設置定位間隔 clientOption.setInterval(2000); aMapLocationClient.setLocationOption(clientOption); aMapLocationClient.startLocation(); } //完成定位回調 @Override public void onLocationChanged(AMapLocation aMapLocation) { if(aMapLocation != null){ if(aMapLocation.getErrorCode() == 0){ //定位成功完成回調 String country = aMapLocation.getCountry(); String province = aMapLocation.getProvince(); String city = aMapLocation.getCity(); String district = aMapLocation.getDistrict(); String street = aMapLocation.getStreet(); double lat = aMapLocation.getLatitude(); double lgt = aMapLocation.getLongitude(); callBack.callBack(country + province + city + district + street,lat,lgt,aMapLocation); }else{ //顯示錯誤信息ErrCode是錯誤碼,errInfo是錯誤信息,詳見錯誤碼表。 Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo()); } } } /** * 自定義圖標 * @return */ public MarkerOptions getMarkerOption(String str,double lat,double lgt){ MarkerOptions markerOptions = new MarkerOptions(); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.position)); markerOptions.position(new LatLng(lat,lgt)); markerOptions.title(str); markerOptions.snippet("緯度:" + lat + " 經度:" + lgt); markerOptions.period(100); return markerOptions; } public interface ILocationCallBack{ void callBack(String str,double lat,double lgt,AMapLocation aMapLocation); } public void setLocationCallBack(ILocationCallBack callBack){ this.callBack = callBack; } }
錯誤情況檢查:
Key是否配置錯誤
在onCreate中調用mapView.onCreate(savedInstanceState);
mapView.onPause()、mapView.onDestroy()、mapView.onResume()、mapView.onSaveInstanceState(outState)是否都已調用且不寫錯
aMap.setMyLocationEnabled(true)設置顯示定位層并可觸發
手機是否開啟定位
以上是“Android怎么調用高德地圖定位”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。