Google地图路线 - 在地图上的两个点之间路线指向

时间:2009-06-18 12:52:24

标签: javascript api google-maps

这看起来应该有用吗?我想要生成从一个纬度/经度到另一个纬度/经度的方向。

var dirMap = new GMap2($("#dirMap").get(0));
var wp = new Array(2);
wp[0] = new GLatLng(35.742149,139.337218);
wp[1] = new GLatLng(35.735347,139.328485);

var marker = new GMarker(wp[1]);
dirMap.addOverlay(marker);
dirMap.setCenter(wp[0], 12);
dirMap.setUIToDefault();

// load directions
directions = new GDirections(dirMap);
directions.load("from: Waypoint1@21.742149,100.337218 to: Waypoint2@15.740815,100.3267");

地图加载,但指示不会进入。我也是这样试过的:

var dirMap = new GMap2($("#dirMap").get(0));
var wp = new Array(2);
wp[0] = new GLatLng(32.742149,119.337218);
wp[1] = new GLatLng(32.735347,119.328485);

var marker = new GMarker(wp[1]);
dirMap.addOverlay(marker);
dirMap.setCenter(wp[0], 12);
dirMap.setUIToDefault();

// load directions
directions = new GDirections(dirMap);
directions.loadFromWaypoints(wp);

同样的事情......地图但没有方向。非常感谢任何帮助,谢谢你提前!

2 个答案:

答案 0 :(得分:1)

乍一看我的代码看不出任何明显的东西,所以我的第一个猜测是 GDirections 请求失败了(我还假设您已经检查了javascript错误日志对于任何错误,如果您还没有这样做,请使用工具/错误控制台。)

我建议你为你的GDirections对象添加一个错误处理程序,这会给你一些指示你的请求发生了什么:

GEvent.addListener(directions, "error", handleErrors);

并在handleErrors回调中查看:

directions.getStatus().code

Geo Status Codes比较。

编辑:好的,我刚试了your code这里works perfectly。我只能假设您的网页上存在导致问题的其他问题。您可以在问题中发布链接,以便我们查看吗?

答案 1 :(得分:1)

检查状态(604)我在Google Maps API Reference尝试时得到了:

  

GDirections对象不能   计算点之间的方向   在查询中提到。这是   通常是因为没有路线   两点之间可用,或者   因为我们没有数据   在该地区进行路由。

这是我使用的代码(稍加修改):

$(function ()
{
    if (GBrowserIsCompatible())
    {
        var wp = [new GLatLng(35.742149,139.337218), new GLatLng(35.735347,139.328485)];

        var map = new GMap2(document.getElementById('map-canvas'));
        map.setCenter(wp[0], 12);
        map.setUIToDefault();

        var marker = new GMarker(wp[1]);
        map.addOverlay(marker);

        var directions = new GDirections(map);
        GEvent.addListener(
            directions,
            'error',
            function ()
            {
                console.log(directions.getStatus().code);
            }
        );
        directions.load('from: Waypoint1@21.742149,100.337218 to: Waypoint2@15.740815,100.3267');
    }
});
相关问题