function initMap() {
var pointA = new google.maps.LatLng(51.2750, 1.0870),
pointC = new google.maps.LatLng(50.7700, 0.28),
pointB = new google.maps.LatLng(51.5379, 0.7138),
airDistAB = google.maps.geometry.spherical.computeDistanceBetween(pointA, pointB),
airDistAC = google.maps.geometry.spherical.computeDistanceBetween(pointA, pointC),
myOptions = {
zoom: 5,
center: pointA,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
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
}),
markerC = new google.maps.Marker({
position: pointC,
title: "point C",
label: "C",
map: map
}),
outputAtoB = document.getElementById('a2b'),
outputAtoC = document.getElementById('a2c');
// calculate routes
responseAB = calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, outputAtoB, airDistAB, true);
responseAC = calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointC, outputAtoC, airDistAC, false);
// click on marker B to get route from A to B
markerB.addListener('click', function() {
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, outputAtoB, airDistAB, true);
});
markerC.addListener('click', function() {
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointC, outputAtoC, airDistAC, true);
});
document.getElementById('mode').addEventListener('change', function() {
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB, outputAtoB, airDistAB, true);
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointC, outputAtoC, airDistAC, false);
});
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, point1, point2, outputTxt, airDist, display) {
var selectedMode = document.getElementById('mode').value;
directionsService.route({
origin: point1,
destination: point2,
travelMode: google.maps.TravelMode[selectedMode]
}, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
if (display) {
directionsDisplay.setDirections(response);
}
outputTxt.innerHTML = "~" + Math.round(response.routes[0].legs[0].distance.value/1000) + " Km" + " (air: ~" + Math.round(airDist/1000) + "Km)";
return response;
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
initMap();
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas {
height: 100%;
width: 60%;
float: left;
}
#floating-panel {
position: float top;
top: 10px;
left: 25%;
z-index: 5;
background-color: #678;
padding: 5px;
border: 1px solid #123;
text-align: center;
font-family: 'Roboto', 'sans-serif';
line-height: 30px;
padding-left: 10px;
}
#output {
padding: 6px;
width: 38%;
float: right;
}
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script>
<div id="floating-panel"> <b>Mode of Travel: </b>
<select id="mode">
<option value="DRIVING">Driving</option>
<option value="WALKING">Walking</option>
<option value="BICYCLING">Bicycling</option>
<option value="TRANSIT">Transit</option>
</select>
</div>
<div id="map-canvas"></div>
<div id="out<put">
<h3>Distances</h3>
<b> A to B: </b><span id="a2b"></span>
<p> <b> A to C: </b><span id="a2c"></span>
</div>
Может быть, вы можете искать направления между двумя пунктами и получить весь возможный транспорт (метро, автобус, автомобиль, авиакомпания). Google Map может предоставить примерное время, а затем выбрать самое низкое время. –
Спасибо за ваш комментарий, но на данный момент мы заботимся только о расстоянии вождения, так как все они на месте техников с фургонами или ute. –
Для вашего второго вопроса поиск подходящего кандидата во всех кандидатах не будет эффективным. Я бы предложил создать структуру данных, которая хранит соседнее местоположение в данном месте. Или используйте карту google и запрос для заданного диапазона, например (в пределах 50 миль), и проверьте, является ли возвращенное местоположение одним из местоположений в вашей базе данных. –