如何使用 Google Places API 获取某个地点的位置坐标?

How to get location coordinates for a place using Google Places API?

我正在实施 Google Places API,使用自动完成我确实获得了地点 ID,现在通过使用地点 ID 我想找到该地点的位置坐标。 GMSPlace 没有 return 几何位置。我搜索了但没有得到任何帮助。

GMSPlacesClient *placeClient=[GMSPlacesClient sharedClient];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterEstablishment;

[placeClient autocompleteQuery:str
                        bounds:nil
                        filter:filter
                      callback:^(NSArray *results, NSError *error) {
                          if (error != nil) {
                              NSLog(@"Autocomplete error %@", [error localizedDescription]);
                              return;
                          }

                          for (GMSAutocompletePrediction* result in results) {
                              //NSLog(@"Result '%@'", result.attributedFullText.string);
                              [placeName addObject:[NSString stringWithFormat:@"%@",result.attributedFullText.string]];
                              [placeID addObject:[NSString stringWithFormat:@"%@",result.placeID]];
                          }

                   NSLog(@"%@",placeName);
                   NSLog(@"%@",placeName);
   }];
}   

如果您有地点 ID,您可以使用此 URL 找到坐标。这为您提供了您所在位置的详细信息。

https://maps.googleapis.com/maps/api/place/details/json?input=bar&placeid=PLACE_ID&key=API_KEY

我认为您可以像这样使用地点 ID 来获取地点详情

NSString *placeID = @"ChIJV4k8_9UodTERU5KXbkYpSYs";

[_placesClient lookUpPlaceID:placeID callback:^(GMSPlace *place, NSError *error) {
  if (error != nil) {
    NSLog(@"Place Details error %@", [error localizedDescription]);
    return;
  }

  if (place != nil) {
    NSLog(@"Place name %@", place.name);
    NSLog(@"Place address %@", place.formattedAddress);
    NSLog(@"Place placeID %@", place.placeID);
    NSLog(@"Place attributions %@", place.attributions);
  } else {
    NSLog(@"No place details for %@", placeID);
  }
}];

参考:here

您可以从地点 ID 获取地点详情。检查此 Dock。此调用将 return GMSPlace。 GSMPlace 有 属性 坐标。

MainDictionaryNSMutableDictionary

plceidkey = @"YOUR_GOOGLE_MAP_PLACE_KEY"; 


NSString *URLString1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/details/json?placeid=%@&key=%@", "YOUR_PLACE_ID",plceidkey];

NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL
                                                               URLWithString:URLString1]];

NSData *response = [NSURLConnection sendSynchronousRequest:request1
                                                 returningResponse:nil error:nil];
NSError *error;
MainDictionary=[NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];

// here you got address
address1=[[MainDictionary valueForKey:@"result"]valueForKey:@"formatted_address"];

// here got your place name
placename=[[MainDictionary valueForKey:@"result"]valueForKey:@"name"];

// here got latitude of your place       
lat1=[[[[MainDictionary valueForKey:@"result"]valueForKey:@"geometry"] valueForKey:@"location" ]valueForKey:@"lat"];

// here got logitude of your place 
long1=[[[[MainDictionary valueForKey:@"result"]valueForKey:@"geometry"] valueForKey:@"location" ]valueForKey:@"lng"];

如果对此代码有任何疑问,请告诉我,我只是帮助你。

编码愉快。