Swift - GoogleMaps SDK 获取触摸坐标
Swift - GoogleMaps SDK get coordinates on touch
我是 swift 和 Google Maps SDK 的新手,想知道如何使用 Google Maps SDK 获取用户点击位置的坐标。例如,如果用户将手指按住地图上的某个地方,就会在该处创建注释。非常感谢您的帮助,谢谢。
在 GMSMapViewDelegate 中有一个名为:mapView:didLongPressAtCoordinate:
的方法,它在特定坐标处的长按手势后调用。见参考资料here.
通过实施此方法,您可以向地图视图添加标记:
func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
marker.title = "Hello World"
marker.map = mapView
}
对于轻击手势,可以实现一个名为 mapView:didTapAtCoordinate:
的类似委托方法,它可以类似的方式使用:
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
print("Tapped at coordinate: " + String(coordinate.latitude) + " "
+ String(coordinate.longitude))
}
试试这个
extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
print("Tapped at coordinate: " + String(coordinate.latitude) + " "
+ String(coordinate.longitude))
}
}
为Swift5.0+
First, make sure you have added GMSMapViewDelegate
delegate to your ViewController Class
在您的 class
中添加此默认功能
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
相关的代码行
The reason why i have used CLGeocoder
and not GMSGeocoder
from
GoogleMapDelegate
is that Apple's CLGeocoder
is much more precise
in getting the place-name while GMSGeocoder
does not fetch
place-name accurately.
- 另请注意:Apple 的地理编码请求对每个应用程序都有速率限制,因此在短时间内发出过多请求可能
导致一些请求失败。
我是 swift 和 Google Maps SDK 的新手,想知道如何使用 Google Maps SDK 获取用户点击位置的坐标。例如,如果用户将手指按住地图上的某个地方,就会在该处创建注释。非常感谢您的帮助,谢谢。
在 GMSMapViewDelegate 中有一个名为:mapView:didLongPressAtCoordinate:
的方法,它在特定坐标处的长按手势后调用。见参考资料here.
通过实施此方法,您可以向地图视图添加标记:
func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
marker.title = "Hello World"
marker.map = mapView
}
对于轻击手势,可以实现一个名为 mapView:didTapAtCoordinate:
的类似委托方法,它可以类似的方式使用:
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
print("Tapped at coordinate: " + String(coordinate.latitude) + " "
+ String(coordinate.longitude))
}
试试这个
extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
print("Tapped at coordinate: " + String(coordinate.latitude) + " "
+ String(coordinate.longitude))
}
}
为Swift5.0+
First, make sure you have added
GMSMapViewDelegate
delegate to your ViewController Class
在您的 class
中添加此默认功能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
The reason why i have used
CLGeocoder
and notGMSGeocoder
fromGoogleMapDelegate
is that Apple'sCLGeocoder
is much more precise in getting the place-name whileGMSGeocoder
does not fetch place-name accurately.
- 另请注意:Apple 的地理编码请求对每个应用程序都有速率限制,因此在短时间内发出过多请求可能 导致一些请求失败。