【问题标题】:iOS SDK: MapKit MKPolyLine not showingiOS SDK:MapKit MKPolyLine 未显示
【发布时间】:2013-05-30 14:04:57
【问题描述】:

我试图在我的地图上显示一条折线,但这条线没有显示出来。我尝试了很多东西,但注意到似乎有效。

我检查了 Core Data 函数,它正在返回数据,所以这不是问题。它必须是我在地图点创建或地图上的某个地方(我猜)。 我确定它一定是某个地方的一个小错误,但我找不到它。

我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    mapView.delegate = self;
}

- (void)createLine
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Logs" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];

    NSError *error;
    NSArray *logs = [context executeFetchRequest:request error:&error];

    int logsCount = [logs count];
    MKMapPoint points[logsCount];

    // loop logs
    for (int i = 0; i < logsCount; i++)
    {
        MKMapPoint point;
        point = MKMapPointMake([[[logs objectAtIndex:i] valueForKey:@"lat"] doubleValue], [[[logs objectAtIndex:i] valueForKey:@"lng"] doubleValue]);

        points[i] = point;
    }

    MKPolyline *routeLine = [MKPolyline polylineWithPoints:points count:logsCount];
    [mapView addOverlay:routeLine];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView *mapOverlayView = [[MKOverlayView alloc] initWithOverlay:overlay];
    return mapOverlayView;
}

【问题讨论】:

  • 你的mapView:viewForOverlay: 方法是什么样的?
  • 另请注意,代码将纬度/经度分配给 MKMapPoint。 MKMapPoint 不是纬度/经度。使用 CLLocationCoordinate2D 代替 MKMapPoint,使用 CLLocationCoordinate2DMake 代替 MKMapPointMake,使用 polylineWithCoordinates 代替 polylineWithPoints。
  • 我将其更改为 MKMapPoint,因为我认为这是问题所在。我将它设置回 CLLocationCoordinate2D。我还添加了我的 mapView:viewForOverlay。

标签: ios objective-c ios6 mapkit


【解决方案1】:

显示的代码有两个问题:

  1. 正在使用MKMapPoints 创建折线,而这些MKMapPoints 被错误地设置为纬度/经度值。 MKMapPoint 不是纬度/经度。它是平面地图投影上纬度/经度的 x/y 变换。使用MKMapPointForCoordinate 将纬度/经度值转换MKMapPoints,或者只使用CLLocationCoordinate2D。当你有纬度/经度时,只使用CLLocationCoordinate2D 更容易编码和理解。
  2. viewForOverlay 中,代码正在创建一个不可见的空MKOverlayView。而是创建一个MKPolylineViewMKOverlayView 的子类,绘制MKPolylines)并设置它的strokeColor


对于第一个问题,使用:

  • CLLocationCoordinate2D 而不是 MKMapPoint
  • CLLocationCoordinate2DMake 而不是 MKMapPointMake
  • polylineWithCoordinates 而不是polylineWithPoints


对于第二个问题,这里有一个例子:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineView *mapOverlayView = [[MKPolylineView alloc] initWithPolyline:overlay];
        //add autorelease if not using ARC
        mapOverlayView.strokeColor = [UIColor redColor];
        mapOverlayView.lineWidth = 2;
        return mapOverlayView;
    }

    return nil;
}


其他几件事:

  • 我会使用 objectForKey:@"lat"@"lng" 相同),而不是 valueForKey:@"lat"
  • 确保地图视图的 delegate 已设置,否则即使进行所有其他更改,viewForOverlay 委托方法也不会被调用。

【讨论】:

  • 感谢您的广泛回答!它现在可以工作了 :-) 注意:objectForKey:@"" 不工作(得到一个错误,说对象上没有这样的选择器),所以我使用 valueForKey:@""。为什么要使用 objectForKey 而不是 valueForKey?
  • 我以为你在 NSDictionary 上调用它。见stackoverflow.com/questions/1062183/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多