Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我试图在Apple地图上绘制两点之间的路线( Swift代码). 以下结构用于存储坐标 struct GeoLocation { var latitude: Double var longitude: Double func distanceBetween(other: GeoLocation) -> Double { let locationA = CL 我试图在Apple地图上绘制两点之间的路线( Swift代码).
以下结构用于存储坐标

struct Geolocation {    var latitude: Double    var longitude: Double    func distanceBetween(other: Geolocation) -> Double {        let locationA = CLLocation(latitude: self.latitude,longitude: self.longitude)        let locationB = CLLocation(latitude: other.latitude,longitude: other.longitude)        return locationA.distanceFromLocation(locationB)    }}self.foundLocations - is an array of these structures

在自定义类中,我接收地图上点的坐标.

var coordinates = self.foundLocations.map{
self.polyline = MKpolyline(coordinates: &coordinates,count: coordinates.count)        self.mapVIEw.addOverlay(self.polyline,level: MKOverlayLevel.AboveRoads)
.coordinate}

然后我在地图上绘制路线

func mapVIEw(mapVIEw: MKMapVIEw!,rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {    if let polylineOverlay = overlay as? MKpolyline {        let render = MKpolylineRenderer(polyline: polylineOverlay)        render.strokecolor = UIcolor.bluecolor()        return render    }    return nil}

要绘制路径,我使用MKMapVIEwDelegate中的以下方法

let geocoder = CLGeocoder()let location = CLLocation(latitude: sourceLatitude,longitude: sourceLongitude)geocoder.reverseGeocodeLocation(location,completionHandler: {        (placemarks:[AnyObject]?,error:NSError?) -> VoID in        if placemarks?.count > 0 {            if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {                self.source =  MKMAPItem(placemark: placemark)            }        }        })

而不是铺设在道路上的实际路线,我只得到两点之间的直线.
如何显示实际路线?

解决方法@H_404_37@ 您实际上必须使用calculateDirectionsWithCompletionHandler从Apple的地图服务器获取路线.

首先为源和目标创建相关的MKMAPItem,例如:

let request:MKDirectionsRequest = MKDirectionsRequest()// source and destination are the relevant MKMAPItemsrequest.setSource(source)request.setDestination(destination)// Specify the transportation typerequest.transportType = MKDirectionsTransportType.automobile;// If you're open to getting more than one route,// requestsAlternateRoutes = true; else requestsAlternateRoutes = false;request.requestsAlternateRoutes = truelet directions = MKDirections(request: request)directions.calculateDirectionsWithCompletionHandler ({    (response: MKDirectionsResponse?,error: NSError?) in    if error == nil {        self.directionsResponse = response        // Get whichever currentRoute you'd like,ex. 0        self.route = directionsResponse.routes[currentRoute] as MKRoute    }})

(重复目的地.)

然后获取MKRoute,例如:

mapVIEw.addOverlay(route.polyline,level: MKOverlayLevel.AboveRoads)

然后在检索MKRoute后,您可以将折线添加到地图中,如下所示:

[+++] 总结

以上是内存溢出为你收集整理的ios – 在Swift中显示地图上的路线全部内容,希望文章能够帮你解决ios – 在Swift中显示地图上的路线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
ios – 在Swift中显示地图上的路线_app_内存溢出

ios – 在Swift中显示地图上的路线

ios – 在Swift中显示地图上的路线,第1张

概述我试图在Apple地图上绘制两点之间的路线( Swift代码). 以下结构用于存储坐标 struct GeoLocation { var latitude: Double var longitude: Double func distanceBetween(other: GeoLocation) -> Double { let locationA = CL 我试图在Apple地图上绘制两点之间的路线( Swift代码).
以下结构用于存储坐标

struct Geolocation {    var latitude: Double    var longitude: Double    func distanceBetween(other: Geolocation) -> Double {        let locationA = CLLocation(latitude: self.latitude,longitude: self.longitude)        let locationB = CLLocation(latitude: other.latitude,longitude: other.longitude)        return locationA.distanceFromLocation(locationB)    }}self.foundLocations - is an array of these structures

在自定义类中,我接收地图上点的坐标.

var coordinates = self.foundLocations.map{
self.polyline = MKpolyline(coordinates: &coordinates,count: coordinates.count)        self.mapVIEw.addOverlay(self.polyline,level: MKOverlayLevel.AboveRoads)
.coordinate}

然后我在地图上绘制路线

func mapVIEw(mapVIEw: MKMapVIEw!,rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {    if let polylineOverlay = overlay as? MKpolyline {        let render = MKpolylineRenderer(polyline: polylineOverlay)        render.strokecolor = UIcolor.bluecolor()        return render    }    return nil}

要绘制路径,我使用MKMapVIEwDelegate中的以下方法

let geocoder = CLGeocoder()let location = CLLocation(latitude: sourceLatitude,longitude: sourceLongitude)geocoder.reverseGeocodeLocation(location,completionHandler: {        (placemarks:[AnyObject]?,error:NSError?) -> VoID in        if placemarks?.count > 0 {            if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {                self.source =  MKMAPItem(placemark: placemark)            }        }        })

而不是铺设在道路上的实际路线,我只得到两点之间的直线.
如何显示实际路线?

解决方法@H_404_37@ 您实际上必须使用calculateDirectionsWithCompletionHandler从Apple的地图服务器获取路线.

首先为源和目标创建相关的MKMAPItem,例如:

let request:MKDirectionsRequest = MKDirectionsRequest()// source and destination are the relevant MKMAPItemsrequest.setSource(source)request.setDestination(destination)// Specify the transportation typerequest.transportType = MKDirectionsTransportType.automobile;// If you're open to getting more than one route,// requestsAlternateRoutes = true; else requestsAlternateRoutes = false;request.requestsAlternateRoutes = truelet directions = MKDirections(request: request)directions.calculateDirectionsWithCompletionHandler ({    (response: MKDirectionsResponse?,error: NSError?) in    if error == nil {        self.directionsResponse = response        // Get whichever currentRoute you'd like,ex. 0        self.route = directionsResponse.routes[currentRoute] as MKRoute    }})

(重复目的地.)

然后获取MKRoute,例如:

mapVIEw.addOverlay(route.polyline,level: MKOverlayLevel.AboveRoads)

然后在检索MKRoute后,您可以将折线添加到地图中,如下所示:

总结

以上是内存溢出为你收集整理的ios – 在Swift中显示地图上的路线全部内容,希望文章能够帮你解决ios – 在Swift中显示地图上的路线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存