高德地圖自定義定位按鈕及點(diǎn)擊事件

最近使用了一下高德地圖,使用期間將出現(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.有不明白的地方可以參考高德地圖官方文檔?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容