ios - Swift 5:如何在点击/触摸坐标时在 GoogleMap 上添加标记?
问题描述
我正在学习适用于 iOS 的 GoogleMap-SDK,在此过程中我确实搜索了上述问题并找到了令人满意的答案,但没有找到实际有用的答案。
它添加了标记,但没有地名或地点详细信息,没有该标记就没有那么有用,为此我必须搜索其他答案以从坐标中获取地址。
所以,在这里我结合了这两个答案,以节省其他开发人员的时间并使其标记更实用。
解决方案
对于 Swift 5.0+
首先,确保您已将
GMSMapViewDelegate
委托添加到您的 ViewController 类
我们不需要UILongPressGestureRecognizer
或UITapGestureRecognizer
为此GMSMapViewDelegate
提供方便的默认方法。
///This default function fetches the coordinates on long-press on `GoogleMapView`
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
//Creating Marker
let marker = GMSMarker(position: coordinate)
let decoder = CLGeocoder()
//This method is used to get location details from coordinates
decoder.reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) { placemarks, err in
if let placeMark = placemarks?.first {
let placeName = placeMark.name ?? placeMark.subThoroughfare ?? placeMark.thoroughfare! ///Title of Marker
//Formatting for Marker Snippet/Subtitle
var address : String! = ""
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)
}
// Adding Marker Details
marker.title = placeName
marker.snippet = address
marker.appearAnimation = .pop
marker.map = mapView
}
}
}
希望能帮助到你 !!!
推荐阅读
- c# - C# DotNetZip 错误:尚未计算最终哈希。在 Ionic.Zip.WinZipAesCipherStream.get_FinalAuthentication()
- string - 可以一次使用多个 BASH 变量操作吗?
- jmeter - 如何从 JSR223/Beanshell 读取 JMeter 测试 Cmd 行参数
- codeigniter - 在 Codeigniter 中获取连接表的数据
- r - 计算R中数据帧中的最大值
- android - 在 ConstraintLayout 中查看内容越界
- javascript - 在 socket.io 中管理用户
- reactjs - redux-form SubmissionError 不更新错误状态
- r - 闪亮的多项选择图表更新
- html - 选择每个
不是其父元素的第一个子元素的元素