HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在Swift中显示地图上的路线大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_607_0@
@H_607_0@
我试图在Apple地图上绘制两点之间的路线( Swift@L_944_2@).
以下结构用于存储坐标

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{$0.coordinatE}

然后我在地图上绘制路线

self.polyline = MKPolyline(coordinates: &coordinates,count: coordinates.count)
        self.mapView.addOverlay(self.polyline,level: MKOverlayLevel.Aboveroads)

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

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
}

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

解决方法

您实际上必须使用calculateDirectionsWithCompletionHandler从Apple的地图服务器获取路线.

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

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)
            }
        }
        })

(重复目的地.)

然后@L_634_9@mKRoute,例如:

let request:MKDirectionsrequest = MKDirectionsrequest()

// source and desTination are the relevant MKMapItems
request.setsource(sourcE)
request.setDesTination(desTination)

// Specify the transportation type
request.transportType = MKDirectionsTransportType.Automobile;

// If you're open to getTing more than one route,// requestsAlternateRoutes = true; else requestsAlternateRoutes = false;
request.requestsAlternateRoutes = true

let 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后,您可以将折线添加到地图中,如下所示:

@H_317_8@mapView.addOverlay(route.polyline,level: MKOverlayLevel.Aboveroads)

@H_607_0@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:iosswift地图显示路线
猜你在找的HTML5相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap