在MKMapView中测量距离绘制路线的距离(以米为单位) [英] Measuring distance in meters from a drawn route in MKMapView

查看:214
本文介绍了在MKMapView中测量距离绘制路线的距离(以米为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算MKMapView中两个坐标之间的路线距离?我不是要求直线距离,而是要求转弯路线的距离。

How can I calculate the route distance between two coordinates in MKMapView? I'm not asking for the straight line distance but the distance for a route with turns.

推荐答案

我假设你'重新使用 MKDirectionsRequest 来获取 MKDirectionsResponse ,从中获取路线。例如:

I'm assuming you're using an MKDirectionsRequest to get a MKDirectionsResponse from which you're getting your route. For example:

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

// source and destination are the relevant MKMapItem's
request.source = source;
request.destination = destination;

// Specify the transportation type
request.transportType = MKDirectionsTransportTypeAutomobile;

// If you're open to getting more than one route, requestsAlternateRoutes = YES; else requestsAlternateRoutes = NO;
request.requestsAlternateRoutes = YES;

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

    if (!error) {
        self.directionsResponse = response;                           
    }
}];

获得 MKDirectionsResponse (在此case self.directionsResponse )并决定该响应的特定路由索引,该路由的 CLLocationDistance (以米)可以使用:

Once you get the MKDirectionsResponse (in this case self.directionsResponse) and decide on a specific route index from that response, the CLLocationDistance of that route (measured in meters) can be found using:

MKRoute *route = self.directionsResponse.routes[currentRoute];
CLLocationDistance distance = route.distance;

如果您不知道要使用哪条特定路线 - 例如如果你想根据距离决定路线 - 你可以通过一个循环来查看 directionsResponse.route 数组,以获得所有的路线距离。

And if you don't know which specific route you want to use -- ex. if you want to decide on a route based on the distance -- you can go through the directionsResponse.route array with a loop to get all the route distances.

编辑:此外,如果您想要找到时间距离(以秒为单位),您可以使用以下方式完成:

Furthermore, if you want to find the distance in time (measured in seconds), you can do so using:

NSTimeInterval seconds = route.expectedTravelTime;






在Swift中:


And in Swift:

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

获取距离:

let route = directionsResponse.routes[currentRoute] as MKRoute
let distance = route.distance

要获得预计旅行时间:

let seconds = route.expectedTravelTime

这篇关于在MKMapView中测量距离绘制路线的距离(以米为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆