0

I am using this method to show the string of location using current location latitude and longitude but it is showing differently

NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",location.coordinate.latitude, location.coordinate.longitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];

NSData *data = [locationString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSDictionary *dic = [[json objectForKey:@"results"] objectAtIndex:0];
NSArray* arr = [dic objectForKey:@"address_components"];
//Iterate each result of address components - find locality and country
NSString *cityName;
NSString *countryName;
for (NSDictionary* d in arr)
{
    NSArray* typesArr = [d objectForKey:@"types"];
    NSString* firstType = [typesArr objectAtIndex:0];
    if([firstType isEqualToString:@"locality"])
        cityName = [d objectForKey:@"long_name"];
    if([firstType isEqualToString:@"country"])
        countryName = [d objectForKey:@"long_name"];

}

NSString* locationFinal = [NSString stringWithFormat:@"%@,%@",cityName,countryName];
NSLog(@"Final Location %@ ",locationFinal);

but final location is showing this type :-

Final Location नठदिलà¥à¤²à¥,India

Why it is showing this type? Can anyone know about this.

user6438311
  • 117
  • 1
  • 3
  • 13

2 Answers2

0

Please supply the language with the API params. If language is not supplied, the geocoder attempts to use the preferred language as specified in the Accept-Language header, or the native language of the domain from which the request is sent.

So please replace the code as with the language parameter as like this.

NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false&language=en",location.coordinate.latitude, location.coordinate.longitude];

and try again.

Prabhu
  • 840
  • 11
  • 28
  • This is a comment, not an answer. Also how is the language important? Surely the encoding is way more important. – Droppy Oct 12 '16 at 07:21
  • I have referred this https://developers.google.com/maps/documentation/geocoding/intro Just look for the optional parameter section. I have executed @user6438311 by choosing the phone language other than English. i got the output relevant to this Final Location नठदिलà¥à¤²à¥,à¥à¤²à¥ But if we pass the optional param of language i got correct response. – Prabhu Oct 12 '16 at 09:05
0

I believe that is an uninitialzed variable which is pointing into random memory.

Try:

NSString *cityName = nil;
NSString *countryName = nil;

Short-circuit your for loop:

for (NSDictionary* d in arr)
{
    // Add this after the existing code:
    if (cityName && countryName)
        break;
}

and check for errors before presenting the results:

if (cityName && countryName) {
    NSString* locationFinal = [NSString stringWithFormat:@"%@,%@",cityName,countryName];
    NSLog(@"Final Location %@ ",locationFinal);
} else {
    NSLog(@"Failed to find location");
}

Finally your JSON-processing code does no error-checking at all. That's a mistake.

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • This is just validation, not an answer. Actually The above response doesn't gave any null value. – Prabhu Oct 12 '16 at 13:52
  • @Prabhu So you maintain that `cityName` and `countryName` are *always* set to a valid string for any value of latitude and longitude? – Droppy Oct 12 '16 at 14:18