测量 MKMapView 中绘制路线的距离(以米为单位)

2024-01-17

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


我假设你正在使用MKDirectionsRequest得到一个MKDirectionsResponse您从中获取路线。例如:

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(在这种情况下self.directionsResponse)并根据该响应决定特定的路由索引,CLLocationDistance该路线的长度(以米为单位)可以使用以下方法找到:

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

如果您不知道要使用哪条具体路线 - 例如。如果您想根据距离决定路线 - 您可以通过directionsResponse.route带循环的数组来获取所有路线距离。

编辑:此外,如果您想查找时间距离(以秒为单位),您可以使用:

NSTimeInterval seconds = route.expectedTravelTime;

在斯威夫特中:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测量 MKMapView 中绘制路线的距离(以米为单位) 的相关文章

  • 由于语言错误,Itunes Connect 无法提交

    Thats all it shows https i stack imgur com 0aZm8 png 我不确定它没有告诉我出了什么问题 it shows its linked to the language https i stack
  • 这个错误是无效上下文0x0吗?

    我在ViewDidLoad中编写了以下代码 Implement viewDidLoad to do additional setup after loading the view typically from a nib void view
  • Swift 3:如何去除UITableView屏幕截图的灰色区域

    我在从桌面视图的屏幕截图中删除灰色区域时遇到了问题 这是我尝试截图时的输出UITableView 实际上我隐藏了一些tableViewCell这是不需要的 可能这就是区域显示灰色的原因 任何帮助表示赞赏 非常感谢 Answer 我通过使用
  • 暂停视频录制[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试创建一个应用程序 用户可以在其中从相机录制视频 该应用程序的功能之一必须是文件录制中的暂停 例如 用户通过按 开始 按钮开始
  • iOS中的performSelector有什么用

    的作用是什么执行选择器 比较 self btnClicked and self performSelector selector btnClicked void btnClicked NSLog Method Called 两者都对我来说工
  • Objective Flickr 照片上传错误

    我正在使用 ObjectiveFlickr 库将照片从我的 iPhone 应用程序上传到 Flickr 我可以授权该应用程序并执行一般请求 但在尝试上传照片时遇到错误 要上传的照片是使用 AVFoundation 捕获的图像 这是相关代码
  • 如何从代码隐藏中设置 CarouselView 的项目?

    我有一个 CarouselView 它绑定到图像的 ItemsSource 但我想通过更改 CarouselView 的索引来更改当前显示的图像 我尝试使用 CarouselView Position 作为必须选择的元素的索引 但不幸的是这
  • iOS UIButton 带有圆角和背景 bug

    我发现圆形 UIButton 存在一个奇怪的问题 这是我创建此按钮的代码块 let roundedButton UIButton type System roundedButton frame CGRectMake 100 100 100
  • UISearchController 保留问题

    我正在尝试使用 UISearchController 但是我遇到了无法解决的保留问题 MainTableview 有两个部分 第1节 基于某些正则表达式过滤数据 第2节 All Data 我将 UISearchController 添加到我
  • 混合静态和动态 UITableViewController 内容会导致 NSRangeException

    我一直在寻找这个错误 并找到了一些具有类似行为的帖子 但没有解决问题的解决方案 我有一个 UITableViewController 在 SB 中声明为静态 它具有以下部分 第 0 部分 配方 是静态的 有 4 个单元格 第 1 部分 口味
  • 我可以知道 requireGestureRecognizerToFail 到底会做什么吗?

    谁能告诉我下面的代码行到底会做什么 我已经提到过Apples https developer apple com library ios documentation uikit reference UIGestureRecognizer C
  • 应用程序传输安全已禁用,但仍然出现 SSL 握手错误

    我在通过 HTTPS SSL 连接到 API 时遇到问题 我已经使用下面的字典完全禁用了应用程序传输安全性 ATS 尽管 SSL 证书通过了 NSCURL 的所有测试
  • 我什么时候应该对 IBOutlet 使用弱或强限定符? [复制]

    这个问题在这里已经有答案了 可能的重复 ARC 下 IBOutlets 应该强还是弱 https stackoverflow com questions 7678469 should iboutlets be strong or weak
  • 将 CALayer 旋转 90 度?

    如何旋转CALayer90度 我需要旋转所有内容 包括子图层和坐标系 Obj C theLayer transform CATransform3DMakeRotation 90 0 180 0 M PI 0 0 0 0 1 0 Swift
  • 应用程序未通过协同设计验证?

    我在提交 iPhone 申请时遇到问题 我看到了一些类似的问题 但没有找到答案 当我存档项目并单击 验证 时 收到错误消息 应用程序未通过协同设计验证 签名无效 或者未使用 Apple 提交证书进行签名 我假设我在协同设计部分做错了什么 我
  • 我应该在哪个方法中设置 UITextField 的委托?

    在 viewDidLoad 或 init 方法中设置 UITextField 的委托是一个好习惯吗 我尝试在 init 方法中将委托设置为 self 但它没有调用相应的委托方法 当我将代码移动到 viewDidLoad 中时 它注册为将 s
  • NSPredicate 的 onFormat 字符串

    我想用 id 键对数据进行排序 我如何理解格式字符串的用途NSPredicate格式 我有一个100号的帖子 我的代码 let objectIDs posts map 0 id let predicate NSPredicate forma
  • 显示键盘时如何在 TextView 下方添加更多填充

    当我在 ScrollView 中有 TextField 并点击它时 键盘会按预期显示 但似乎 TextField 已向上移动到足以显示输入区域 但我希望移动到足够的位置 以便整体可见 否则它看起来像是被剪裁了的 我找不到改变这种行为的方法
  • 在 UIMenuItem 上设置accessibilityLabel

    我正在尝试设置accessibilityLabel of a UIMenuItem而且似乎没有效果 无论如何 VoiceOver 只是读取项目的标题 let foo UIMenuItem title foo action selector
  • ResponseSerializer“无法使用 Swift 3 调用非函数类型“NSHTTPURLResponse”的值?

    我一直在使用以下代码 没有出现任何问题 直到更新到 Xcode 8 beta 6 它类似于这个例子 https github com Alamofire Alamofire generic response object serializa

随机推荐