Thursday, 15 April 2010

Google Maps API directions recalculate on drag -



Google Maps API directions recalculate on drag -

thank time.

brief:

google maps api v3 project click on map place markers directions service calculates route between 2 sequential points

goal:

recalculate route when 1 of markers dragged.

progress:

so far have assembled page: http://test.bluecactus.ro/gmaps.html. have commented of code.

problem:

page works ok first drag. when drag sec marker or drag first 1 again, polylines no longer 'regenerated' way should (delete incoming , outgoing, calculate new position, plot on map).

for multiple drags on same marker polylines should deleted left on map. on dragging sec marker, wrong polylines deleted.

this lead me believe wrong identifying of dragged marker's position in array holds of markers; function below. have redone 'math' several times - on paper seems ok. have tried putting outside for loop , using loop identify position in global variable. none of them worked.

http://test.bluecactus.ro/gmaps.html

google.maps.event.addlistener(marker, 'dragend', function(event) { //ondrag (var = 0; < gmarkers.length; i++) { if(gmarkers[i].getposition() == marker.getposition()) { //find out array position of current marker console.log("i = " + i); //hide incoming , outgoing polylines; polylines index offset -1 (n markers => n-1 polylines) gpolys[i-1].setmap(null); gpolys[i].setmap(null); drawpath(gmarkers[i-1].getposition(), marker.getposition()); //calculate route between moved marker , previous point drawpath(marker.getposition(), gmarkers[i+1].getposition()); //calculate route between moved marker , next point //overwrite initial polylines; drawpath function automatically adds new polylines end of gpolys array gpolys[i-1] = gpolys[gpolys.length-2]; console.log(gpolys); gpolys[i] = gpolys[gpolys.length-1]; console.log(gpolys); //delete lastly 2 elements gpolys gpolys.pop(); console.log(gpolys); gpolys.pop(); console.log(gpolys); } } });

notes:

first , lastly marker not work, not dealt moment. i have found of workaround: on drag recalculate whole route - take markers in array , plot between & i+1; works smaller routes , without dragging fast - otherwise over_query_limit hit. resolved delay between calls, seems odd recalculate everytime 1 point.

i sense i'm close solution, can't grasp it. suggestions appreciated. give thanks you.

google-maps-api-3 map-directions

No comments:

Post a Comment