我正在尝试编写一个可以显示电车线路的应用程序。我添加了地图,但是我在尝试找到如何将此路线添加到我的应用时遇到了问题。
我看过MKOverlayRenderer,我想我必须在地图上添加一个图像才能做到这一点。有一些教程,但它们已经过时了。
有人可以帮我解决这个问题。谢谢
答案 0 :(得分:1)
作为初学者,您应该查看https://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial。
例如
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
if overlay is MKPolyline {
let lineView = MKPolylineRenderer(overlay: overlay)
lineView.strokeColor = UIColor.redColor()
lineView.lineWidth = 1
return lineView
}
return nil
}
func addRoute() {
mapView.deselectAnnotation(selectedAnnotationView.annotation, animated: true)
let track = Track.GetAll()// to get list of coordinates you should write your own way to store
if track.count == 0 {
return
}
var pointsToUse: [CLLocationCoordinate2D] = []
var isTrackChanged = false
for i in 0...track.count-1 {
let x = CLLocationDegrees((track[i].Latitude as NSString).doubleValue)
let y = CLLocationDegrees((track[i].Longitude as NSString).doubleValue)
pointsToUse += [CLLocationCoordinate2DMake(x, y)]
if i > 0 {
if pointsToUse[i-1].latitude != pointsToUse[i].latitude || pointsToUse[i-1].longitude != pointsToUse[i].longitude {
isTrackChanged = true
}
}
}
let myPolyline = MKGeodesicPolyline(coordinates: &pointsToUse, count: track.count)
mapView.addOverlay(myPolyline)
}
//模型
class Track{
var latitude =""
var longitude=""
}