[英]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.检查您是否正确执行了以下操作。
GMSMapViewDelegate
GMSMapViewDelegate
mapView.delegate = self
inside your ViewDidLoad() (Not in mapVieWdidTapAt
function. Otherwise this function is not going to be called.)mapView.delegate = self
(不在mapVieWdidTapAt
函数中。否则不会调用此函数。) 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.