如何从 Directions api 响应中获取行车路线?

发布于 2024-12-27 05:48:32 字数 3171 浏览 2 评论 0原文

我正在尝试在 Android 中的谷歌地图上绘制一条路线。 为此,我使用谷歌方向 api 来获取路线点 http ://code.google.com/intl/de-DE/apis/maps/documentation/directions/

现在我想知道如何获取方向箭头(例如:左转,...) 来自此 api 的 json 响应。

响应如下:

    {
 "routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 48.306950,
           "lng" : 14.304310
        },
        "southwest" : {
           "lat" : 48.163440,
           "lng" : 14.033580
        }
     },
     "copyrights" : "Map data ©2012 GeoBasis-DE/BKG (©2009), Google",
     "legs" : [
        {
           "distance" : {
              "text" : "34.6 km",
              "value" : 34554
           },
           "duration" : {
              "text" : "31 mins",
              "value" : 1866
           },
           "end_address" : "Linz, Austria",
           "end_location" : {
              "lat" : 48.306950,
              "lng" : 14.285860
           },
           "start_address" : "Wels, Austria",
           "start_location" : {
              "lat" : 48.16545000000001,
              "lng" : 14.036620
           },
           "steps" : [
              {
                 "distance" : {
                    "text" : "0.3 km",
                    "value" : 252
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 28
                 },
                 "end_location" : {
                    "lat" : 48.164110,
                    "lng" : 14.033890
                 },
                 "html_instructions" : "Head \u003cb\u003esouthwest\u003c/b\u003e on \u003cb\u003eHans-Sachs-Straße\u003c/b\u003e toward \u003cb\u003eGemeindestr.\u003c/b\u003e",
                 "polyline" : {
                    "points" : "ai~dH{ottAHHHNN`@jBjFzBxF"
                 },
                 "start_location" : {
                    "lat" : 48.16545000000001,
                    "lng" : 14.036620
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.1 km",
                    "value" : 149
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 48
                 },
                 "end_location" : {
                    "lat" : 48.163440,
                    "lng" : 14.03470
                 },
                 "html_instructions" : "At the roundabout, take the \u003cb\u003e3rd\u003c/b\u003e exit onto \u003cb\u003eMagazinstr./B1\u003c/b\u003e",
                 "polyline" : {
                    "points" : "u`~dHy~stAEBCDAHAH@H@HDHB@B@D?DABADGBG@G?GAIAGvAkCP_@"
                 },
                 "start_location" : {
                    "lat" : 48.164110,
                    "lng" : 14.033890
                 },
                 "travel_mode" : "DRIVING"
              },
    .
    .
    .

这应该是可能的,因为谷歌地图: http://maps.google.com/使用相同的方向 api(据我所知),并在路线描述的每个条目的左侧绘制这样的箭头(只需计算任何路线即可了解我的意思)。

我非常感谢各种帮助

I am trying to draw a route on google maps in android.
For that I use the google direction api to get the route points http://code.google.com/intl/de-DE/apis/maps/documentation/directions/

Now I would like to know how to get the directions arrow (like: turn left,...)
from the json response of this api.

A response look like:

    {
 "routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 48.306950,
           "lng" : 14.304310
        },
        "southwest" : {
           "lat" : 48.163440,
           "lng" : 14.033580
        }
     },
     "copyrights" : "Map data ©2012 GeoBasis-DE/BKG (©2009), Google",
     "legs" : [
        {
           "distance" : {
              "text" : "34.6 km",
              "value" : 34554
           },
           "duration" : {
              "text" : "31 mins",
              "value" : 1866
           },
           "end_address" : "Linz, Austria",
           "end_location" : {
              "lat" : 48.306950,
              "lng" : 14.285860
           },
           "start_address" : "Wels, Austria",
           "start_location" : {
              "lat" : 48.16545000000001,
              "lng" : 14.036620
           },
           "steps" : [
              {
                 "distance" : {
                    "text" : "0.3 km",
                    "value" : 252
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 28
                 },
                 "end_location" : {
                    "lat" : 48.164110,
                    "lng" : 14.033890
                 },
                 "html_instructions" : "Head \u003cb\u003esouthwest\u003c/b\u003e on \u003cb\u003eHans-Sachs-Straße\u003c/b\u003e toward \u003cb\u003eGemeindestr.\u003c/b\u003e",
                 "polyline" : {
                    "points" : "ai~dH{ottAHHHNN`@jBjFzBxF"
                 },
                 "start_location" : {
                    "lat" : 48.16545000000001,
                    "lng" : 14.036620
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.1 km",
                    "value" : 149
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 48
                 },
                 "end_location" : {
                    "lat" : 48.163440,
                    "lng" : 14.03470
                 },
                 "html_instructions" : "At the roundabout, take the \u003cb\u003e3rd\u003c/b\u003e exit onto \u003cb\u003eMagazinstr./B1\u003c/b\u003e",
                 "polyline" : {
                    "points" : "u`~dHy~stAEBCDAHAH@H@HDHB@B@D?DABADGBG@G?GAIAGvAkCP_@"
                 },
                 "start_location" : {
                    "lat" : 48.164110,
                    "lng" : 14.033890
                 },
                 "travel_mode" : "DRIVING"
              },
    .
    .
    .

This should be possible, because google maps: http://maps.google.com/ uses the same direction api (as far as i know) and it draws such arrows on the left side of every entry of the route description (just calculate any route to see what I mean).

I very appreciate every kind of help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2025-01-03 05:48:33

看看我的答案。这将为您提供另一种获取方向说明的方式
使用 KML 文件

Have a look at my answer. This will give you an other way get the direction instructions
using KML file

饮惑 2025-01-03 05:48:33

这就是我正在使用的,

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr="+latitude_cur+","+longitude_cur+"&daddr="+latitude+","+longitude));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER );     
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

This is what I am using,

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr="+latitude_cur+","+longitude_cur+"&daddr="+latitude+","+longitude));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER );     
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文