gpt4 book ai didi

ios - 如何在 MKMapview 上跟踪用户位置并在用户路径上画线

转载 作者:行者123 更新时间:2023-12-01 23:28:45 25 4
gpt4 key购买 nike

当用户单击 map 上的按钮时,我想在mapView上跟踪我的用户路径,并沿着路径绘制一条蓝色或红色的线,如下图所示。我还想测量用户行驶的距离。目前我正在使用 MKMapView。使用 iOS map 套件可以完成此任务吗?或者我应该继续使用 Google map SDK。我刚刚开始学习iOS开发,如果您发现问题不符合顺序,请耐心等待。 enter image description here预先感谢...;)

最佳答案

可能有点太晚了,但为了相关性,这里是 @Rinju 在 Swift 中的代码(我也在这里添加了更多信息):

override func viewDidLoad() {
super.viewDidLoad()
//This is a dummy location, you'd add locations to it using the
// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
let location:CLLocation = CLLocation(latitude: 200, longitude: 100)
let locationArray:Array<CLLocation> = [location]
let camera:GMSCameraPosition = GMSCameraPosition.camera(withLatitude: (locationArray.first?.coordinate.latitude)!, longitude: (locationArray.first?.coordinate.longitude)!, zoom: 2)
//You can obtain the Lat and Long for above from the list of arrays of locations you saved
//You can use the .first or .last on the array (I used first)

let mapview:GMSMapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

let path:GMSMutablePath = GMSMutablePath()
for nextLocation in locationArray {
if locationArray.index(of: nextLocation) != 0 {
//You dont want to use the first one as you've already done it
//so you start with 1
path.addLatitude(nextLocation.coordinate.latitude, longitude: nextLocation.coordinate.longitude)
}
}

let polyline:GMSPolyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.red
polyline.strokeWidth = 2
polyline.map = mapview
self.view = mapview

//I personally prefer view.addSubview(mapview)
}

关于ios - 如何在 MKMapview 上跟踪用户位置并在用户路径上画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20129683/

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