- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我无法让 MKPolygonRenderer 出现在我的 map 上。我有一个包含 MKMapView
的 MapViewController
类,并创建 CustomMapOverlay
实例以在 MKMapView
上呈现。
MapViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
}
// ...
// Later, I retrieve some models and generate CustomMapOverlay instances from them...
for (Model *model in models) {
CustomMapOverlay *customMapOverlay = [[CustomMapOverlay alloc] initWithModel:model];
[self.mapView addOverlay:customMapOverlay];
}
// ...
// Implement MKMapViewDelegate protocol
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKPolygonRenderer *polygonRenderer = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
polygonRenderer.lineWidth = 2;
polygonRenderer.strokeColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];
polygonRenderer.fillColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.5];
return polygonRenderer;
}
CustomMapOverlay.m:
@implementation CustomMapOverlay
// Required by MKOverlay protocol
@synthesize coordinate,
boundingMapRect;
- (instancetype)initWithModel:(Model *)model {
coordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
double radiusInPoints = MKMapPointsPerMeterAtLatitude(model.latitude) * model.radius;
boundingMapRect = MKMapRectMake(model.latitude, model.longitude, radiusInPoints, radiusInPoints);
return self;
}
@end
mapView:rendererForOverlay
被调用,在调试器中检查 overlay
我看到 map 当前屏幕边界内的 coordinate
以及看起来合理的 boundingMapRect
(虽然我不确定“ map 点”是什么,但我相信 MKMapPointsPerMeterAtLatitude
方法会按照它所说的去做) .
但是, map 上没有出现多边形。
更新:
我现在意识到我正在尝试渲染多边形而不创建它们。然后,我现在生成 MKPolygon
叠加层,而不是 CustomMapOverlay
:
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(centerCoordinate, model.radius, model.radius);
int numCoords = 4;
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numCoords);
coords[0] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
coords[1] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
coords[2] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
coords[3] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:numCoords];
free(coords);
[self.mapView addOverlay:polygon];
但是,现在根本不再调用 mapView:rendererForOverlay
。
最佳答案
在创建 MKPolygon
的更新代码中,coords
数组中的坐标是反向的。例如,这一行:
coords[0] = CLLocationCoordinate2DMake(
(region.center.longitude - 0.5*region.span.longitudeDelta),
(region.center.latitude + 0.5*region.span.latitudeDelta));
应该是:
coords[0] = CLLocationCoordinate2DMake(
(region.center.latitude + 0.5*region.span.latitudeDelta,
(region.center.longitude - 0.5*region.span.longitudeDelta));
在CLLocationCoordinate2DMake
函数中,第一个参数是latitude,然后是longitude。
因为坐标是向后的,所以它们可能完全无效或位于错误的位置。
rendererForOverlay
委托(delegate)方法只会在覆盖层的 boundingMapRect
(MKPolygon
将根据给定坐标自动定义)位于当前显示的 map 区域。但如果坐标无效或位置错误,boundingMapRect
也将无效。
CustomMapOverlay
的原始代码中,至少存在以下两个问题:
initWithModel
方法不调用 [super init]
(假设它是一个 NSObject
子类)。boundingMapRect
计算不正确。 MKMapRectMake
函数采用 MKMapPoint
值,但代码以度为单位传递纬度和经度。 MKMapPoint
与 CLLocationCoordinate2D
不同。您可以使用 MKMapPointForCoordinate
函数将 CLLocationCoordinate2D
转换为 MKMapPoint
。参见 MKMapPointForCoordinate returning invalid coordinates和 Map Coordinate Systems in the documentation了解更多信息。关于ios - MKOverlayRenderer 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103165/
import UIKit import CoreData import Foundation import MapKit struct Geoshape : Codable { let coo
我试图在运行时修改 MKPolyline 的路径以避免它与另一个重叠。 我已经设法获得所有重叠点,我想做的是在 func createPath() 中MKPolylineRenderer 的添加一个偏
我无法让 MKPolygonRenderer 出现在我的 map 上。我有一个包含 MKMapView 的 MapViewController 类,并创建 CustomMapOverlay 实例以在
我正在尝试使用 MKOverlayRenderer 在 map 上呈现文本。我有一个现有的功能性 MKOverlayRenderer 渲染一组点,所以我唯一的问题是为 '-(void)drawMapR
我想在 mapview 上绘制带孔的圆(如 donut )。我的代码在这里。 - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale
我正在尝试使用 MKOverlayRenderer 绘制一条线。我的覆盖渲染器的 drawMapRect 大致是: let path = CGPathCreateMutable() CGPathMov
我将自定义 MKOverlayRenderer 添加到 MKMapView,然后我将其删除removeOverlays() 但是覆盖在那里停留了一段时间,mkmapview 更新绘图,大多数时候我将
我想在 map 叠加层中放置一张图片,该图片会随着 map 缩放。使用下面的代码,图像出现在 map View 中,但它被拉伸(stretch)以适合 View 。如何在叠加层内保持图像的原始纵横比?
MKMapView 正在按图 block 绘制 MKOverlayRenderer。问题是并非覆盖的所有图 block 都以相同的缩放比例绘制在屏幕上。同时,叠加层后面的 map 被精确且正确地绘制出
现在,由于它们在 MKOverlayView 中不再有 View ,并且它被称为 MKOverlayRenderer,我如何才能将 UIImageView 添加到我的 MKMapView 中?注意:我
我正在开发一个高度基于 map 的应用程序,并且我正在使用 MBXMapKit 框架(基于 MapKit 构建)以便在我的 MapView 中显示自定义 Mapbox map 图 block 而不是默
我正在为 iOS 7 更新一个 iOS 6 应用程序,发现在 iOS 7 中处理覆盖的方式已经完全改变。 我们正在整个 map 上绘制浅灰色叠加层。在 iOS 6 中一切正常,在 iOS 7 中我们没
我是代码新手,我正在尝试实现类似 Reminders 应用程序的功能: 我关注了另一个 answer实现它并 这里是我的代码: 在我的 ViewController 中: var circle = M
我构建了一个自定义 MKOverlayRenderer 以构建多边形、应用混合模式,然后将它们添加到 map View 。在我的 drawMapRect 函数中,我使用 CGPoints 数组来构建多
我收到这个错误 "Nil is incompatible with return type 'MKOverlayRenderer'". 这是我的代码: func mapView(mapView: MK
在 iOS 7 中,MKOverlayView 被 MKOverlayRenderer 取代。之前,我可以添加一个 UIImageView 作为 MKOverlayView 的 subview ,并访
对于一个应用程序,我必须在 MKMapView 上放置一个 MKCircle 注释。但是,需要移动/调整圆圈的大小。我知道有人问过这个问题,例如Moving MKCircle in MKMapView
在 MKOverlayRenderer 已经传递给 Map 之后,如何访问和修改它的 alpha 属性? 事实上,我可以在 rendererForOverlay 方法中修改 alpha 属性: - (
所以当我使用 Swift 1.2 时,这个函数运行良好,并为我提供了我需要的功能。 func mapView(mapView: MKMapView, rendererForOverlay overla
我有一个 map View ,如下所示 map 中绘制的线条使用的是MKPolyline。我使用苹果 MapKit 框架来显示我的 map 。我的要求是当用户在 Polyline 上选择注释时,折线应
我是一名优秀的程序员,十分优秀!