Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API V3 : How show the direction from a point A to point B (Blue line)?

I have latitude and longitude for 2 points on database, I want my Google Map to display a route from point A to point B...

Just like we see here (Google Maps Directions)

Image from the link

How to draw that direction line on map ?

like image 301
Yugal Jindle Avatar asked May 11 '11 05:05

Yugal Jindle


People also ask

How do you change the color of the direction on Google Maps?

Right click on the route and select Properties. There you can change the colour and line width. Generally it's better to use Google Maps for directions, Google Earth is more limited. Wow,,, all that just to change the color.

How do you tell what direction you're facing on Google Maps?

Rather than manually move your map to face a certain direction, you can tap the compass icon to automatically point the map view towards north and south at your current location. This means that, as long as your blue icon has a beam that points upwards, you're heading north.


2 Answers

Use directions service of Google Maps API v3. It's basically the same as directions API, but nicely packed in Google Maps API which also provides convenient way to easily render the route on the map.

Information and examples about rendering the directions route on the map can be found in rendering directions section of Google Maps API v3 documentation.

like image 98
Tomik Avatar answered Oct 21 '22 18:10

Tomik


Using Javascript

I created a working demo that shows how to use the Google Maps API Directions Service in Javascript through a

  • DirectionsService object to send and receive direction requests
  • DirectionsRenderer object to render the returned route on the map

function initMap() {    var pointA = new google.maps.LatLng(51.7519, -1.2578),      pointB = new google.maps.LatLng(50.8429, -0.1313),      myOptions = {        zoom: 7,        center: pointA      },      map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),      // Instantiate a directions service.      directionsService = new google.maps.DirectionsService,      directionsDisplay = new google.maps.DirectionsRenderer({        map: map      }),      markerA = new google.maps.Marker({        position: pointA,        title: "point A",        label: "A",        map: map      }),      markerB = new google.maps.Marker({        position: pointB,        title: "point B",        label: "B",        map: map      });      // get route from A to B    calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB);    }        function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB) {    directionsService.route({      origin: pointA,      destination: pointB,      travelMode: google.maps.TravelMode.DRIVING    }, function(response, status) {      if (status == google.maps.DirectionsStatus.OK) {        directionsDisplay.setDirections(response);      } else {        window.alert('Directions request failed due to ' + status);      }    });  }    initMap();
    html,      body {        height: 100%;        margin: 0;        padding: 0;      }      #map-canvas {        height: 100%;        width: 100%;      }
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>    <div id="map-canvas"></div>

Also on Jsfiddle: http://jsfiddle.net/user2314737/u9no8te4/

Using Google Maps Web Services

You can use the Web Services using an API_KEY issuing a request like this:

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=API_KEY

An API_KEY can be obtained in the Google Developer Console with a quota of 2,500 free requests/day.

A request can return JSON or XML results that can be used to draw a path on a map.

Official documentation for Web Services using the Google Maps Directions API are here

like image 37
user2314737 Avatar answered Oct 21 '22 17:10

user2314737