调用 [mapView addOverlay:] 没有效果

发布于 2024-12-10 13:17:47 字数 1403 浏览 0 评论 0原文

由于某种原因,当我使用 [mapView addOverlay:] 时,没有任何反应。

代码:

NSData* dataLat = [[NSUserDefaults standardUserDefaults] objectForKey:@"latKey"];
NSArray* overlayLat = [NSKeyedUnarchiver unarchiveObjectWithData:dataLat];
double lats[overlayLat.count];
NSData* dataLong = [[NSUserDefaults standardUserDefaults] objectForKey:@"longKey"];
NSArray* overlayLong = [NSKeyedUnarchiver unarchiveObjectWithData:dataLong];
double longs[overlayLong.count];

for(int iii = 0; iii < overlayLat.count; iii++)
{
    NSNumber* a = (NSNumber*)[overlayLat objectAtIndex:iii];
    lats[iii] = [a doubleValue];
}

for(int iii = 0; iii < overlayLong.count; iii++)
{
    NSNumber* a = (NSNumber*)[overlayLong objectAtIndex:iii];
    longs[iii] = [a doubleValue];
}

int size = (sizeof(lats) / sizeof(lats[0]));

NSLog(@"%d", size);

MKMapPoint points[size];

for(int iii = 0; iii < overlayLong.count; iii++)
{
    MKMapPoint point = MKMapPointMake(lats[iii], longs[iii]);
    if(lats[iii] != 0)
        points[iii] = point;
}

for(int iii = 0; iii < 15; iii++)
{
    NSLog(@"Lat (x):%f", points[iii].x);
    NSLog(@"Long (y):%f", points[iii].y);
}

MKPolyline* line = [MKPolyline polylineWithPoints:points count:size];

[mapView addOverlay:line];

NSLog(@"finished");

该代码的绝大多数只是访问数据并将其转换为可用的坐标。我的问题是(因为我知道由于 NSLogs,坐标是有效的),为什么调用 addOverlay: 时没有任何内容添加到 mapView 中?如果需要,我可以发布更多代码。谢谢!

For some reason, when I use [mapView addOverlay:], nothing happens.

Code:

NSData* dataLat = [[NSUserDefaults standardUserDefaults] objectForKey:@"latKey"];
NSArray* overlayLat = [NSKeyedUnarchiver unarchiveObjectWithData:dataLat];
double lats[overlayLat.count];
NSData* dataLong = [[NSUserDefaults standardUserDefaults] objectForKey:@"longKey"];
NSArray* overlayLong = [NSKeyedUnarchiver unarchiveObjectWithData:dataLong];
double longs[overlayLong.count];

for(int iii = 0; iii < overlayLat.count; iii++)
{
    NSNumber* a = (NSNumber*)[overlayLat objectAtIndex:iii];
    lats[iii] = [a doubleValue];
}

for(int iii = 0; iii < overlayLong.count; iii++)
{
    NSNumber* a = (NSNumber*)[overlayLong objectAtIndex:iii];
    longs[iii] = [a doubleValue];
}

int size = (sizeof(lats) / sizeof(lats[0]));

NSLog(@"%d", size);

MKMapPoint points[size];

for(int iii = 0; iii < overlayLong.count; iii++)
{
    MKMapPoint point = MKMapPointMake(lats[iii], longs[iii]);
    if(lats[iii] != 0)
        points[iii] = point;
}

for(int iii = 0; iii < 15; iii++)
{
    NSLog(@"Lat (x):%f", points[iii].x);
    NSLog(@"Long (y):%f", points[iii].y);
}

MKPolyline* line = [MKPolyline polylineWithPoints:points count:size];

[mapView addOverlay:line];

NSLog(@"finished");

The vast majority of this code just accesses the data and turns it into usable coordinates. My question is (because I know that the coordinates are valid because of the NSLogs), why doesn't anything get added to the mapView when addOverlay: is called? I can post more code if need be. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇柔作态 2024-12-17 13:17:47

polylineWithPoints:count: 方法采用 MKMapPoint 结构的 C 数组。

MKMapPointCLLocationCooperative2D(纬度、经度)不同。您正在使用 MKMapPointMake 创建 MKMapPoint 结构,但为其提供纬度和经度值。这样的 MKMapPoint 不在预期位置。

使用 MKMapPointForCooperative 从纬度/经度坐标创建 MKMapPoint,或者可能更简单,创建一个 CLLocationCooperative2D 结构的 C 数组并调用 改为 polylineWithCoordinates:count:

The polylineWithPoints:count: method takes a C array of MKMapPoint structs.

An MKMapPoint is not the same as a CLLocationCoordinate2D (latitude, longitude). You are creating MKMapPoint structs using MKMapPointMake but giving it latitude and longitude values. Such an MKMapPoint is not at the expected location.

Either use MKMapPointForCoordinate to create an MKMapPoint from lat/long coordinates or, probably easier, create a C array of CLLocationCoordinate2D structs and call polylineWithCoordinates:count: instead.

天暗了我发光 2024-12-17 13:17:47

会不会是你的“尺码”不对?您的点数组也会在源数据为 0 的任何地方缺少项目。我会使用 NSMutableArray 并将项目添加到其中(如果它们被认为有效),然后在最后对其进行计数并将其传递给 polylineWithPoints。

Could it be your 'size' is wrong? Your points array will also have missing items anywhere the source data had 0. I'd go with an NSMutableArray and add items to that if they are deemed valid, then count that at the end and pass it to polylineWithPoints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文