gpt4 book ai didi

ios - 标记当前位置不断给出坐标 0,0

转载 作者:行者123 更新时间:2023-11-28 12:07:14 24 4
gpt4 key购买 nike

我正在为我使用 Xcode 9.2 和 Swift 4 制作的应用程序使用 Google map 服务。当我按下一个按钮时,我希望我的当前位置被标记在 map View 上,并在其上方有一个标记。相反,当点击按钮时,它会将我带到坐标 0,0。我究竟做错了什么?感谢您提前提供的所有帮助。

//Location Manager

var userLocation = CLLocation()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

userLocation = locations.last!

let camera = GMSCameraPosition.camera(withLatitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude, zoom: 14);
self.googleMapsView.camera = camera

//Finally stop updating location otherwise it will come again and again in this delegate

self.locationManager.stopUpdatingLocation()

}

//Button Marker Function

@IBAction func markCurrentLocation(_ sender: UIButton) {

let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

let camera = GMSCameraPosition.camera(withLatitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude, zoom: 14);
self.googleMapsView.camera = camera
self.googleMapsView.isMyLocationEnabled = true

let marker = GMSMarker(position: center)

print("Latitude :- \(userLocation.coordinate.latitude)")
print("Longitude :-\(userLocation.coordinate.longitude)")

marker.map = self.googleMapsView

marker.title = "Current Location"

googleMapsView.animate(to: camera)

//Finally stop updating location otherwise it will come again and again in this delegate

self.locationManager.stopUpdatingLocation()
}

最佳答案

它将返回 0,因为您创建了一个新的 CLLocation() 实例,其中没有任何内容。从 locationManager(_:didUpdateLocations:) 获取当前位置代表。

这样做:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
userLocation = locations.last
}

并从你的 markCurrentLocation(_:) 方法中移除 let userLocation = CLLocation()

还要确保在您的 info.plist 中添加请求 enter image description here

关于ios - 标记当前位置不断给出坐标 0,0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49163565/

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