简体   繁体   English

如何通过在 iOS 中使用 swift 在谷歌地图上点击来放置标记

[英]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?如何使用swift在iOS中的谷歌地图上点击来放置标记? 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设置GMSMapViewDelegate
  2. set mapView.delegate = self inside your ViewDidLoad() (Not in mapVieWdidTapAt function. Otherwise this function is not going to be called.)在 ViewDidLoad() 中设置mapView.delegate = self (不在mapVieWdidTapAt函数中。否则不会调用此函数。)
  3. Set mapVieWdidTapAt function设置mapVieWdidTapAt函数

     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 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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