Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latitude & Longitude for Autocomplete place search google API in ios

I implemented the Autocomplete Place Search API from Google. It works fine.

But in the response, the google is not returning the "Latitude & Longitude" of the place.

my code is as follow

NSString *strUrl =
[[[NSString stringWithFormat:@"%@%@%@?sensor=false&key=%@&input=%@",
   PLACES_API_BASE,
   TYPE_AUTOCOMPLETE,
   OUT_JSON,API_KEY,
   txt_search.text]
  stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
 stringByReplacingOccurrencesOfString:@" " withString:@"%20"];


NSLog(@"--- address ---  %@",strUrl);

NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl] options:NSDataReadingUncached error:nil];


NSDictionary  *dictResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:Nil];
NSLog(@"%@",dictResponse);

Response

{
  "status": "OK",
  "predictions": [ {
    "description": "Paris, France",
    "id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
    "reference": "CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "France",
      "offset": 7
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }
like image 873
user2893370 Avatar asked Nov 28 '25 06:11

user2893370


2 Answers

According to Place Autocomplete documentation objects of predictions array don't have location info. You can use reference property to retrieve place details.

The returned predictions are designed to be presented to the user to aid them in selecting the desired place. You can send a Place Details request for more information about any of the places which are returned.

like image 163
vokilam Avatar answered Nov 29 '25 20:11

vokilam


Try this

NSString * urlString =[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=Your address &sensor=true"];
NSURL * url=[NSURL URLWithString:urlString];
NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:url];
NSURLResponse * response;
NSError * error;
NSData * responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * outputData=[[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@" output is %@",outputData);

In this change the "your address" to the location u want

like image 21
Chandru Avatar answered Nov 29 '25 20:11

Chandru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!