Monday, May 13, 2024
50
rated 0 times [  57] [ 7]  / answers: 1 / hits: 79844  / 10 Years ago, sun, december 7, 2014, 12:00:00

Hi I am trying to draw a route map between two markers using javascript.
I have tried various examples found online but my map is not loading while trying out different examples. I am unable to figure out the cause of the error. My map just does not load.



I am trying to draw a route for the below two markers.



    <script>

function mapLocation() {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(37.334818, -121.884886);
var mapOptions = {
zoom: 7,
center: chicago
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
}

function calcRoute() {
var start = new google.maps.LatLng(37.334818, -121.884886);
var end = new google.maps.LatLng(38.334818, -181.884886);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}

google.maps.event.addDomListener(window, 'load', initialize);
}
mapLocation();
</script>


Can someone please help me draw a route between the two markers?


More From » google-maps-api-3

 Answers
7

Quite a few mistakes. First is the geolocation. Your second location is wrong as the longitude can only be from +180 to -180 so -181 doesn't exist in the earth! Secondly, as MrUpsidedown mentioned in the comment, you are calling a function within a function. Correct the geolocation first and then your function calls, that should fix the problems you're having.


[#68556] Thursday, December 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
roberts

Total Points: 212
Total Questions: 101
Total Answers: 101

Location: Philippines
Member since Thu, Apr 14, 2022
2 Years ago
;