如何创建一个覆盖 MKMapView 的当前区域的 Square Overlay。我的 MKMapView 设置了用户当前位置的坐标,但它们是中心坐标。如何计算正方形坐标,以便创建一个完全适合当前 View 的正方形叠加层?
多谢你们!
最佳答案
您可以使用 centerCoordinate
和 region
MKMapView
的属性然后创建一个 MKPolygon
通过提取四个角进行叠加,如下面的代码所示:
vertex[0]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude+map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude-map.region.span.longitudeDelta/2.);
vertex[1]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude+map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude+map.region.span.longitudeDelta/2.);
vertex[2]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude-map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude+map.region.span.longitudeDelta/2.);
vertex[3]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude-map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude-map.region.span.longitudeDelta/2.);
MKPolygon *square = [MKPolygon polygonWithCoordinates:vertex count:4];
然后将多边形添加为叠加层:
[map addOverlay:square]
最后在你的
mapView:rendererForOverlay:
您定义基于多边形叠加呈现的正方形:- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {
if([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:(MKPolygon *)overlay];
renderer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.25];
return renderer;
} else {
return nil;
}
}
关于ios - 在 MKMapView 中创建覆盖当前区域的方形叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23449708/