MKPolyLine没有显示

MKPolyLine没有显示

本文介绍了iOS SDK:MapKit MKPolyLine没有显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地图上显示折线,但该线条未显示。我尝试了很多东西,但注意似乎有用。

I'm trying to display a polyline on my map, but the line doesn't show up. I tried a lot of things, but noting seems to work.

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

I checked the Core Data functions, and it is returning data, so that is not the problem. It must me somewhere in the mappoint creation or the dwawing on the map (I guess).I'm sure it must be a little mistake somewhere, but I can't find it.

我的代码:

- (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;
}


推荐答案

有两个问题显示的代码:

There are two issues with the code shown:


  1. 使用 MKMapPoint 创建折线不正确到纬度/经度值。 MKMapPoint 不是纬度/经度。它是平面地图投影上纬度/经度的x / y变换。使用 MKMapPointForCoordinate 转换纬度/经度值转换为 MKMapPoint ,或者只使用 CLLocationCoordinate2D 。只要使用 CLLocationCoordinate2D ,就可以更容易地编码和理解。

  2. viewForOverlay ,代码创建一个空的 MKOverlayView ,这是不可见的。改为创建 MKPolylineView MKOverlayView 的子类绘制 MKPolyline s)并设置其 strokeColor

  1. The polyline is being created using MKMapPoints which are incorrectly set to latitude/longitude values. An MKMapPoint is not degrees of latitude/longitude. It is an x/y transformation of a lat/long on the flat map projection. Either convert the latitude/longitude values to MKMapPoints using MKMapPointForCoordinate or just use CLLocationCoordinate2D instead. Just using CLLocationCoordinate2D when you have the lat/long is much easier to code and understand.
  2. In viewForOverlay, the code is creating an empty MKOverlayView which is invisible. Create an MKPolylineView instead (a subclass of MKOverlayView that draws MKPolylines) and set its strokeColor.



对于第一期,请使用:


For the first issue, use:


  • CLLocationCoordinate2D 而不是 MKMapPoint

  • CLLocationCoordinate2DMake 而不是 MKMapPointMake

  • polylineWithCoordinates 而不是 polylineWithPoints

  • CLLocationCoordinate2D instead of MKMapPoint,
  • CLLocationCoordinate2DMake instead of MKMapPointMake,
  • and polylineWithCoordinates instead of polylineWithPoints



第二个问题,这是一个例子:


For the second issue, here's an example:

- (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;
}



其他一些事情:


A couple of other things:


  • 而不是 valueForKey:@lat,我会使用 objectForKey:@lat(同样适用于 @lng)。

  • 确保地图设置了视图的委托,否则即使进行所有其他更改,也不会调用 viewForOverlay 委托方法。

  • Instead of valueForKey:@"lat", I would use objectForKey:@"lat" (same for @"lng").
  • Make sure the map view's delegate is set otherwise the viewForOverlay delegate method will not get called even with all the other changes.

这篇关于iOS SDK:MapKit MKPolyLine没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:40