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!

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top