[英]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)
}
}
檢查您是否正確執行了以下操作。
GMSMapViewDelegate
mapView.delegate = self
(不在mapVieWdidTapAt
函數中。否則不會調用此函數。) 設置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.