ios - 根据以前的位置绘制折线,以跟踪用户路径
问题描述
我在 Xcode 中使用了以下代码来显示我过去位置的折线。但它似乎没有出现。任何帮助将不胜感激。
我不确定我将位置存储在数组中的方法是否错误,或者我显示折线的代码是否错误。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
theLabel.text = "\(locations[0])"
storedLocations.append(locations[0] as CLLocation)
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapKitView.setRegion(region, animated:true)
print (location.speed)
print (location.altitude)
print (location.coordinate)
print (location.course) //Direction you are heading in NSEW
altitude.text = String(location.altitude)
mgrs.text = String(location.coordinate.latitude)
if (storedLocations.count > 1)
{
let sourceIndex = storedLocations.count - 1
let destinationIndex = storedLocations.count - 2
let c1 = storedLocations[sourceIndex].coordinate
let c2 = storedLocations[destinationIndex].coordinate
var a = [c1, c2]
let polyline = MKPolyline(coordinates: &a, count: a.count)
mapKitView.add(polyline)
print("running")
}
self.mapKitView.showsUserLocation = true
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 5.0
return renderer
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
解决方案
推荐阅读
- python - 如何对推文进行分类(支持与不支持)以预测选举结果
- google-cloud-firestore - 设置文档失败时,如何在我的 firebase 函数中发现错误?
- linux - 最大化 linux 原始套接字吞吐量
- amazon-web-services - 是否可以在 2 个存储桶之间复制特定的 S3 文件夹?
- java - 将 SQL 'IN' 子查询转换为 JPA 标准查询的问题
- batch-file - 批处理文件:从第二个输出行错误设置变量,输出上有空格“此时意外”
- java - 线程如何在 Spring Async 函数中成功终止以及如何在 java 中的多线程方法中递归调用相同的函数
- java - 生成随机一致地址
- javascript - 如何将数据从客户端发送到节点
- java - 错误:包 com.badlogic.gdx.backends.lwjgl 不存在