显示谷歌方向Web服务的结果,而不使用JavaScript API [英] Displaying results of google direction web service without using javascript api

查看:131
本文介绍了显示谷歌方向Web服务的结果,而不使用JavaScript API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它将在2点之间找到方向,将其显示在谷歌地图上并将其存储在服务器端,以便在需要时再次渲染它,以便将一条路线与另一条路线进行比较。

我已成功使用 Google地图API web服务查找路线而且我还使用 Google地图javascript v3 查找路线并使用 DirectionsRenderer 将其显示在地图上。

这两种服务都会返回类似的JSON,并带有JSON属性名称的差异(请参见下面的示例1)。更重要的是JS api从一个调用返回不同的属性名称到lat / lng(见下面的示例2)。


所以问题是我无法使用从服务器端Web服务获得的JSON调用并直​​接将其传递给javascripts DirectionRenderer类以显示路由。此外,每当我在地图上显示路线时,我都必须拨打JS电话。这会导致我的配额不必要的使用。有没有更好的方法来做到这一点。我已经经历了以下问题,但我实际上无法得到答案 )和这里



示例1: b
$ b

Web服务调用结果

  {
routes:[
{
bounds:{
northeast:{
lat:12.9198217,
lng:77.6124895
},
southwest:{
lat:12.912811 ,
lng:77.60924989999999
}
},
.....

这没有路径属性。



Javascript V3调用结果:

  {
routes:[
{
bounds:{
ta:{
d:12.91281,
b:12.919820000000001
},
ga:{
b:77.60925,
d:77.61249000000001
}
},
.... ..
path:[
{
d:12.913920000000001,
e:77.60928000000001
},
{
d:12.913960000000001,
e:77.60958000000001
},
{
d:12.91398,
e:77.61
}
],
..........

示例2:
另一个Javascript V3调用结果:

 $ {
aa:{
k :12.91281,
j:12.919820000000001
},
qa:{
j:77.60925,
k:77.61249000000001
}
},
.....
路径:[
{
k:12.91281,
A:77.60925
},
{
k:12.913920000000001,
A:77.60928000000001
}
],
...... ...


解决方案

s,但有几个问题。

首先,请确保您的应用符合10.5.d这里的要求:
https://developers.google.com/maps/terms#10-license-restrictions



与来自Directions API Web服务的JSON响应不同,该服务始终命名字段,JavaScript方向服务的JSON响应不适合您进行分析。相反,您需要以包含 google.maps.LatLng 对象的JavaScript对象的形式访问 DirectionsResult 对象。 routes [0] .legs [0] .steps [0] .start_location



https://developers.google.com/maps/documentation/javascript/directions#DirectionsResults
https://developers.google.com/maps/documentation/javascript/directions #Steps



对于您从Directions API网络服务获取的路线,可以通过多种方式将其显示在JavaScript API上。我的首选是将JSON响应翻译为 DirectionsResult 对象。您可以在您的服务器或JavaScript中执行此操作,但我认为前者更好。

另外,您可以重新查询Directions服务(使用JavaScript服务)再次获得路线。这不仅会消耗额外的配额,还可能会返回一个不同的(更新的)路线。无论这是好还是坏,取决于你想要什么。


I am developing a application which will find direction between 2 points, display it on the google map and store it in server side so that I can render it again when needed also to compare one route to another route.
I have successfully used the google maps api web serivce to find the route and a I have also used the google maps javascript v3 to find the route and display it on the map using DirectionsRenderer.
Both of these services return similar JSON with the difference in the name of properties of JSON (see example 1 below). More importantly the JS api return different property names for lat/lng from one invocation to another (see example 2 below).

So the problem is I cannot use the JSON obtained from server side web service call and directly pass it on to the javascripts DirectionRenderer class to display the route. Moreover, I have to make a JS call everytime I have display the route on the map. This will cause unnecessary usage of my quota. Is there any better way to do this. I have gone through the following questions but I cannot actually get the answer here and here

example 1 :

Web serivce call result :

{
    "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 12.9198217,
               "lng" : 77.6124895
            },
            "southwest" : {
               "lat" : 12.912811,
               "lng" : 77.60924989999999
            }
         },
          .....

This does not have a "path" property.

Javascript V3 call result :

{
"routes": [
    {
        "bounds": {
            "ta": {
                "d": 12.91281,
                "b": 12.919820000000001
            },
            "ga": {
                "b": 77.60925,
                "d": 77.61249000000001
            }
        },
        ......
        "path": [
                            {
                                "d": 12.913920000000001,
                                "e": 77.60928000000001
                            },
                            {
                                "d": 12.913960000000001,
                                "e": 77.60958000000001
                            },
                            {
                                "d": 12.91398,
                                "e": 77.61
                            }
                        ],
                        ..........

example 2 : Another Javascript V3 call result :

{
"routes": [
    {
        "bounds": {
            "Aa": {
                "k": 12.91281,
                "j": 12.919820000000001
            },
            "qa": {
                "j": 77.60925,
                "k": 77.61249000000001
            }
        },
        .....
        "path": [
                            {
                                "k": 12.91281,
                                "A": 77.60925
                            },
                            {
                                "k": 12.913920000000001,
                                "A": 77.60928000000001
                            }
                        ],
                        .........

解决方案

You can do this, but there are several gotchas.

First, make sure your app is compliant with point 10.5.d here: https://developers.google.com/maps/terms#10-license-restrictions

Unlike the JSON response from Directions API web service, which has consistently named fields, the JSON response from the JavaScript Directions service is not for you to parse. Instead, you need to access the DirectionsResult object as a JavaScript object, which contains google.maps.LatLng objects, e.g. routes[0].legs[0].steps[0].start_location

https://developers.google.com/maps/documentation/javascript/directions#DirectionsResults https://developers.google.com/maps/documentation/javascript/directions#Steps

For a route you've gotten from the Directions API web service, there are several ways to display it on the JavaScript API. My preference would be to "translate" the JSON response into a DirectionsResult object. You can do this on your server or in JavaScript, but I think the former is better.

Alternatively, you can re-query the Directions service (using the JavaScript service) to get the route again. Not only will this consume additional quota, it may also return a different (more up-to-date) route. Whether that's good or bad, it depends on what you want.

这篇关于显示谷歌方向Web服务的结果,而不使用JavaScript API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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