0

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;
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51
  • Does `jsonArray` contain only the latitudes? It would be better to return a single JSON array with name, latitude, and longitude for each annotation together rather than a separate array for each attribute. Show a small sample of the JSON data being returned. –  Aug 20 '13 at 12:19
  • @AnnaKarenina I have added my json return data. Does it matter if I return each jsonArray alone? – Luai Kalkatawi Aug 20 '13 at 16:09

3 Answers3

0

Try

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

of MKMApView

Samkit Jain
  • 2,523
  • 16
  • 33
0

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.

  • It is working now... I thought I should run it in the `didUpdateUserLocation` delegate method not in `viewDidLoad` when I saw lots of tutorial And a someone told me to run `FOR`loop separate each time due to it will be much safe. Thanks – Luai Kalkatawi Aug 21 '13 at 07:53
  • Instead of opening new question how can I find the nearest pin for me? – Luai Kalkatawi Aug 21 '13 at 07:54
  • If straight-line distance is acceptable, you can use distanceFromLocation method of CLLocation class. If driving distance is required, the current iOS SDK doesn't provide this (and you'll need to get the data from a third-party API). –  Aug 21 '13 at 12:33
  • I just want to show this pin is the nearest one for you, so do I have to use the `distanceFromLocation` ? – Luai Kalkatawi Aug 21 '13 at 12:36
0

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)
}
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56