简体   繁体   English

如何使用mapView和CLLocationManager在iOS Swift中的两个位置之间绘制方向

[英]How to plot Directions between two locations in iOS Swift using mapView and CLLocationManager

I am using mapView.showUserLocation = true to plot user location and display it as an Annotation on my MKMapView object. 我使用mapView.showUserLocation = true来绘制用户位置并将其显示为我的MKMapView对象上的Annotation。

Now, when I have obtained the users' location, I want to plot a path from that set of coords to my destination. 现在,当我获得用户的位置时,我想绘制从该组坐标到目的地的路径。 I tried using MKDirections for this 我尝试使用MKDirections

Here is how I was trying to do it: 这是我试图这样做的方式:

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    mapView.showsUserLocation = true
    print("test")
    //Setting Up Source Location
    let sourceLocation = self.mapView.userLocation.coordinate
    let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let sourceAnnotation = MKPointAnnotation()
    sourceAnnotation.title = "You are Here"

    if let location = sourcePlacemark.location {
        sourceAnnotation.coordinate = location.coordinate
    }

    //Setting Up Destination Location
    let destinationLocation = CLLocationCoordinate2D(latitude: 28.6621292, longitude: 77.30198310000003)
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
    let destinationAnnotation = MKPointAnnotation()
    destinationAnnotation.title = "Conclave is Here"

    if let location = destinationPlacemark.location {
        destinationAnnotation.coordinate = location.coordinate
    }


    self.mapView.removeAnnotations(self.mapView.annotations)
    self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)


    //Plotting a course from Source to Destination
    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .Automobile

    // Calculate the direction
    let directions = MKDirections(request: directionRequest)

    // 8.
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void in

        guard let response = response else {
            if let error = error {
                //print("Error: \(error)")
            }

            return
        }

        let route = response.routes[0]
        self.mapView.addOverlay((route.polyline), level: MKOverlayLevel.AboveRoads)

        let rect = route.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
    }

}

However, when I tried debugging it, I get the following error: Error: Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x1742f7600 {NSLocalizedFailureReason=A route could not be determined between these locations., MKErrorGEOError=-403, MKDirectionsErrorCode=1, NSLocalizedDescription=Directions Not Available} 但是,当我尝试调试它时,我收到以下错误: Error: Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x1742f7600 {NSLocalizedFailureReason=A route could not be determined between these locations., MKErrorGEOError=-403, MKDirectionsErrorCode=1, NSLocalizedDescription=Directions Not Available}

I did some research and found out that MKDirections in not available in India , so I was wondering how to can get around showing Navigation Directions to the user. 我做了一些研究,发现MKDirections印度没有,所以我想知道如何向用户展示导航方向。

Thanks for the help! 谢谢您的帮助!

Better to use google api to draw polyline from source & destination. 最好使用谷歌api从源和目的地绘制折线。 You can use the api 你可以使用api

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=%@&destinations=%@&key= https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=%@&destinations=%@&key=

Then you get the json data of lat long(s) from where path can be drawn. 然后你从路径可以绘制的lat long获得json数据。 Decode the json data and draw polyline in MKMapkit 解码json数据并在MKMapkit中绘制折线

Error Domain=MKErrorDomain Code=5 "Directions Not Available"

You are most likely to get this error if the location doesn't belong to any of the countries in this list: http://www.apple.com/ios/feature-availability/#maps-directions 如果该位置不属于此列表中的任何国家/地区,则最有可能出现此错误: http//www.apple.com/ios/feature-availability/#maps-directions

While on iOS simulator, you can easily customize your current location. 在iOS模拟器上,您可以轻松自定义当前位置。 Two ways to do it: 两种方法:

iOS Simulator -> 'Debug' tab -> Location -> {Choose} iOS模拟器 - >'调试'选项卡 - >位置 - > {选择}

Xcode -> 'Debug' tab -> Simulate Location -> {Choose} Xcode - >'Debug'选项卡 - >模拟位置 - > {Choose}

So i suggest you to Use this. 所以我建议你使用它。 GoogleMaps SDK and draw route. GoogleMaps SDK和绘制路线。

https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中以文本形式获取两个位置之间的路线 - How to get directions between two locations in the form of text in iOS Swift iOS,CLLocationManager didUpdateLocations记录未访问的位置 - Swift iOS, CLLocationManager didUpdateLocations recording unvisited locations 如何在谷歌地图iOS swift中绘制两个位置之间的路线 - How to draw routes between two locations in google maps iOS swift 如何使用Swift iOS在Mapview中添加两个注释? - How can I add two annotations in mapview using swift ios? 如何在 Swift 中使用 MapKit 在两个位置之间绘制路线? - How to draw a route between two locations using MapKit in Swift? 如何使用谷歌计算两个位置之间的距离 api in swift 4 - How to calculate distance between two locations using google api in swift 4 如何在iOS中获取两个地址之间的路线 - How to get directions between two addresses in iOS iOS Mapview使用Word搜索位置 - iOS mapview search locations using word 如何在iOS中的两个位置之间获取路线? - How to get route between two locations in iOS? 是否可以在Swift中使用CLLocationManager获取位置服务的“重要位置”? - Is it possible to get Location Service's “Significant Locations” using CLLocationManager in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM