Search code examples
objective-cxcodeclplacemark

how to add Latitude and longitude value to nsmutablearray from geocoder values?


I want to add latitude and longitude value to NSMutableArray from geocoder value but I get an error in the image .

-(void) loadDataArray:(NSString *) text{

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:[NSString stringWithFormat:@"%@",text] completionHandler:^(NSArray* placemarks, NSError* error){


    for (CLPlacemark* aPlacemark in placemarks)
    {
        NSLog(@"aPlace: %@",aPlacemark.country);
        NSLog(@"MyPlace: %@",userCountry);
        //if ([aPlacemark.country isEqual:userCountry]) {

            [dataArray addObject:aPlacemark];
        //NSNumber *lat = [NSNumber numberWithDouble:aPlacemark.location.coordinate.latitude];
        //NSNumber *lon= [NSNumber numberWithDouble:aPlacemark.location.coordinate.longitude];


        [dataArrayLat addObject:aPlacemark.location.coordinate.latitude];
        [dataArrayLon addObject:aPlacemark.location.coordinate.longitude];

            //NSLog(@" %@",lat);
        //}
        //else{
          //  NSLog(@"Bulunamadı");

        //}

    }
    [searchTable reloadData];
}];

}

enter image description here

Thanks in replies.

EDIT

I want to use for search location and show tableview.I select tableview item and then location show on Apple Map.


Solution

  • You're trying to add a primitive to an NSMutableArray, but it will only hold objects.

    The quickest workaround would be to wrap your CLLocationDegrees value (which is just a double) in an NSNumber, like this:

    [dataArrayLat addObject:@(aPlacemark.location.coordinate.latitude)];
    [dataArrayLon addObject:@(aPlacemark.location.coordinate.longitude)];
    

    Keep in mind though, that you'll have to unwrap the value later if you want to access the double again. You would do so like this:

    NSNumber *num = dataArrayLat[someIndex];
    double latitude = num.doubleValue;