gpt4 book ai didi

ios - 获取谷歌地图的经纬度中心

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:55 25 4
gpt4 key购买 nike

我想显示全屏 mapView,始终获取 mapView 中心的纬度和经度并在该点显示标记。

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

let lat = mapView.camera.target.latitude
print(lat)

let lon = mapView.camera.target.longitude
print(lon)


marker.position = CLLocationCoordinate2DMake(CLLocationDegrees(centerPoint.x) , CLLocationDegrees(centerPoint.y))
marker.map = self.mapView
returnPostionOfMapView(mapView: mapView)

}

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
print("idleAt")

//called when the map is idle

returnPostionOfMapView(mapView: mapView)

}

func returnPostionOfMapView(mapView:GMSMapView){
let geocoder = GMSGeocoder()
let latitute = mapView.camera.target.latitude
let longitude = mapView.camera.target.longitude




let position = CLLocationCoordinate2DMake(latitute, longitude)
geocoder.reverseGeocodeCoordinate(position) { response , error in
if error != nil {
print("GMSReverseGeocode Error: \(String(describing: error?.localizedDescription))")
}else {
let result = response?.results()?.first
let address = result?.lines?.reduce("") { $0 == "" ? $1 : $0 + ", " + $1 }

print(address)
// self.searchBar.text = address
}
}
}

我使用此代码如何知道 returnPostionOfMapView 方法中返回的纬度和经度是中心 mapView 的位置并在此位置显示标记?

最佳答案

通过使用 google map 的 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) 委托(delegate),您正确地获得了 map 的中心。

取中心坐标变量

var centerMapCoordinate:CLLocationCoordinate2D!

实现此委托(delegate)以了解中心位置。

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let latitude = mapView.camera.target.latitude
let longitude = mapView.camera.target.longitude
centerMapCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.placeMarkerOnCenter(centerMapCoordinate:centerMapCoordinate)
}

在中心点放置标记的功能

func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {
let marker = GMSMarker()
marker.position = centerMapCoordinate
marker.map = self.mapView
}

在这种情况下你会得到很多标记。所以保持全局持有marker并检查它是否已经存在,只需更改位置

var marker:GMSMarker!

func placeMarkerOnCenter(centerMapCoordinate:CLLocationCoordinate2D) {
if marker == nil {
marker = GMSMarker()
}
marker.position = centerMapCoordinate
marker.map = self.mapView
}

关于ios - 获取谷歌地图的经纬度中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com