jumpr3
jumpr3

Reputation: 125

Adding annotations from a KML file to an MKMapView

I am trying to load data from a KML file over to an MKMapView. I was able to parse the data into an array and now I am trying to create annotations on the map for each item.

Using the code below, I was able to create annotations on the map but the location is not correct:

Parser *parser = [[Parser alloc] initWithContentsOfURL:url];
parser.rowName = @"Placemark";
parser.elementNames = @[@"name", @"address", @"coordinates", @"description"];
[parser parse];

//parseItem is an array returned with all data after items are parsed.
for (NSDictionary *locationDetails in parser.parseItems) {
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.title = locationDetails[@"name"];
    NSArray *coordinates = [locationDetails[@"coordinates"] componentsSeparatedByString:@","];
    annotation.coordinate = CLLocationCoordinate2DMake([coordinates[0] floatValue], [coordinates[1] floatValue]);

    [self.mapView addAnnotation:annotation];

}

Upvotes: 1

Views: 174

Answers (1)

user467105
user467105

Reputation:

The result of the NSLog of the coordinates was:

coords=-73.96300100000001,40.682846,0

So it looks like the coordinates are coming in longitude,latitude order but the CLLocationCoordinate2DMake function takes latitude,longitude.

Unless the coordinates are supposed to be in Antarctica instead of New York City, try:

annotation.coordinate = CLLocationCoordinate2DMake(
                           [coordinates[1] doubleValue], 
                           [coordinates[0] doubleValue]);


Also note you should change floatValue to doubleValue for more accurate placement (it will also match the type of CLLocationDegrees which is a synonym for double).

Upvotes: 1

Related Questions