谷歌地图在 For 循环内调用不返回距离

发布于 2024-08-29 10:42:38 字数 1895 浏览 3 评论 0原文

我在 JavaScript 中的 for 循环中调用谷歌地图,因为我有多条路线需要根据距离单独计算成本。

除了仅返回其中一条路线的距离之外,一切都很好。

我有一种感觉,这与我在地图的 ajax 调用中声明项目的方式有关。有什么想法可能是下面代码中的问题吗?

for (var i = 1; i <= numJourneys; i++) {
                var mapContainer = 'directionsMap' + i;
                var directionContainer = $('#getDistance' + i);

                $.ajax({
                    async: false,
                    type: "POST",
                    url: "Journey/LoadWayPoints",
                    data: "{'args': '" + i + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != '[]') {
                            var map = new GMap2(document.getElementById(mapContainer));
                            var distance = directionContainer;
                            var wp = new Array();

                            //routes
                            var counter = 0;
                            $.each(content, function () {
                                wp[counter] = new GLatLng(this['Lat'], this['Long']);
                                counter = counter + 1;
                            });

                            map.clearOverlays();
                            map.setCenter(wp[0], 14);

                            // load directions
                            directions = new GDirections(map);
                            GEvent.addListener(directions, "load", function () {
                                alert(directions.getDistance());
                                //directionContainer.html(directions.getDistance().html);
                            });
                            directions.loadFromWaypoints(wp, { getSteps: true });
                        }
                    }
                });
            }

I am calling google maps within a for loop in my javascript as I have mulitple routes that need to be costed separately based on distances.

Everything works great except that the distance is only returned for one of the routes.

I have a feeling that it is something to do with the way I have the items declared within the ajax call for the maps. Any ideas what could be the issue from the code below?

for (var i = 1; i <= numJourneys; i++) {
                var mapContainer = 'directionsMap' + i;
                var directionContainer = $('#getDistance' + i);

                $.ajax({
                    async: false,
                    type: "POST",
                    url: "Journey/LoadWayPoints",
                    data: "{'args': '" + i + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != '[]') {
                            var map = new GMap2(document.getElementById(mapContainer));
                            var distance = directionContainer;
                            var wp = new Array();

                            //routes
                            var counter = 0;
                            $.each(content, function () {
                                wp[counter] = new GLatLng(this['Lat'], this['Long']);
                                counter = counter + 1;
                            });

                            map.clearOverlays();
                            map.setCenter(wp[0], 14);

                            // load directions
                            directions = new GDirections(map);
                            GEvent.addListener(directions, "load", function () {
                                alert(directions.getDistance());
                                //directionContainer.html(directions.getDistance().html);
                            });
                            directions.loadFromWaypoints(wp, { getSteps: true });
                        }
                    }
                });
            }

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

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

发布评论

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

评论(1

别想她 2024-09-05 10:42:38

问题归结于未声明的变量。在 GEvent 调用之前有一个名为“directions”的变量,但实际上从未使用 var 声明过该变量,因此它没有被清除。

var directions = new GDirections(map);

执行上述操作对我有用。

The issue was down to a non declared variable. Just before the GEvent call there is a variable called 'directions' but this was never actually declared with a var so it wasn't being cleared out.

var directions = new GDirections(map);

Doing the above worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文