1
如何创建覆盖MKMapView当前区域的方形覆盖图。我的MKMapView用用户当前位置的坐标设置,但它们是中心坐标。我如何计算平方坐标,以便我可以创建一个完全适合当前视图的方形覆盖图?创建覆盖MKMapView中当前区域的方形覆盖图
谢谢你们!
如何创建覆盖MKMapView当前区域的方形覆盖图。我的MKMapView用用户当前位置的坐标设置,但它们是中心坐标。我如何计算平方坐标,以便我可以创建一个完全适合当前视图的方形覆盖图?创建覆盖MKMapView中当前区域的方形覆盖图
谢谢你们!
您可以使用MKMapView
的centerCoordinate
和region
属性,然后通过提取四个角创建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;
}
}
你不知道你帮了我多少!非常感谢!对此,我真的非常感激! –