调用 [mapView addOverlay:] 没有效果
由于某种原因,当我使用 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
polylineWithPoints:count:
方法采用MKMapPoint
结构的 C 数组。MKMapPoint
与CLLocationCooperative2D
(纬度、经度)不同。您正在使用MKMapPointMake
创建MKMapPoint
结构,但为其提供纬度和经度值。这样的MKMapPoint
不在预期位置。使用
MKMapPointForCooperative
从纬度/经度坐标创建MKMapPoint
,或者可能更简单,创建一个CLLocationCooperative2D
结构的 C 数组并调用改为 polylineWithCoordinates:count:
。The
polylineWithPoints:count:
method takes a C array ofMKMapPoint
structs.An
MKMapPoint
is not the same as aCLLocationCoordinate2D
(latitude, longitude). You are creatingMKMapPoint
structs usingMKMapPointMake
but giving it latitude and longitude values. Such anMKMapPoint
is not at the expected location.Either use
MKMapPointForCoordinate
to create anMKMapPoint
from lat/long coordinates or, probably easier, create a C array ofCLLocationCoordinate2D
structs and callpolylineWithCoordinates:count:
instead.会不会是你的“尺码”不对?您的点数组也会在源数据为 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.