最近使用了一下高德地圖,使用期間將出現(xiàn)的問題記錄一下。文中出現(xiàn)不對(duì)的地方,悉聽各路大神指正。2018年11月23日15:59:46
1.首先我們初始化map控制對(duì)象aMap?。
if (aMap ==null) {
aMap =mMapView.getMap();
}
2.設(shè)置setMyLocationButtonEnabled為false(true:顯示地圖默認(rèn)右上方圓形定位圖標(biāo)? false:不顯示)
aMap.getUiSettings().setMyLocationButtonEnabled(false);
3.實(shí)現(xiàn)位置變化監(jiān)聽器setOnMyLocationChangeListener
aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {?
@Override
? ? public void onMyLocationChange(Location location) {
????????????????double latitude = location.getLatitude();
? ? ? ? ? ? ? ? double longitude = location.getLongitude();
? ? ? ????????? latLng =new LatLng(latitude,longitude);
? ? ? ????????? if(followMove){
????????????????????????????aMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
? ? ? ? ????????}
????????}
});
4.實(shí)現(xiàn)setOnMapTouchListener監(jiān)聽
aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
????@Override
? ? public void onTouch(MotionEvent motionEvent) {
????????//用戶拖動(dòng)地圖后,不再跟隨移動(dòng),直到用戶點(diǎn)擊定位按鈕
? ? ? ? followMove =false;
? ? }
});
5.activity的xml中新增一個(gè)imageview,放到你喜歡的地方。用你喜歡的icon,(使用FrameLayout來實(shí)現(xiàn)覆蓋)
<ImageView
? ? android:layout_width="40dp"
? ? android:layout_height="40dp"
? ? android:src="@mipmap/ic_location"
????android:onClick="doclick"
? ? android:padding="5dp"
? ? android:layout_marginLeft="50dp"
? ? android:layout_marginTop="100dp"/>
6.在imageview點(diǎn)擊事件中使用moveCamera更新位置,完成定位
public void doclick(View view) {
????????aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
}
7.有不明白的地方可以參考高德地圖官方文檔?