最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps GDirections - Route directions between two points on a map - Stack Overflow

programmeradmin2浏览0评论

Does this look like it should work? I'm wanting to generate directions from one latitude/longitude to another latitude/longitude.

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: [email protected],100.337218 to: [email protected],100.3267");

The map loads fine, but the directions don't e in. I've tried it this way too:

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);

Same thing... map but no directions. Any help is greatly appreciated, thank you in advance!

Does this look like it should work? I'm wanting to generate directions from one latitude/longitude to another latitude/longitude.

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: [email protected],100.337218 to: [email protected],100.3267");

The map loads fine, but the directions don't e in. I've tried it this way too:

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);

Same thing... map but no directions. Any help is greatly appreciated, thank you in advance!

Share Improve this question asked Jun 18, 2009 at 12:52 ChaddeusChaddeus 13.4k30 gold badges107 silver badges166 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

I can't see anything obvious at first glance at your code, so my first guess is a failure ing back in for the GDirections request (I am also assuming you have checked the javascript error log for any errors, Tools/Error Console if you haven't already done this).

I suggest you add an error handler for your GDirections object, this will give you some indication what is happening with your request:

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

and in the handleErrors callback have a look in:

directions.getStatus().code

Compare with the Geo Status Codes.

EDIT: Ok, I just tried out your code here and it works perfectly. I can only assume that there is some other problem on your page that is causing the issue. Can you post a link in the question so we can check it out ?

Checking the status (604) I got when I tried in the Google Maps API Reference says:

The GDirections object could not pute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.

and this is the code I used (slightly modified):

$(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: [email protected],100.337218 to: [email protected],100.3267');
    }
});
发布评论

评论列表(0)

  1. 暂无评论