我是Swift和Google Maps SDK的新手,想知道如何使用Google Maps SDK获取用户点击的坐标。例如,如果用户在地图上按下手指,会在那里创建一个注释。我非常感谢您的帮助,谢谢。
我是Swift和Google Maps SDK的新手,想知道如何使用Google Maps SDK获取用户点击的坐标。例如,如果用户在地图上按下手指,会在那里创建一个注释。我非常感谢您的帮助,谢谢。
mapView:didLongPressAtCoordinate:
的方法,它在长按特定坐标后被调用。请参见此处的参考资料。func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
marker.title = "Hello World"
marker.map = mapView
}
对于轻触手势,可以实现一个类似的委托方法,称为mapView:didTapAtCoordinate:
,可以以类似的方式使用:
对于轻拍手势,可以实现一个类似的委托方法,称为mapView:didTapAtCoordinate:
,可以以类似的方式使用:
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
print("Tapped at coordinate: " + String(coordinate.latitude) + " "
+ String(coordinate.longitude))
}
针对 Swift 5.0+
首先,请确保您已将
GMSMapViewDelegate
委托添加到您的 ViewController 类中
在您的类中添加此默认函数
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
debugPrint("Coordinates: ", coordinate)
}
如果你只想要坐标,那么上面的函数对你来说是完美的。但如果你想创建标记或从触摸获取本地地址,则请参见下面的函数。
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate) //Add this line if you want to add marker
let decoder = CLGeocoder()
decoder.reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) { placemarks, err in
if let placeMark = placemarks?.first {
let plName = placeMark.name ?? placeMark.subThoroughfare ?? placeMark.thoroughfare! //Place Name
var address : String! = "" //This will be the local address
if let subLocality = placeMark.subLocality ?? placeMark.name {
address.append(subLocality)
address.append(", ")
}
if let city = placeMark.locality ?? placeMark.subAdministrativeArea {
address.append(city)
address.append(", ")
}
if let state = placeMark.administrativeArea, let country = placeMark.country {
address.append(state)
address.append(", ")
address.append(country)
}
// Add Marker:
marker.title = plName
marker.snippet = address
marker.appearAnimation = .pop
marker.map = mapView
}
}
}
此函数不仅获取坐标,还使用从坐标中获取的所有详细信息(如地名、城市、州、国家等)创建标记
如果您只想要本地地址,则删除与marker
相关的所有代码行
我使用
CLGeocoder
而不是GoogleMapDelegate
的GMSGeocoder
原因是Apple的CLGeocoder
在获取地名时更加精确,而GMSGeocoder
不会准确地获取地名。
试试这个
extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
print("Tapped at coordinate: " + String(coordinate.latitude) + " "
+ String(coordinate.longitude))
}
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
let geocoder = GMSGeocoder()
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
} else {
if let places = response?.results() {
if let place = places.first {
if let lines = place.lines {
// this will print the address formatted in on line
print("GEOCODE: Formatted Address: \(lines)")
// GEOCODE: Formatted Address: ["213 Borough High St, London SE1 1JA, UK"]
}
} else {
print("GEOCODE: nil first in places")
}
} else {
print("GEOCODE: nil in places")
}
}
}
// this to clear any marker on map
mapView.clear()
// this to center the camera of the map to the tapped area
let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 15.0)
mapView.animate(to: camera)
// this to add a marker on the tapped location
let marker = GMSMarker(position: coordinate)
marker.appearAnimation = .pop
marker.map = mapView
}