Below code shows the path between two latlongs :
JavaScript
x
52
52
1
function mapLocation() {
2
var directionsDisplay;
3
var directionsService = new google.maps.DirectionsService();
4
var map;
5
6
function initialize() {
7
directionsDisplay = new google.maps.DirectionsRenderer({
8
draggable: true
9
});
10
var city = new google.maps.LatLng(41.015137, 28.979530);
11
var mapOptions = {
12
zoom: 7,
13
center: city
14
};
15
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
16
directionsDisplay.setMap(map);
17
calcRoute();
18
}
19
20
function calcRoute() {
21
var start = new google.maps.LatLng(41.01524, 28.975994);
22
var end = new google.maps.LatLng(41.013232, 28.978676);
23
24
var request = {
25
origin: start,
26
destination: end,
27
travelMode: google.maps.TravelMode.DRIVING
28
29
};
30
directionsService.route(request, function(response, status) {
31
if (status == google.maps.DirectionsStatus.OK) {
32
alert(response.routes[0].legs[0].distance.value + " meters");
33
directionsDisplay.setDirections(response);
34
directionsDisplay.setMap(map);
35
36
//alert(request.distance);
37
} else {
38
alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
39
}
40
41
});
42
43
44
45
46
}
47
48
49
google.maps.event.addDomListener(window, 'load', initialize);
50
}
51
mapLocation();
52
The code always works, I can see the path all the time when I drag those two latlongs. But I want to get the position of latlongs after dragging. How can I do that? I tried to put alerts in various places in the codes but none of them worked. Can you help me with that?
JavaScript
1
51
51
1
function mapLocation() {
2
var directionsDisplay;
3
var directionsService = new google.maps.DirectionsService();
4
var map;
5
6
function initialize() {
7
directionsDisplay = new google.maps.DirectionsRenderer({
8
draggable: true
9
});
10
var city = new google.maps.LatLng(41.015137, 28.979530);
11
var mapOptions = {
12
zoom: 7,
13
center: city
14
};
15
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
16
directionsDisplay.setMap(map);
17
calcRoute();
18
}
19
20
function calcRoute() {
21
var start = new google.maps.LatLng(41.01524, 28.975994);
22
var end = new google.maps.LatLng(41.013232, 28.978676);
23
24
var request = {
25
origin: start,
26
destination: end,
27
travelMode: google.maps.TravelMode.DRIVING
28
29
};
30
directionsService.route(request, function(response, status) {
31
if (status == google.maps.DirectionsStatus.OK) {
32
//alert(response.routes[0].legs[0].distance.value + " meters");
33
directionsDisplay.setDirections(response);
34
directionsDisplay.setMap(map);
35
36
//alert(request.distance);
37
} else {
38
alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
39
}
40
41
});
42
43
44
45
46
}
47
48
49
google.maps.event.addDomListener(window, 'load', initialize);
50
}
51
mapLocation();
JavaScript
1
8
1
html,
2
body,
3
#map-canvas {
4
height: 90%;
5
width: 100%;
6
margin: 0px;
7
padding: 0px
8
}
JavaScript
1
5
1
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
2
3
<body>
4
<!-- <input type="button" id="routebtn" value="route" /> -->
5
<div id="map-canvas"></div>
Advertisement
Answer
The markers locations are in the directions property of the directionsDisplay. To retrieve them when the route changes, add an event listener to the directionsDisplay for the directions_changed
event, parse the directions object returned for the start and end locations of the route. For your route, with only a single leg, they will be:
JavaScript
1
3
1
directionsDisplay.getDirections().routes[0].legs[0].start_location;
2
directionsDisplay.getDirections().routes[0].legs[0].end_location;
3
To put them in a <input>
field on the page:
JavaScript
1
5
1
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
2
document.getElementById("startlatlng").value = directionsDisplay.getDirections().routes[0].legs[0].start_location.toUrlValue(6);
3
document.getElementById("endlatlng").value = directionsDisplay.getDirections().routes[0].legs[0].end_location.toUrlValue(6);
4
});
5
code snippet:
JavaScript
1
43
43
1
function mapLocation() {
2
var directionsDisplay;
3
var directionsService = new google.maps.DirectionsService();
4
var map;
5
6
function initialize() {
7
directionsDisplay = new google.maps.DirectionsRenderer({
8
draggable: true
9
});
10
var city = new google.maps.LatLng(41.015137, 28.979530);
11
var mapOptions = {
12
zoom: 7,
13
center: city
14
};
15
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
16
directionsDisplay.setMap(map);
17
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
18
document.getElementById("startlatlng").value = directionsDisplay.getDirections().routes[0].legs[0].start_location.toUrlValue(6);
19
document.getElementById("endlatlng").value = directionsDisplay.getDirections().routes[0].legs[0].end_location.toUrlValue(6);
20
});
21
calcRoute();
22
}
23
24
function calcRoute() {
25
var start = new google.maps.LatLng(41.01524, 28.975994);
26
var end = new google.maps.LatLng(41.013232, 28.978676);
27
var request = {
28
origin: start,
29
destination: end,
30
travelMode: google.maps.TravelMode.DRIVING
31
};
32
directionsService.route(request, function(response, status) {
33
if (status == google.maps.DirectionsStatus.OK) {
34
directionsDisplay.setDirections(response);
35
directionsDisplay.setMap(map);
36
} else {
37
alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status);
38
}
39
});
40
}
41
google.maps.event.addDomListener(window, 'load', initialize);
42
}
43
mapLocation();
JavaScript
1
8
1
html,
2
body,
3
#map-canvas {
4
height: 90%;
5
width: 100%;
6
margin: 0px;
7
padding: 0px
8
}
JavaScript
1
7
1
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
2
3
<body>
4
<input type="text" id="startlatlng" />
5
<input type="text" id="endlatlng" />
6
<!-- <input type="button" id="routebtn" value="route" /> -->
7
<div id="map-canvas"></div>