我有地图视图,用户可以拖动标记。点击标记时,它会显示包含标记位置的代码段。
如何获取标记的经度和纬度? 如果标记被拖动到另一个位置。
这是我所做的:
var latitude = -7.034323799999999
var longitude = 110.42400399999997
var displayAddress = [String]()
func customizeMap() {
mapView.delegate = self
let camera = GMSCameraPosition.camera(withLatitude: Double(latitude), longitude: Double(longitude), zoom: 17)
mapView.animate(to: camera)
let markerImage = UIImage(named: "ic_home_detail_marker_location")
let markerView = UIImageView(image: markerImage)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(Double(latitude), Double(longitude))
marker.isDraggable = true
marker.iconView = markerView
marker.snippet = displayAddress[0]
mapView.selectedMarker = marker
marker.map = mapView
}
现在我正在使用静态经度和纬度
为此,您可以使用didBeginDragging marker
和didEndDragging marker
委托方法GMSMapViewDelegate
。
func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker) {
print("Drag")
}
func mapView(_ mapView: GMSMapView, didBeginDragging marker: GMSMarker) {
print("Old Coordinate - (marker.position)")
}
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
print("New Coordinate - (marker.position)")
}