GMSPlace is a class from the Google Maps SDK for iOS that represents a specific place or location with details like name, address, phone number, website, etc. If you have a set of coordinates (latitude and longitude), you can use the Google Places API to get place details for that location.

The Google Places API provides various endpoints to search for places, get place details, and more. To get place details using coordinates, you need to make a request to the "Place Details" endpoint of the Places API.

Here's how you can do it in iOS using Swift:

  1. First, make sure you have installed the Google Maps SDK for iOS and set up an API key.

  2. Import the necessary GoogleMaps and GooglePlaces modules:

swift
import GoogleMaps import GooglePlaces
  1. Use the GMSPlacesClient to fetch place details using coordinates:
swift
func getPlaceDetailsFromCoordinates(latitude: Double, longitude: Double) { let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let placesClient = GMSPlacesClient.shared() placesClient.lookUpPlaceID("your_place_id_here") { (place, error) in if let error = error { print("Error fetching place details: \(error.localizedDescription)") return } if let place = place { print("Place Name: \(place.name)") print("Place Address: \(place.formattedAddress ?? "")") // Other place details can be accessed using the 'place' object } } }

Note: Instead of using the lookUpPlaceID method, you can use other methods like lookUpPlaceID or lookUpPhoneNumber depending on the available information you have about the place.

Replace "your_place_id_here" with the actual Google Place ID of the location. If you don't have the Place ID, you can use the GMSGeocoder to get the Place ID from the coordinates:

swift
func getPlaceIDFromCoordinates(latitude: Double, longitude: Double) { let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let geocoder = GMSGeocoder() geocoder.reverseGeocodeCoordinate(coordinates) { (response, error) in if let error = error { print("Error fetching place ID: \(error.localizedDescription)") return } if let results = response?.results(), let placeID = results.first?.placeID { // Use the placeID to get place details self.getPlaceDetailsFromPlaceID(placeID) } } }

In this example, the getPlaceIDFromCoordinates function takes latitude and longitude as input and retrieves the Place ID for the location using the GMSGeocoder. Then, the getPlaceDetailsFromPlaceID function fetches the place details using the Place ID using the GMSPlacesClient. You can call these functions with the desired coordinates to get the place details for the corresponding location.

Make sure to handle errors appropriately and check if you have proper permissions to access location data on the device. Also, ensure that you have set up the Google Maps API key properly in your app.

Have questions or queries?
Get in Touch