当用户点击两个标记时,我需要在谷歌地图标记上绘制路线,路线出现在用户点击的标记之间。 在图像中,当用户点击两个标记路线时,您可以看到我需要的多个地图标记。
var map;
function initMap() {
var origin_place_id = null;
var destination_place_id = null;
var myCenter=new google.maps.LatLngBounds();
var travel_mode = google.maps.TravelMode.WALKING;
map = new google.maps.Map(document.getElementById('map'), {
mapTypeControl: false,
center: myCenter,
zoom: 7,
mapTypeId:google.maps.MapTypeId.ROADMAP
});
var markers = [
['Faisalabad, Pakistan', 31.4187,73.0791],
['Multan, Pakistan', 30.1984,71.4687],
['Lahore, Pakistan', 31.5546,74.3572],
['Sahiwal, Pakistan', 30.6612,73.1086]
];
var infoWindow = new google.maps.InfoWindow(), marker, i;
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
myCenter.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
map.fitBounds(myCenter);
}
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(8);
google.maps.event.removeListener(boundsListener);
});
google.maps.event.addDomListener(document.getElementById('go'), 'click',route);
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
var origin_input = document.getElementById('origin-input');
var destination_input = document.getElementById('destination-input');
var modes = document.getElementById('mode-selector');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(modes);
var origin_autocomplete = new google.maps.places.Autocomplete(origin_input);
origin_autocomplete.bindTo('bounds', map);
var destination_autocomplete = new google.maps.places.Autocomplete(destination_input);
destination_autocomplete.bindTo('bounds', map);
function setupClickListener(id, mode) {
var radioButton = document.getElementById(id);
radioButton.addEventListener('click', function() {
travel_mode = mode;
});
}
setupClickListener('changemode-walking', google.maps.TravelMode.WALKING);
setupClickListener('changemode-transit', google.maps.TravelMode.TRANSIT);
setupClickListener('changemode-driving', google.maps.TravelMode.DRIVING);
function expandViewportToFitPlace(map, place) {
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
}
origin_autocomplete.addListener('place_changed', function() {
var place = origin_autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
return;
}
expandViewportToFitPlace(map, place);
origin_place_id = place.place_id;
route(origin_place_id, destination_place_id, travel_mode,
directionsService, directionsDisplay);
});
destination_autocomplete.addListener('place_changed', function() {
var place = destination_autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
return;
}
expandViewportToFitPlace(map, place);
$('#go').click(function(){
var origin_input = document.getElementById('origin-input').value;
var res = origin_input.split(",");
var bc = res[0];
var destination_input = document.getElementById('destination-input').value;
var res = destination_input.split(",");
var bd = res[0];
destination_place_id = place.place_id;
route(origin_place_id, destination_place_id, travel_mode,
directionsService, directionsDisplay);
});
});
$('#mode-selector').hide();
$('#go').click(function(){
//$('#go').hide(250);
$('#mode-selector').show(250);
});
function route(origin_place_id, destination_place_id, travel_mode,
directionsService, directionsDisplay) {
if (!origin_place_id || !destination_place_id) {
return;
}
directionsService.route({
origin: {'placeId': origin_place_id},
destination: {'placeId': destination_place_id},
travelMode: travel_mode
},
function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
}
答案 0 :(得分:1)
<!DOCTYPE html>
<html>
<head>
<title>Simple click event</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// JavaScript Document
var map;
function initMap()
{
var myCenter=new google.maps.LatLngBounds();
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
map = new google.maps.Map(document.getElementById('map'), {
mapTypeControl: false,
center: myCenter,
zoom: 7,
mapTypeId:google.maps.MapTypeId.ROADMAP
});
var markers = [
['Faisalabad, Pakistan', 31.4187,73.0791],
['Multan, Pakistan', 30.1984,71.4687],
['Lahore, Pakistan', 31.5546,74.3572],
['Sahiwal, Pakistan', 30.6612,73.1086]
];
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
var infoWindow = new google.maps.InfoWindow(), marker, i;
for( i = 0; i < markers.length; i++ ) {
//var myLatLng = {lat: markers[i][1], lng: markers[i][2]};
var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
myCenter.extend(pos);
marker = new google.maps.Marker({
position: pos,
map: map,
title: markers[i][0]
});
map.fitBounds(myCenter);
var route = 0;
var pos_source=0;
var pos_destination=0;
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
route++;
if(route==1)
{
pos_source = this.position;
}
if(route==2)
{
pos_destination = this.position;
route = 0;
var start = new google.maps.LatLng(pos_source.lat(), pos_source.lng());
var end = new google.maps.LatLng(pos_destination.lat(), pos_destination.lng());
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);
}
});
}
}
})(marker, i));
}
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
</body>
</html>
&#13;
答案 1 :(得分:0)
您所要做的就是跟踪用户点击的两个标记。为此创建一个全局标记数组。在每个标记上单击检查标记数组是否为: