摘要:1. 添加marker下面的代码添加了10个marker到地图上,marker是被添加到map的,也就是mapview获取的map对象上面。通过在marker中添加bundle对象,在点击marker的时候确定数据进行显示或者跳转LatLngpoint;&
1. 添加marker
下面的代码添加了10个marker到地图上,marker是被添加到map的,也就是mapview获取的map对象上面。通过在marker中添加bundle对象,在点击marker的时候确定数据进行显示或者跳转LatLngpoint;for(inti=0;i
point=newLatLng(location[0]+i*1,location[1]+i*1);
BitmapDescriptormarker=BitmapDescriptorFactory.fromResource(R.mipmap.marker);
OverlayOptionsoption=newMarkerOptions()
.position(point).icon(marker);
Markermker=(Marker)mMapView.getMap().addOverlay(option);
Bundlebundle=newBundle();
bundle.putInt("mk",i);
mker.setExtraInfo(bundle);
markers.add(mker);
}
2. marker上面添加自定义view,当点击到响应的marker的时候就显示一个自定义view。
在创建InfoWindow的时候的第三个参数表示view在marker的位置,下面代码中写的-60表示在marker网上移动60个单位,在这里设置这个偏移能够让infowindow能够随着缩放而移动位置,而如果不再这里设置而是在上面单独像有些人写的直接latlng的位置y上面减的话在缩放地图的时候就会导致infowindow不动而偏离marker(这个问题困扰好久)//给marker添加点击事件,所有的marker都有这个点击
mMapView.getMap().setOnMarkerClickListener(newBaiduMap.OnMarkerClickListener(){
@OverridepublicbooleanonMarkerClick(Markermarker){
BundleextraInfo=marker.getExtraInfo();intmk=extraInfo.getInt("mk");//获取marker的位置
LatLngp=marker.getPosition();//添加infowindow
Viewview=View.inflate(MainActivity.this,R.layout.view_marker,null);
InfoWindowinfoWindow=newInfoWindow(BitmapDescriptorFactory.fromView(view),p,-60,newInfoWindow.OnInfoWindowClickListener(){
@OverridepublicvoidonInfoWindowClick(){
mMapView.getMap().hideInfoWindow();
}
});
mMapView.getMap().showInfoWindow(infoWindow);returntrue;
}
});
通过showInfoWindow和hideInfoWindow两个方法来显示和隐藏这个自定义的view
3. 遇到的问题:网络连接正常,但是整个不显示地图,可能是AppKey导致的,需要去开发者中心重新去申请key