【问题标题】:How to place markers by tapping at google maps in iOS using swift如何使用swift在iOS中点击谷歌地图来放置标记
【发布时间】:2018-07-03 04:08:49
【问题描述】:

如何使用 swift 在 iOS 中通过点击谷歌地图来放置标记?代码如下:

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
 }

【问题讨论】:

  • 无需使用mapView.delegate = self这一行以及您面临的问题
  • 点按功能没有工作
  • 你的意思是这个方法没有被称为func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) 正确否则
  • 调用You just have to initialize delegate at viewDidLoad`mapView.delegate = self

标签: ios swift google-maps google-maps-markers


【解决方案1】:
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)
    }
}

【讨论】:

    【解决方案2】:

    检查您是否正确执行了以下操作。

    1. 设置GMSMapViewDelegate
    2. 在 ViewDidLoad() 中设置 mapView.delegate = self(不在 mapVieWdidTapAt 函数中。否则不会调用此函数。)
    3. 设置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
      
          }
      

    【讨论】:

      猜你喜欢
      • 2020-06-18
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多