簡體   English   中英

如何通過在 iOS 中使用 swift 在谷歌地圖上點擊來放置標記

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

如何使用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
 }
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)
    }
}

檢查您是否正確執行了以下操作。

  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 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM