Rohit Bhatia
Rohit Bhatia

Reputation: 1

How I can retrieve the coordinates by searching the place

I'm facing the problem while retrieving the data from google map Place autocomplete because my view did load latitude and longitude [Unity] sare default while showing the map

let camera = GMSCameraPosition.camera(withLatitude: 30.7096, longitude: 76.7016, zoom: 12)

let mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 58, width: 320, height: 510), camera: camera)

    let marker = GMSMarker()
    marker.position = camera.target
    marker.snippet = "Hello World"
    marker.map = mapView
    self.mapSize.addSubview(mapView)
}

@IBAction func autocompleteClicked(_ sender: UIButton)
{
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self as! GMSAutocompleteViewControllerDelegate
    present(autocompleteController, animated: true, completion: nil)
}

@IBOutlet weak var mapSize: UIView!

Upvotes: 0

Views: 743

Answers (2)

Jack
Jack

Reputation: 14329

You are missing delegate as GMSAutocompleteViewControllerDelegate which need to be add as -

class ProductDetailsViewController: UIViewController,GMSAutocompleteViewControllerDelegate {}

hence after this no more force wrap of

autocompleteController.delegate = self as! GMSAutocompleteViewControllerDelegate

just write

autocompleteController.delegate = self

after that you will get info as

  func viewController(viewController: GMSAutocompleteViewController, didAutocompleteWithPlace place: GMSPlace) {
        print("latitude is ",place.coordinate.latitude)
        print("longitude is ",place.coordinate.longitude)

  }

Upvotes: 1

sarosh mirza
sarosh mirza

Reputation: 702

Did you implement the GMSAutocompleteViewControllerDelegate protocol in the parent view controller?

    extension ViewController: GMSAutocompleteViewControllerDelegate {

    // Handle the user's selection.
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
        print("Place name: \(place.name)")
        print("Place address: \(place.formattedAddress)")
        print("Place attributions: \(place.attributions)")
        dismiss(animated: true, completion: nil)
    }

    func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
        // TODO: handle the error.
        print("Error: ", error.localizedDescription)
    }

    // User canceled the operation.
    func wasCancelled(_ viewController: GMSAutocompleteViewController) {
        dismiss(animated: true, completion: nil)
    }

    // Turn the network activity indicator on and off again.
    func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

    func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }

}

Upvotes: 1

Related Questions