亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

android 百度地圖marker添加自定義視圖

原創(chuàng) 2016-11-07 16:17:07 896
摘要:1. 添加marker下面的代碼添加了10個(gè)marker到地圖上,marker是被添加到map的,也就是mapview獲取的map對(duì)象上面。通過在marker中添加bundle對(duì)象,在點(diǎn)擊marker的時(shí)候確定數(shù)據(jù)進(jìn)行顯示或者跳轉(zhuǎn)       LatLng point;    &

1. 添加marker

下面的代碼添加了10個(gè)marker到地圖上,marker是被添加到map的,也就是mapview獲取的map對(duì)象上面。通過在marker中添加bundle對(duì)象,在點(diǎn)擊marker的時(shí)候確定數(shù)據(jù)進(jìn)行顯示或者跳轉(zhuǎn)

       LatLng point;        for (int i = 0; i < 10; i++) {
            point = new LatLng(location[0]+i * 1, location[1]+ i *1);
            BitmapDescriptor marker = BitmapDescriptorFactory.fromResource(R.mipmap.marker);
            OverlayOptions option = new MarkerOptions()
                    .position(point).icon(marker);
            Marker mker = (Marker) mMapView.getMap().addOverlay(option);
            Bundle bundle = new Bundle();
            bundle.putInt("mk", i);
            mker.setExtraInfo(bundle);
            markers.add(mker);
        }

2. marker上面添加自定義view,當(dāng)點(diǎn)擊到響應(yīng)的marker的時(shí)候就顯示一個(gè)自定義view。

在創(chuàng)建InfoWindow的時(shí)候的第三個(gè)參數(shù)表示view在marker的位置,下面代碼中寫的-60表示在marker網(wǎng)上移動(dòng)60個(gè)單位,在這里設(shè)置這個(gè)偏移能夠讓infowindow能夠隨著縮放而移動(dòng)位置,而如果不再這里設(shè)置而是在上面單獨(dú)像有些人寫的直接latlng的位置y上面減的話在縮放地圖的時(shí)候就會(huì)導(dǎo)致infowindow不動(dòng)而偏離marker(這個(gè)問題困擾好久)

// 給marker添加點(diǎn)擊事件,所有的marker都有這個(gè)點(diǎn)擊
        mMapView.getMap().setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override            public boolean onMarkerClick(Marker marker) {
                Bundle extraInfo = marker.getExtraInfo();                int mk = extraInfo.getInt("mk");                // 獲取marker的位置
                LatLng p = marker.getPosition();                // 添加infowindow
                View view = View.inflate(MainActivity.this, R.layout.view_marker, null);
                InfoWindow infoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(view), p, -60, new InfoWindow.OnInfoWindowClickListener() {
                    @Override                    public void onInfoWindowClick() {
                        mMapView.getMap().hideInfoWindow();
                    }
                });
                mMapView.getMap().showInfoWindow(infoWindow);                return true;
            }
        });

通過showInfoWindow和hideInfoWindow兩個(gè)方法來(lái)顯示和隱藏這個(gè)自定義的view

3. 遇到的問題:網(wǎng)絡(luò)連接正常,但是整個(gè)不顯示地圖,可能是AppKey導(dǎo)致的,需要去開發(fā)者中心重新去申請(qǐng)key


發(fā)佈手記

熱門詞條