简体   繁体   中英

How to place markers by tapping at google maps in iOS using swift

How to place markers by tapping at google maps in iOS using swift? Code is as follows:

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) 
{   
    let markerr = GMSMarker(position: coordinate)
    markerr.position.latitude = coordinate.latitude
    markerr.position.longitude = coordinate.longitude
    print("hello")
    print(markerr.position.latitude)
    let ULlocation = markerr.position.latitude
    let ULlgocation = markerr.position.longitude
    print(ULlocation)
    print(ULlgocation)
    markerr.map = self.googleMapsView
    mapView.delegate = self
 }
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    mapView.clear()
    DispatchQueue.main.async {
        let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
        self.marker.position = position
        self.marker.map = mapView
        self.marker.icon = UIImage(named: "default_marker")
        print("New Marker Lat Long - ",coordinate.latitude, coordinate.longitude)
    }
}

Check if you have done the following correctly.

  1. Set GMSMapViewDelegate
  2. set mapView.delegate = self inside your ViewDidLoad() (Not in mapVieWdidTapAt function. Otherwise this function is not going to be called.)
  3. Set mapVieWdidTapAt function

     func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { mapView.clear() let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) let marker = GMSMarker(position: position) marker.title = "Hello World" marker.map = mapView }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM