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)
How to draw that direction line on map ?
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.
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.
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.
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 requestsDirectionsRenderer
object to render the returned route on the mapfunction 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/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With