Question

I am trying to add pins from the server and show it one the map.

Normally I can show the pin but I want to add it online. I have three parsed json data NAME, Longitude and Latitude. I have parsed it in array. I couldn't know how to view it on the map

CLLocationCoordinate2D annotationCoord;

annotationCoord.latitude = 40.0000;
annotationCoord.longitude = 29.000;

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = name;

[self.locationMap addAnnotation:annotationPoint]; 

I have tried to add annotationCoord.latitude and annotationCoord.longitude in for loop but I get this error "bad receiver type 'CLLocationDegrees' (akadouble)" I think I am making big mistake but where I couldn't know. Please need help.

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

for (int i = 0; i < jsonArray.count; i++) {

    NSString *lat = [jsonArray objectAtIndex:i];

    [annotationCoord.latitude addObject:lat];

}

}

My JSON return:

 response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/mapLocation.php"]];

 NSError *parseError = nil;

    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];

    jsonArray1 = [[NSMutableArray alloc] init];
    jsonArray2 = [[NSMutableArray alloc] init];
    jsonArray3 = [[NSMutableArray alloc] init];

    for(int i=0;i<[jsonArray count];i++)
    {
        name = [[jsonArray objectAtIndex:i] objectForKey:@"name"];

        [jsonArray1 addObject:name];            
    }

    for(int i=0;i<[jsonArray count];i++)
    {
        longitude = [[jsonArray objectAtIndex:i] objectForKey:@"longitude"];

        [jsonArray2 addObject:longitude];            
    }

    for(int i=0;i<[jsonArray count];i++)
    {
        latitude = [[jsonArray objectAtIndex:i] objectForKey:@"latitude"];

        [jsonArray3 addObject:latitude];            
    }

    self.locationMap.delegate = self;
Was it helpful?

Solution

Based on the updated code, jsonArray already is an array of dictionaries with each dictionary holding the properties of each annotation.

I don't understand why you want to split that up into three separate arrays (one for each property).

Why not use jsonArray as-is to create the annotations:

jsonArray = [NSJSONSerialization JSONObjectWithData:...

self.locationMap.delegate = self;  //set delegate before adding annotations

for (int i=0; i < [jsonArray count]; i++)
{
    NSDictionary *annotationDictionary = [jsonArray objectAtIndex:i];

    name = [annotationDictionary objectForKey:@"name"];

    annotationCoord.latitude 
      = [[annotationDictionary objectForKey:@"latitude"] doubleValue];
    annotationCoord.longitude 
      = [[annotationDictionary objectForKey:@"longitude"] doubleValue];

    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationPoint.coordinate = annotationCoord;
    annotationPoint.title = name;

    [self.locationMap addAnnotation:annotationPoint]; 
}

The updated code in the question also shows it looping through jsonArray in the didUpdateUserLocation delegate method. It's not clear why this is being done in that delegate method but if you're planning to update the annotations on the map every time the user moves, you may also need to remove all/some existing annotations before adding again to avoid duplicate annotations.

OTHER TIPS

Try

- (void)addAnnotations:(NSArray *)annotations;

of MKMApView

The explanation given in top answer. I only convert this code into Swift 3.

Swift 3

jsonArray = JSONSerialization.jsonObjectWithData()
locationMap.delegate = self

for i in 0..<jsonArray.count() {
var annotationDictionary = jsonArray[i]
name = annotationDictionary["name"]
annotationCoord.latitude = annotationDictionary["latitude"]!
annotationCoord.longitude = annotationDictionary["longitude"]!
var annotationPoint = MKPointAnnotation()
annotationPoint.coordinate = annotationCoord
annotationPoint.title = name
locationMap.addAnnotation(annotationPoint)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top