点标记适配屏幕
功能场景
在地图区域需要展示多个点标记,常用于检索结果的展示。
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/点标记适配屏幕@android.mp4
include(List<LatLng> latLngs)
newLatLngBounds(LatLngBounds bounds, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom)
设置显示在指定相对于MapView的padding中的地图地理范围
setMapStatus(MapStatusUpdate update)
setViewPadding(int left, int top, int right, int bottom)
设置地图上控件与地图边界的距离,包含比例尺、缩放控件、logo、指南针的位置
/**
* 最佳视野内显示所有点标记
*/
private void setBounds(ArrayList<LatLng> mLatLngs , int paddingBottom ) {
int padding = 80;
// 构造地理范围对象
LatLngBounds.Builder builder = new LatLngBounds.Builder();
// 让该地理范围包含一组地理位置坐标
builder.include(mLatLngs);
// 设置显示在指定相对于MapView的padding中的地图地理范围
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(builder.build(), padding, padding,
padding, paddingBottom);
// 更新地图
mBaiduMap.setMapStatus(mapStatusUpdate);
// 设置地图上控件与地图边界的距离,包含比例尺、缩放控件、logo、指南针的位置
mBaiduMap.setViewPadding(0,0,0,paddingBottom);
}
复制成功
//mapopen-website-wiki.bj.bcebos.com/demos/iosVideos/点标记适配屏幕@2xios.mp4
CLLocationCoordinate2D coordinate;
- (id)initWithAnnotation:(id <BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
初始化并返回一个BMKPinAnnotationView实例
- (void)addAnnotation:(id <BMKAnnotation>)annotation;
BMKAnnotationViewFitMapViewController
- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated;
将所有AnnotationView适配到最佳地图视野范围
- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated {
if (!annotations || annotations.count == 0) {
return;
}
//只有一个则直接设置地图中心为annotation的位置
if (annotations.count == 1) {
id<BMKAnnotation> annotation = [annotations firstObject];
[_mapView setCenterCoordinate:annotation.coordinate animated:animated];
return;
}
double left = DBL_MAX;
double right = DBL_MIN;
double top = DBL_MAX;
double bottom = DBL_MIN;
for (id <BMKAnnotation> annotation in annotations) {
//如果是固定在屏幕上的,则不进行计算,会影响展示效果。
if ([annotation isKindOfClass:[BMKPointAnnotation class]] && ((BMKPointAnnotation *)annotation).isLockedToScreen) {
continue;
}
BMKMapPoint point = BMKMapPointForCoordinate([annotation coordinate]);
left = fmin(left, point.x);
right = fmax(right, point.x);
top = fmin(top, point.y);
bottom = fmax(bottom, point.y);
}
double x = left;
double y = top;
double width = right - left;
double height = bottom - top;
if (width > 0 && height > 0) {
BMKMapRect newRect = BMKMapRectMake(x, y, width, height);
if (newRect.size.width == 0) {
newRect.size.width = 1;
}
if (newRect.size.height == 0) {
newRect.size.height = 1;
}
//为了更好的展示效果且不影响地图其他控件的位置在这里+50
UIEdgeInsets padding = UIEdgeInsetsMake(self.mapView.mapPadding.top + 50, self.mapView.mapPadding.left + 50, self.mapView.mapPadding.bottom + 50, self.mapView.mapPadding.right + 50);
[self.mapView fitVisibleMapRect:newRect edgePadding:padding withAnimated:YES];
}
}
复制成功