1

I am using some helpful code I found at http://exploregooglemaps.blogspot.com.br/2012/02/measuring-distance-with-markers.html to add markers and measure distance.

I would like the markers to snap to road by default, but when the users holds the shift key a direct line is used.

I have managed to implement the if statement for the shift key, and a path is drawn when I hold the shift key, however no snap to road path is drawn by default.

I am calling two functions, one to draw a straight line drawPath and one to draw a snap to road line drawPathD

Any help would be much appreciated.

<code>
var map,
service = new google.maps.DirectionsService(),
shiftPressed = false,
poly;

var path = [];

shiftPressed = false;
var markers = [];
var routePoints = [];

google.maps.event.addDomListener(document, "keydown", function () {
shiftPressed = true;
});
google.maps.event.addDomListener(document, "keyup", function () {
shiftPressed = false;
});

function $(id) {
return document.getElementById(id);
}

var map;
var mapOptions = {
center: new google.maps.LatLng(-23.5489433, - 46.6388182),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var markers = [];
var line;

function initialize() {
map = new google.maps.Map($("map_canvas"), mapOptions);
line = new google.maps.Polyline({
map: map,
strokeColor: "#FF3333",
strokeOpacity: 0.5,
strokeWeight: 8
});

google.maps.event.addListener(map, 'click', function (event) {
var marker = new google.maps.Marker({
map: map,
position: event.latLng,
draggable: true
});


if (shiftPressed) {

markers.push(marker);
drawPath();

} else {

markers.push(marker);
drawPathD();

}


google.maps.event.addListener(marker, 'dblclick', function (event) {
marker.setMap(null);
drawPath();
});

google.maps.event.addListener(marker, 'drag', function (event) {
drawPath();
});
});
}

function countMarkers() {
count = 0;
for (var i = routePoints.length - 1; i >= 0; i--) {
if (markers[i].getMap() == null) {
routePoints.splice(i, 1);
} else {
count++;
}
}
return count;
}

function drawPath() {
countMarkers();

var coords = [];
for (var i = 0; i < markers.length; i++) {
coords.push(markers[i].getPosition());
}
line.setPath(coords);

meters = google.maps.geometry.spherical.computeLength(coords);
$("distKm").value = Math.round(meters / 1000 * 100) / 100;
$("distMi").value = Math.round(meters / 1609 * 100) / 100;
}

function drawPathD(event) {
countMarkers();
var coords = [];

service.route({
origin: path[path.length - 1],
destination: event.latLng,
travelMode: google.maps.DirectionsTravelMode.WALKING
}, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
path = path.concat(result.routes[0].overview_path);
routePoints.setPath(path);
}
});



meters = google.maps.geometry.spherical.computeLength(coords);
$("distKm").value = Math.round(meters / 1000 * 100) / 100;
$("distMi").value = Math.round(meters / 1609 * 100) / 100;
}

function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null)
}
drawPath();
}
google.maps.event.addDomListener(window, 'load', initialize);

</code>
londonfed
  • 1,170
  • 2
  • 12
  • 27

1 Answers1

0

Here's how to implement the "Snap to Road" functionality in Google Maps API v3: polyline snap to road using google maps api v3

Community
  • 1
  • 1
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57