Swift 为值数组画一条路线
Swift Draw a route for an array of value
您好,我有一个像这样的数组 route = [[latitude,longitude],[latitude,longitude]...]。
我应该画一条遵循数组坐标的线。我试过这个功能:
func addPolylineToMap(locations: [CLLocation]){
var coordinates = locations.map({ (location: CLLocation!) -> CLLocationCoordinate2D in
return location.coordinate})
var countLocations = locations.count
let geodesic = MKGeodesicPolyline(coordinates: &coordinates, count: locations.count)
mapView.addOverlay(geodesic)
}
并调用给它数组路由的函数:
addPolylineToMap(locations: route as! [CLLocation])
但编译器给我错误 ambiguous use of 'init(coordinates:count:)'
无论如何,我不太确定它是否可行。
有人可以向我解释如何做得更好吗?
P.S.
路线
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
manager.startUpdatingLocation()
var userLatitude = (userLocation.coordinate.latitude)
var userLongitude = (userLocation.coordinate.longitude)
userMoves = [(userLatitude),(userLongitude)]
route 是附加用户移动而创建的数组
您可以删除那个 &
。在 Swift 3:
func addPolylineToMap(locations: [CLLocation]) {
let coordinates = locations.map { [=10=].coordinate }
let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: coordinates.count)
mapView.add(geodesic)
}
或在Swift 2:
func addPolylineToMap(locations: [CLLocation]) {
let coordinates = locations.map { [=11=].coordinate }
let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(geodesic)
}
您好,我有一个像这样的数组 route = [[latitude,longitude],[latitude,longitude]...]。 我应该画一条遵循数组坐标的线。我试过这个功能:
func addPolylineToMap(locations: [CLLocation]){
var coordinates = locations.map({ (location: CLLocation!) -> CLLocationCoordinate2D in
return location.coordinate})
var countLocations = locations.count
let geodesic = MKGeodesicPolyline(coordinates: &coordinates, count: locations.count)
mapView.addOverlay(geodesic)
}
并调用给它数组路由的函数:
addPolylineToMap(locations: route as! [CLLocation])
但编译器给我错误 ambiguous use of 'init(coordinates:count:)'
无论如何,我不太确定它是否可行。 有人可以向我解释如何做得更好吗?
P.S.
路线
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
manager.startUpdatingLocation()
var userLatitude = (userLocation.coordinate.latitude)
var userLongitude = (userLocation.coordinate.longitude)
userMoves = [(userLatitude),(userLongitude)]
route 是附加用户移动而创建的数组
您可以删除那个 &
。在 Swift 3:
func addPolylineToMap(locations: [CLLocation]) {
let coordinates = locations.map { [=10=].coordinate }
let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: coordinates.count)
mapView.add(geodesic)
}
或在Swift 2:
func addPolylineToMap(locations: [CLLocation]) {
let coordinates = locations.map { [=11=].coordinate }
let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(geodesic)
}