Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v3 Create routes between two points

I'm using the Google Maps API to develop a web application. I am trying to create a route between two points but for some reason I haven't figured out how create it. Below is my code, please let me know if there is something I am missing. Thank you.

<script>
var Center=new google.maps.LatLng(18.210885,-67.140884);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
var properties = {
    center:Center,
    zoom:20,
    mapTypeId:google.maps.MapTypeId.SATELLITE
};

map=new google.maps.Map(document.getElementById("map"), properties);
directionsDisplay.setMap(map);

var marker=new google.maps.Marker({
position:Center,
animation:google.maps.Animation.BOUNCE,
});

marker.setMap(map);

}

function Route() {

var start = new google.maps.LatLng(18.210885,-67.140884);
var end =new google.maps.latLng(18.211685,-67.141684);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.WALKING
 };
 directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay.setDirections(result);
}
});
} 

google.maps.event.addDomListener(window,'load',initialize);
</script>
like image 602
user1661865 Avatar asked Jan 28 '13 06:01

user1661865


1 Answers

It works for me if I:

  1. call the Route function
  2. change:

    var end =new google.maps.latLng(18.211685,-67.141684);
    

to:

    var end =new google.maps.LatLng(18.211685,-67.141684);

(javascript is case sensitive, the browser reported the error in the javascript console)

working version

code snippet:

var Center = new google.maps.LatLng(18.210885, -67.140884);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var properties = {
    center: Center,
    zoom: 20,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  };

  map = new google.maps.Map(document.getElementById("map"), properties);
  directionsDisplay.setMap(map);

  var marker = new google.maps.Marker({
    position: Center,
    animation: google.maps.Animation.BOUNCE,
  });

  marker.setMap(map);
  Route();
}

function Route() {

  var start = new google.maps.LatLng(18.210885, -67.140884);
  var end = new google.maps.LatLng(18.211685, -67.141684);
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.WALKING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
    } else {
      alert("couldn't get directions:" + status);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
  margin: 0;
  padding: 0;
  height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map"></div>
like image 139
geocodezip Avatar answered Oct 04 '22 15:10

geocodezip