基础功能
1.获取MapView控件
map = (MapView) findViewById(R.id.mapView);
2.获取BaiduMap对象
mBaiduMap = map.getMap();
3.根据经纬度定位
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(经纬度));
4.设置地图放大层级
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(18f).build()));
5.画点
mBaiduMap.addOverlay(new MarkerOptions().position(经纬度).icon(BitmapDescriptorFactory.fromResource(图标)));
6.画线
mBaiduMap.addOverlay(new PolylineOptions().width(10).points(坐标组).dottedLine(true).customTextureList(线条组).textureIndex(线条index组));
画点详细介绍
百度地图提供的画点有两种,一种是单个单个的画,一种是一组的去画,一组的去画我试了给设置点击事件没用我详细介绍单点
获取画的这个点并赋值
MarkerOptions options=new MarkerOptions().position(经纬度).icon(BitmapDescriptorFactory.fromResource(图标));
Marker m = (Marker) mBaiduMap.addOverlay(options);
Bundle bundle1 = new Bundle();
bundle1.putInt("index", index);
m.setExtraInfo(bundle1);
//二次设置
marker1.setPosition(位置);
marker1.setIcon(BitmapDescriptorFactory.fromResource(图标));
点击事件,我这里是弹出了一个框,点击事件点一下会执行两次,这个解决方案自行百度
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
Log.d("TAG", "执行了监听图标方法");
GeoCoder geoCoder = GeoCoder.newInstance();
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.layout_map_details, null);
TextView txtDate = view.findViewById(R.id.txt_map_details_date);
TextView txtTime = view.findViewById(R.id.txt_map_details_time);
TextView txtAddress = view.findViewById(R.id.txt_map_details_address);
txtDate.setText(runLocal.get(finalIndexMarker).getUpdatetime() + " " + localType(runLocal.get(finalIndexMarker).getSource()));
if (timeMin>0){
txtTime.setText(time);
}else{
txtTime.setText("");
}
txtAddress.setText(arg0.getAddress());
//创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
InfoWindow mInfoWindow = new InfoWindow(view, getLatLng(runLocal.get(finalIndexMarker)), -50);
//显示InfoWindow
mBaiduMap.showInfoWindow(mInfoWindow);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBaiduMap.hideInfoWindow();
}
});
}
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
}
});
//设置反地理编码位置坐标
ReverseGeoCodeOption op = new ReverseGeoCodeOption();
op.location(getLatLng(runLocal.get(indexMarker)));
//发起反地理编码请求(经纬度->地址信息)
geoCoder.reverseGeoCode(op);
return false;
}
});
}
画线详细介绍
cav_polyline = (Polyline) mBaiduMap.addOverlay(new PolylineOptions()
.width(10) //宽度
.points()//位置组
.dottedLine(true)
.customTextureList(runBitmap)//线条组
.textureIndex(list_cav_int));//线条下标组
//二次设置
cav_polyline.setPoints(list_cav);//设置位置组等
判断经纬度是否在View上
Point pt = mBaiduMap.getMapStatus().targetScreen;
Point point = mBaiduMap.getProjection().toScreenLocation(经纬度);
if (point.x < 0 || point.x > pt.x * 2 || point.y < 0 || point.y > pt.y * 2) {
Log.d("TAG","超出屏幕");
}
注意事项
1.画线的那个List集合存储图片的不要存相同的进去,后面那个下标组,假如图片组存了3种图片进去,那下表组子值就是0或者1或者2,长度为位置组-1
2.画点的时候,弹框出来,假如你再次定位到其他位置,弹框的位置在View上不可见的时候就会引发崩溃
网友评论