MKMapPointForCoordinate返回无效的坐标 [英] MKMapPointForCoordinate returning invalid coordinates

查看:377
本文介绍了MKMapPointForCoordinate返回无效的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MKMapView的注释,叠加层等,但是由于MKMapPointForCoordinate()返回无效坐标,我在解决屁股问题时感到很痛苦.

I am working with MKMapView's, annotations, overlays, etc, but I'm having a pain in the butt issue with MKMapPointForCoordinate() returning an invalid coordinate.

代码:

MKMapPoint* pointArr;
for (Category* route in validRoutes){

    NSString* routeID = [route routeid];
    NSArray* pointData = [routes objectForKey:routeID];

    pointArr = malloc(sizeof(MKMapPoint) * [pointData count]);
    int i = 0;

    for (NSDictionary* routeData in pointData) {

        NSString* latitude = [routeData objectForKey:@"latitude"];
        NSString* longitude = [routeData objectForKey:@"longitude"];
        NSLog(@"L: %@ L: %@",latitude, longitude);

        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[f numberFromString:latitude] doubleValue], [[f numberFromString:longitude] doubleValue]);


        NSLog(@"Coord: %f %f",coord.latitude,coord.longitude);

        MKMapPoint point = MKMapPointForCoordinate(coord);
        NSLog(@"Point: %f %f",point.x,point.y);
        pointArr[i] = point;
        i++;

    }

    MKPolyline *polyline = [MKPolyline polylineWithPoints:pointArr count: i];
    polyline.title = [route name];
    [routeOverlays setObject:polyline forKey: [route routeid]];
    [map addOverlay:polyline];
    free(pointArr);
}

输出示例:

L:41.380840 L:-83.641319

L: 41.380840 L: -83.641319

坐标:41.380840 -83.641319

Coord: 41.380840 -83.641319

点:71850240.204982 100266073.824832

Point: 71850240.204982 100266073.824832

我不明白为什么向MKMapPoint的转换会破坏我的CLLocationCoordinate2D的值.覆盖层未显示在地图上,因为值无效...

I don't understand why the conversion to a MKMapPoint is destroying the values of my CLLocationCoordinate2D. The overlay doesn't show up on the map because the values are invalid...

我使用MKMapPointMake代替了,但是,我的覆盖图仍然没有显示.这是mapView:viewForOverlay:代码:

I got the point working by using MKMapPointMake instead, BUT, my overlay still isn't showing. This is the mapView: viewForOverlay: code:

-(MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
MKOverlayView *overlayView = nil;

//Checks if the overlay is of type MKPolyline
if([overlay isKindOfClass:[MKPolyline class]]){
    MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:overlay];

    routeLineView.strokeColor = [UIColor orangeColor];
    routeLineView.lineWidth = 10;
    return overlayView;
}

return nil;
}

该方法被调用(使用断点进行确认),并且我有注释在起作用(因此,我认为必须正确设置委托)

The method gets called (Used a breakpoint to confirm), and I have annotations working (So the delegate has to be setup correctly, I assume)

双重:facepalm:每次在委托代码中我都返回nil.这就是我复制和粘贴先前版本代码; P

Double edit: :facepalm: I was returning nil every time in the delegate code. That's what I get for copying and pasting the previous versions code ;P

推荐答案

MKMapPoint 不是 的纬度/经度(例如CLLocationCoordinate2D).

它们不可互换,因此您不应期望MKMapPoint x,y值与相应的纬度和经度显示任何明显的关系.

They are not interchangeable and so you should not expect the MKMapPoint x,y values to show any obvious relation to the corresponding latitude and longitude.

An MKMapPoint是使用x,y值将纬度和经度转换为平面投影,x,y值与纬度和经度不在同一比例或范围内.请参阅地图坐标系部分.

An MKMapPoint is the conversion of latitude and longitude onto a flat projection using x,y values which are not in the same scale or range as latitude and longitude. Please see the Map Coordinate Systems section in the Location Awareness Programming Guide for a more detailed explanation.

顺便说一句,如果您具有CLLocationCoordinate2D值,则使用polylineWithCoordinates而不是polylineWithPoints创建折线要容易得多.这样,您就不必为任何转换而烦恼.

By the way, if you have CLLocationCoordinate2D values, it's much easier to create a polyline using polylineWithCoordinates instead of polylineWithPoints. That way, you don't need to bother with any conversion.

有关更多详细信息,请参见 iOS SDK:MapKit MKPolyLine未显示.一个例子.

See iOS SDK: MapKit MKPolyLine not showing for some more details and an example.

这篇关于MKMapPointForCoordinate返回无效的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆