<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 33.738045, lng: 73.084488 },
zoom: 10
});
new AutocompleteDirectionsHandler(map);
}
/**
* @constructor
*/
function AutocompleteDirectionsHandler(map) {
this.map = map;
this.originPlaceId = null;
this.destinationPlaceId = null;
this.travelMode = 'DRIVING';
var originInput = document.getElementById('origin-input');
var destinationInput = document.getElementById('destination-input');
var modeSelector = document.getElementById('mode-selector');
this.directionsService = new google.maps.DirectionsService;
this.directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
this.directionsDisplay.setMap(map);
var originAutocomplete = new google.maps.places.Autocomplete(
originInput, { placeIdOnly: true });
var destinationAutocomplete = new google.maps.places.Autocomplete(
destinationInput, { placeIdOnly: true });
this.setupClickListener('changemode-walking', 'DRIVING');
this.setupPlaceChangedListener(originAutocomplete, 'ORIG');
this.setupPlaceChangedListener(destinationAutocomplete, 'DEST');
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput);
this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector);
}
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
AutocompleteDirectionsHandler.prototype.setupClickListener = function (id, mode) {
var radioButton = document.getElementById(id);
var me = this;
radioButton.addEventListener('click', function () {
me.travelMode = 'DRIVING';
me.route();
});
};
AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function (autocomplete, mode) {
var me = this;
autocomplete.bindTo('bounds', this.map);
autocomplete.addListener('place_changed', function () {
var place = autocomplete.getPlace();
if (!place.place_id) {
window.alert("Please select an option from the dropdown list.");
return;
}
if (mode === 'ORIG') {
me.originPlaceId = place.place_id;
} else {
me.destinationPlaceId = place.place_id;
}
me.route();
});
};
AutocompleteDirectionsHandler.prototype.route = function () {
if (!this.originPlaceId || !this.destinationPlaceId) {
return;
}
var me = this;
this.directionsService.route({
origin: { 'placeId': this.originPlaceId },
destination: { 'placeId': this.destinationPlaceId },
travelMode: this.travelMode
}, function (response, status) {
if (status === 'OK') {
me.directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
};
</script>
Currently I am making a project in asp.net C# web forms which reads the location and save it in database
I want to find the country city latitude longitude when the markers are dragged and want to bind the search boxes according to markers positions
If you are after the details (country, city, latitude, longitude) of the initial origin and destination, before dragging directions, just remove { placeIdOnly: true }
from google.maps.places.Autocomplete()
calls.
If you are after the details (country, city, latitude, longitude) of the places where markers are dragged to, when a user is dragging directions, you'll need to
directionsDisplay
for 'directions_changed'
. See the directions-draggable example . start_location
of all legs
, and end_location
of last one, to get waypoints' latlngs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.