views:

1396

answers:

2

Hi forum.

Playing around with Google Maps these days, with some directions.

I have a map that gets the directions and address (reverse-geocoding) when dragging and dropping the markers.

If there is two nodes on the map (http://dev.korebogen.dk/gmap/) the script is working fine (click set directions) - but I need to add a click event so I can place those two markers instead of hard-code the location by hand, but still be able to drag them around - or place new with new click. But I only need A to B markers.

Ive been playing around with some click events, but I can not seem to accomplish what I am looking for - hope to see some help here. Thank you very much.

+3  A: 

This code will allow you to click and place two markers, which you can then use to load GDirections, and remove the original markers. Note that you must use this format for the query string: "from: marker@35,-25 to: marker@-20,15".

var markerArray = [];

var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
    var marker = new GMarker(latlng, { draggable: true });
    map.addOverlay(marker);
    markerArray.push(marker);
    if (markerArray.length > 1) {
     GEvent.removeListener(listener);
     var marker1 = markerArray[0];
     var marker2 = markerArray[1];

     gdir.load("from: marker1@" + marker1.getLatLng() + " to: marker2@" + marker2.getLatLng());

     map.removeOverlay(marker1);
     map.removeOverlay(marker2);
    }
});
Chris B
A: 

Hi,

I would like to do quite the same.I just need one marker for user to add because the other one is fixed in the center of the map. How can I implement this in Flex?

candylolipop