views:

5866

answers:

2

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 come 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!

+1  A: 

I can't see anything obvious at first glance at your code, so my first guess is a failure coming 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 ?

Cannonade
The problem was on my testing box... so no link. After you tested it out, I copied out the code to a fresh html file and successfully got it to load.At least I know it's not the javascript's fault now. I truly appreciate the help on this, thank you very much.
Chad
Glad to hear it :).
Cannonade
A: 

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

The GDirections object could not compute 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');
    }
});
anddoutoi
There is no problem with the code he provided. I used it verbatim and a correct route was calculated.
Cannonade