Is there any way to get GSMAddress from placeID. I am interested in getting the address by using the placeID obtained from autocompleteQuery method.
P.S: I have a placeID and finding a way to get corresponding GMSAddress in iOS.
I found a thread here but that does not help.
Thanks,
After autocomplete, we do a place look up, then pass the coordinates to GMSGeocoder to retrieve the details, but I am still missing street_name and street_number.
CLLocationCoordinate2D addressCoordinates = CLLocationCoordinate2DMake(latitude,longitude);
GMSGeocoder* coder = [[GMSGeocoder alloc] init];
[coder reverseGeocodeCoordinate:addressCoordinates completionHandler:^(GMSReverseGeocodeResponse *results, NSError *error) {
if (error) {
NSLog(#"Error %#", error.description);
} else {
GMSAddress* address = [results firstResult];
NSLog(#"thoroughfare %#",address.thoroughfare);
NSLog(#"locality %#",address.locality);
NSLog(#"subLocality %#",address.subLocality);
NSLog(#"administrativeArea %#",address.administrativeArea);
NSLog(#"postalCode %#",address.postalCode);
NSLog(#"country %#",address.country);
NSLog(#"lines %#",address.lines);
}
}];
I'm thinking of switching to iOS's own reverse geocoder.
[CLGeocoder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] completionHandler:
^(NSArray* placemarks, NSError* error) {
It seems there is no direct way to get the GSMAddress from GMSPlace placeID.
One way that I figured out is to use GMSGeocoder().reverseGeocodeCoordinate method along with coordinate property from GMSPlace.
The steps I followed can be summed as:
Use lookUpPlaceID method to find the coordinates
Reverse geocode using the coordinates obtained from lookUpPlaceID
Code is as follows:
placesClient?.lookUpPlaceID(placeID, callback: {
(place, error) -> Void in
// Get place.coordinate
GMSGeocoder().reverseGeocodeCoordinate(place!.coordinate, completionHandler: {
(response, error) -> Void in
for addObj in response.results() {
// Address object
}
})
})
Comments and better solutions are welcome.
Related
I have implemented google map on IOS, now i want to locate marker on some company brand name, before i have experience of implementing in Android.
In Android getFromLocationName() to get the address info like lat, long from geocoder but I am not getting any in build function like this in IOS?
any help?
i think its giving you error because, geocoder is not able to find the location.
check this code ,
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:#"Palolem Beach, Goa, India" completionHandler:^(NSArray* placemarks, NSError* error){
if (!error) {
for (CLPlacemark* aPlacemark in placemarks)
{
NSLog(#"place--%#", [aPlacemark locality]);
NSLog(#"lat--%f\nlong--%f",aPlacemark.location.coordinate.latitude,aPlacemark.location.coordinate.longitude);
}
}
else{
NSLog(#"error--%#",[error localizedDescription]);
}
}];
I am trying to display autocomplete results using Google Places API for iOS, I might be missing something but here is my code for the task:
- (void)placeAutocomplete {
GMSVisibleRegion visibleRegion = mapView_.projection.visibleRegion;
//GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:visibleRegion.farLeft
// coordinate:visibleRegion.nearRight];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterGeocode;
GMSPlacesClient *placesClient;
[placesClient autocompleteQuery:#"indira nagar"
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 '%#' with placeID %#", result.attributedFullText.string, result.placeID);
}
}];
}
I've added the pods(pod 'GoogleMaps'), I even got the map But when i try autocomplete all I am getting is (in the log)"Google Maps SDK for iOS version: 1.10.17867.0" can any one help understand how it works?
This is the issue of my api key so i had to change(create a new one) to resolve this issue
I am using Autocomplete of Google Places API for iOS, and it returns placeID. How can I get GMSAddress from the placeID? I currently use lookupPlaceID to get GMSPlace, and then use reverseGeocodeCoordinate to get GMSAddress from coordinate in GMSPlace. But it request Google API 3 times: AutoComplete, lookupPlaceID, reverseGeocodeCoordinate.
Is there any way to get GMSAddress from PlaceID or GMSPlace directly?
This is a bug with 1.1.0 of the Google Maps SDK for iOS. See here.
As the documentation states here, the address property should expose a GMSAddress on GMSPlace instances.
I've just switched to using the REST API. It's not too bad to write a quick wrapper for the API and ditch the Google SDK completely. If you are using Alamofire ping me and I may be able to share what I've written.
I could get the address from placedID in one API call, here is the code:
GMSPlacesClient *placesClient = [[GMSPlacesClient alloc] init];
[placesClient lookUpPlaceID:result.placeID callback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(#"Place Details error %#", [error localizedDescription]);
return;
}
if (place != nil) {
NSString *locality = #"";
for (GMSAddressComponent *add in place.addressComponents) {
//locality
NSLog(#"%#",add.type);
NSLog(#"%#",add.name);
//type will be street_name, locality, admin_area, country,
//name will hold the corresponding value
}
} else {
NSLog(#"No place details for %#", result.placeID);
}
}];
Now for the time being, is the code below safe (enough) for this version of the SDK (1.10.5)? What is the probability that this will break in the next 6 months?
NSArray* dic = [place valueForKey:#"addressComponents"];
for (int i=0;i<[dic count];i++) {
if ([[[dic objectAtIndex:i] valueForKey:#"type"] isEqualToString:#"street_number"]) {
NSLog(#"street_number: %#",[[dic objectAtIndex:i] valueForKey:#"name"]);
}
I have coordinates from a user tapping the map on the screen. Can I get the GMSPlace from the Google API by using the coordinates. Couldn't find anything in the documents or online that helped.
Thanks
If you tap on a place, you can also get the place id, then use the place id to create a GMSPlace instance like this:
GMSPlacesClient *placeClient = [GMSPlacesClient sharedClient];
[placeClient lookUpPlaceID:placeID callback:^(GMSPlace * _Nullable result, NSError * _Nullable error) {
if(!error) {
[self updatePlace:result];
}else {
NSLog(#"Error : %#",error.localizedDescription);
}
}];
I'm not sure if this is new with google maps but I think it looks like this version of Google Maps for iOS already has a reverse geocoding method...one which I can't really figure out how to use. In their documentation page, under section Camera Position, they have a function, but it doesn't look like the one that came with the SDK...or maybe there's something I don't understand here. Can someone help out? Here's the function that comes up in XCode:
- (void)reverseGeocodeCoordinate:(CLLocationCoordinate2D)coordinate
completionHandler:(GMSReverseGeocodeCallback)handler{}
How can I use that if I have my coordinates in an array? How do I get the address and country and all the possible results?
By the way, this is what they have...how does it compare:
- (void)mapView:(GMSMapView *)mapView
idleAtCameraPosition:(GMSCameraPosition *)cameraPosition {
id handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
if (error == nil) {
GMSReverseGeocodeResult *result = response.firstResult;
GMSMarker *marker = [GMSMarker markerWithPosition:cameraPosition.target];
marker.title = result.addressLine1;
marker.snippet = result.addressLine2;
marker.map = mapView;
}
};
[geocoder_ reverseGeocodeCoordinate:cameraPosition.target completionHandler:handler];
}
You can try this code
[[GMSGeocoder geocoder]reverseGeocodeCoordinate:CLLocationCoordinate2DMake(latitude, longitude)
completionHandler:^(GMSReverseGeocodeResponse * response, NSError *error){
for(GMSReverseGeocodeResult *result in response.results){
NSLog(#"addressLine1:%#", result.addressLine1);
NSLog(#"addressLine2:%#", result.addressLine2);
}
}];