Google Maps Api JSON无法读取html_instructions [英] Google Maps Api JSON can't read html_instructions

查看:37
本文介绍了Google Maps Api JSON无法读取html_instructions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从Google Maps Api json响应中读取html_instructions时遇到问题,我怀疑这很大程度上是因为我对Json对象的处理不熟悉,但是我无法通过缝隙找到对我有帮助的答案,所以在这里我们去.

I'm having trouble reading the html_instructions from the Google Maps Api json response, I suspect it's largely my lack of familiarity with dealing with Json objects but I can't seam to find the answer that would help me with this so here we go.

按照Google路线服务示例,我在javascript中创建了calcRoute函数,该函数发布了开始和结束位置,并接收了json格式的驾驶说明列表,它在地图上绘制了所有路线.但是,当我要列出驾驶说明时,我可以生成一个步骤列表,其中包含每个步骤的距离和持续时间,但是对于html_instructions总是返回"undefined".当我查看收到的json时,每个步骤都有一个带有文本的html_instructions属性,但是我无法在代码中访问它.很简单,是的,所以我想,所以我想念什么白痴?

Following the Google directions service example I created a calcRoute function in javascript that posts a start and end location and receives a list of driving instructions in json format, it draws the route on the map all just fine. However when I want to list the driving instructions I can produce a list of steps containing distance and duration for each step but always return 'undefined' for the html_instructions. When I look at the json received each step has a html_instructions attribute with text, but i can't access it in code. Simple yes, so I thought, so what idiot thing am I missing?

function calcRoute() {    
    var start = document.getElementById('start').value;
    try {
        var request = {
            origin: start,
            destination: bowenSense,           
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.IMPERIAL            
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                parseJson(response);
                directionsDisplay.setDirections(response);                
            }
            else {
                alert("Error: " + status);
            }
        });
    }
    catch (error) {
        alert("Error: " + error);
    };
}
function parseJson(response) {
    for (i = 0; i < response.routes.length; i++) {
        var route = response.routes[i];
        for (j = 0; j < route.legs.length; j++) {
            var leg = route.legs[j];
            // Do something with leg attributes...
            for (k = 0; k < leg.steps.length; k++) {
                var step = leg.steps[k];
                var distance = step.distance.text;    // fine
                var duration = step.duration.text;    // fine
                var instruction = step.html_instructions; // == undefined ?
                alert("instruction: " + instruction); 
                var mode = step.travel_mode;          // works
            }
        }        
    }
}

返回示例JSON,显示填充的html_instructions值

Example JSON returned, showing populated html_instructions value

....
"steps" : [
              {
                 "distance" : {
                    "text" : "0.4 mi",
                    "value" : 695
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 61
                 },
                 "end_location" : {
                    "lat" : 53.3590723,
                    "lng" : -1.5065088
                 },
                 "html_instructions" : "Head \u003cb\u003ewest\u003c/b\u003e on \u003cb\u003eBannerdale Rd\u003c/b\u003e toward \u003cb\u003eNeedham Way\u003c/b\u003e",
                 "polyline" : {
                    "points" : "i~sdIjscHUvAi@vD[vAk@fC}@hDMf@[v@c@vAiA|Dy@dCuAzD[r@EJGLMXOTINQRUTYPSNOJQJSPOR"
                 },
                 "start_location" : {
                    "lat" : 53.3554115,
                    "lng" : -1.4983018
                 },
                 "travel_mode" : "DRIVING"
              },
              ....

令人困惑的是html_instruction和travel_mode都是字符串值,一个起作用而另一个不起作用,所以这是字符串内容的问题吗?html_instructions字符串有很多'\ u * * '(unicode?)编码的字符,而travel_mode是普通的旧大写字母吗?

The puzzling thing is that both html_instruction and travel_mode are string values, one works the other doesn't, so is this an issue with the contents of the string? The html_instructions string has a lot of '\u**' (unicode?) encoded characters, wheras travel_mode is a plain old set of capitals?

任何帮助,将不胜感激,并在此先感谢.ps如果需要的话,我正在使用最新版本的firefox浏览器.

Any help with this would be greatly appreciated, and thanks in advance. ps I'm using most recent version of firefox browser if that matters.

推荐答案

当您使用

It seems when you use URL to get direction JSON, you get an html_instructions as a property of step, but using JS API's you get instructions as property of step.

这篇关于Google Maps Api JSON无法读取html_instructions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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