从方向API结果启动具有特定方向叠加的Android Map意图

时间:2015-12-05 23:26:53

标签: android google-maps android-intent

我正在制作导航应用程序,我从Googles Directions API返回了一堆路由(设置替代为true),然后用户可以选择。我想要做的是使用选定的路线启动Google地图的地图意图。

我想知道这是否可行?

我已经看过传递saddr和daddr的示例,它会自动显示最佳路线,但我希望它显示用户从其中一个方向结果中选择的那个最好的(最优的)。

由于

1 个答案:

答案 0 :(得分:0)

在阅读google方向api之后,我在其响应类型(https://developers.google.com/maps/documentation/directions/intro#sample-response)中看到了这一点,我们确定了每个步骤,纬度和经度。

假设您的输出路线看起来像(这是虚拟的,而不是原始的):

                 {
                  ...
                  "routes": [ {
                  "summary": "I-40 W",

                  // There are many legs, but say user choses this below first leg.
                  "legs": [ {
                  "steps": [ 

                  {
                    "travel_mode": "DRIVING",
                    "start_location": {
                    "lat": 41.8507300,
                    "lng": -87.6512600
                    },
                    "end_location": {
                    "lat": 41.8525800,
                    "lng": -87.6514100
                    },
                    "polyline": {
                    "points": "a~l~Fjk~uOwHJy@P"
                    },
                    "duration": {
                    "value": 19,
                    "text": "1 min"
                    },
                    "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
                    "distance": {
                    "value": 207,
                    "text": "0.1 mi"
                    }
                 },

                 {
                    "travel_mode": "DRIVING",
                    "start_location": {
                    "lat": 41.8107300,
                    "lng": -87.6012600
                    },
                    "end_location": {
                    "lat": 41.8725800,
                    "lng": -87.6414100
                    },
                    "polyline": {
                    "points": "a~l~Fjk~uOwHJy@P"
                    },
                    "duration": {
                    "value": 19,
                    "text": "1 min"
                    },
                    "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
                    "distance": {
                    "value": 207,
                    "text": "0.1 mi"
                    }
                 }

                 "start_address": "Oklahoma City, OK, USA",
                 "end_address": "Los Angeles, CA, USA"
                 } ],

                 .... // many other legs ....
                 "legs":[ { ..... } ]
                 .
                 .
                 "copyrights": "Map data ©2010 Google, Sanborn",
                 ...
                 ..
                 .
                 } ] 
                 }
                 ]
                 }

现在,上面的每一步,我们都有lat和lng条目。我们将使用它们来创建我们的意图请求。

我们的要求将成为:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("https://maps.google.ch/maps?saddr=Oklahoma City, OK, USA&daddr=Los Angeles, CA, USA to:41.8525800,-87.6514100 to: 41.8107300,-87.6012600"));
startActivity(intent);

请注意,我在网址中使用了lat和lng以及to:前缀的步骤。使用此网址中最多只能使用两个to:前缀,因为谷歌地图应用并不会确认所有路点,只会在作为意图传递时确认第一个和最后一个路径。

把它概括为:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("https://maps.google.ch/maps?saddr=[address1]&daddr=[address2] to:[address3] to: [address4]"));
startActivity(intent);

因此,这将打开谷歌地图应用程序,其导航标记为[address1]到[address2],通过[address3]和[address4]。

希望这能解决问题。