1

As the question says I am trying to add pins to my map based on the coordinates returned by my php file. Said file returns the following results [{"dogid":"1","latitude":"15.435786","longitude":"-21.318447"},{"dogid":"1","latitude":"14.00000","longitude":"-18.536711"}]

What I am doing (well I believe i am) is taking the values from the link and saving them to a string. Secondly, save that string value to an array. Then, I go thru this array and save out the latitude and longitude and assign it to CLLocationCordinate 2dcoord. After whch I expect both pins to be dropped on whatever location they received.

However, what occurs is: Upon running the program, when it arrives on this lin

for (NSDictionary *row in locations) {

the loop is not run to assign the values, and it jumps to the end. Oddly, a single pin is dropped on the map (thou location doesnt appear to be the values that it waas passed).

Would appreciate a little incite into the matter.

Thanks

- (void)viewDidAppear:(BOOL)animated
{


    NSMutableArray *annotations = [[NSMutableArray alloc] init];
    NSURL *myURL =[NSURL URLWithString:@"link.php"];
    NSError *error=nil;
    NSString *str=[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];

    CLLocationCoordinate2D coord;

    NSArray *locations=[NSArray arrayWithContentsOfFile:str];
    for (NSDictionary *row in locations) {
        NSNumber *latitude = [row objectForKey:@"latitude"];
        NSNumber *longitude = [row objectForKey:@"longitude"];
        // NSString *title = [row objectForKey:@"title"];
        //Create coordinates from the latitude and longitude values
        coord.latitude = latitude.doubleValue;
        coord.longitude = longitude.doubleValue;
    }

    MKPointAnnotation *pin = [[MKPointAnnotation alloc] init];
    pin.coordinate = coord;
    [self.mapView addAnnotation:pin];
}
Craig
  • 8,093
  • 8
  • 42
  • 74
Sleep Paralysis
  • 449
  • 7
  • 22
  • 1
    This has nothing to do with MapKit. The `arrayWithContentsOfFile` method doesn't convert a string containing JSON to an array (it assumes you're passing it a string containing a _path to a file (containing a plist with an array)_. Since your string is not a valid file path, the array is empty. –  Sep 07 '14 at 21:16
  • Instead, you want to use `NSJSONSerialization`. See http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization, http://stackoverflow.com/questions/20399087/json-parsing-using-nsjsonserialization-in-ios, etc. –  Sep 07 '14 at 21:17

1 Answers1

0

It looks like you are trying to save api response to and Array.

Api always returns json string which is NSString.

You need to convert decode json string.

In your case

NSString *str=[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];

you need to decode str with [NSJSONSerialization JSONObjectWithData:<#(NSData )#> options:<#(NSJSONReadingOptions)#> error:<#(NSError *)#>] which give you proper array of dictionary.

Hope it will help you

Kuntal Gajjar
  • 792
  • 6
  • 12