xcode 如何获取地图上两点之间的路线方向以绘制行车路线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1566944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:54:15  来源:igfitidea点击:

how to get Route directions between two points on a map to draw driving directions?

xcodeiphone-sdk-3.0mapkitlocationmap-directions

提问by Rahul Vyas

is there a way to use core location or google or some other api to provide two points on the map and get the resulting set of latitude/longitude pairs for the route?

有没有办法使用核心位置或谷歌或其他一些api在地图上提供两个点并获得路线的纬度/经度对的结果集?

i have seen in this tutorial Drawing polyines or routes on a MKMapViewthat how to draw driving direction using core graphics...but in the sample code it has route.csv which has predefined set of longitudes and latitudes.....how do i make pair of longitudes and latitudes so that i can draw driving directions.

我在本教程中看到在MKMapView 上绘制多段线或路线,如何使用核心图形绘制行驶方向……但在示例代码中,它有 route.csv,它具有预定义的一组经度和纬度……怎么办我制作了一对经度和纬度,以便我可以绘制行车路线。

回答by iKenndac

Companies invest millions of dollars into developing routing algorithms, so I very much doubt that you'll be able to get it for free anywhere (I'm happy to be wrong, though). For example, the Google Maps JavaScript API supports routing, so I guess you could hack that up. The results wouldn't be fast orpretty, mind you.

公司在开发路由算法上投入了数百万美元,所以我非常怀疑您是否能够在任何地方免费获得它(不过我很高兴错了)。例如,Google Maps JavaScript API 支持路由,所以我想你可以破解它。请注意,结果不会很快很漂亮。

回答by Cyril Godefroy

If you have a limited use of the directions request (less than a certain amount per day) and always display results on a Google map, you do qualify for the free Google Directions API.

如果您对路线请求的使用有限(每天少于一定数量)并且始终在 Google 地图上显示结果,则您确实有资格使用免费的 Google Directions API。

https://developers.google.com/maps/documentation/directions/

https://developers.google.com/maps/documentation/directions/

Read the license part thoroughly to know the limits. You should easily be able to request a json response that will b easier to parse than an xml one (plus Apple added json parser in iOS).

仔细阅读许可部分以了解限制。您应该能够轻松请求一个 json 响应,该响应比 xml 更容易解析(加上 Apple 在 iOS 中添加的 json 解析器)。

For example

例如

http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false

http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|列克星敦,MA&sensor=false

If you are over the limit, you should look into their business licenses.

如果超过限额,您应该查看他们的营业执照。

回答by Codezy

Old question but MTDirectionsKit was recently open sourced and it works great:

老问题,但 MTDirectionsKit 最近开源,效果很好:

MTDirectionsKit

MT方向工具包

回答by Bryan J. Diaz

with Google APIyou can get directions from two points, the only problem is you just can use it once a day if you don't pay.

使用Google API,您可以从两点获取路线,唯一的问题是如果您不付费,则每天只能使用一次。

Search how to get the key API from google, there are a lots of videos on youtube talking about it.

搜索如何从 google 获取关键 API,youtube 上有很多视频在谈论它。

Furthemore, i got a library which hepls you to draw the line between points this onehttps://github.com/jd-alexander/Google-Directions-Android

此外,我有一个库可以帮助您在点之间划清界限https://github.com/jd-alexander/Google-Directions-Android

After you implemented it in build.gradle(Module:app), implement the libraryin the class like

在你实现它之后build.gradle(Module:app)library在类中实现像

public class LocationMapActivity extends AppCompatActivity implements RoutingListener

public class LocationMapActivity extends AppCompatActivity implements RoutingListener

will appear a error to implemets the methods (obligatoryto implement them but It's not necessary to use them).

将出现一个错误来实现这些方法(必须实现它们,但没有必要使用它们)。

I just did this:

我只是这样做:

 public void route(){

       Routing routing = new Routing.Builder()
                    .travelMode(/* Travel Mode */)
                    .withListener(/* Listener that delivers routing results.*/)
                    .waypoints(/*waypoints*/)
                    .key(/*api key for quota management*/)
                    .build();
        routing.execute();
    }

More info in the link.

链接中的更多信息。