google maps Uncaught TypeError: Cannot read property 'start_location' of undefined in google maps

℡╲_俬逩灬. 提交于 2019-12-25 08:36:50

问题


Hi I have this simple function in JS for google maps and I get uncaught typeerror

directionResult is google directions response object which is passed by other function to this function.

                var myRoute = directionResult.routes[0].legs[0];
                var warnings = document.getElementById("warnings_panel");

                for (var i=0;i<3;i++)
                {
                        warnings.innerHTML += "<br/><br/>start lat = " + myRoute.steps[i].start_location.lat() + 
                                                                    "start lng = " + myRoute.steps[i].start_location.lng() + "<br />";
                        warnings.innerHTML += "end lat = " + myRoute.steps[i].end_location.lat() + 
                                                                    "end lng = " + myRoute.steps[i].end_location.lng() + "<br /> + Path :";     

                                    for(var path=0;path<myRoute.steps[i].path.length;path++)
                                            warnings.innerHTML += myRoute.steps[i].path[path];                              



                }//

回答1:


Hard to tell without more information but I would check that the myRoute.steps array contains at least three elements.




回答2:


The error is the length of myRoute.steps [myRoute.steps.length]

var myRoute = directionResult.routes[0].legs[0];
var warnings = document.getElementById("warnings_panel");
for (var i=0;i<myRoute.steps.length;i++)
{
    warnings.innerHTML += "<br/><br/>start lat = " + myRoute.steps[i].start_location.lat() + "start lng = " + myRoute.steps[i].start_location.lng() + "<br />";
    warnings.innerHTML += "end lat = " + myRoute.steps[i].end_location.lat() + "end lng = " + myRoute.steps[i].end_location.lng() + "<br /> + Path :";
    for(var path=0;path<myRoute.steps[i].path.length;path++)
        warnings.innerHTML += myRoute.steps[i].path[path];
}


来源:https://stackoverflow.com/questions/5968035/google-maps-uncaught-typeerror-cannot-read-property-start-location-of-undefin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!