Чего я хочу добиться, так это одной карты с несколькими (в данном случае тремя) кнопками, когда вы нажимаете кнопку, карта загружает маркер с расположением этих кнопок.Таким образом, вы можете «прыгать» из одного места в другое.
Я работаю с тремя отдельными функциями и списками событий, но я думаю, что есть способ объединить все это до одной функции?Рабочее решение закомментировано в примере кода ниже ...
<div id="floating-panel">
<input id="address-1" class="address" value="Paris" type="button">
<input id="address-2" class="address" value="London" type="button">
<input id="address-3" class="address" value="New York" type="button">
var address = null;
function initMap() {
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('map'), {
zoom: @MapZoom,
center: { lat: @MapCenterLat, lng: @MapCenterLng}
});
document.getElementByClassName('address').addEventListener('click', function () {
address = this.value();
geocodeAddress(geocoder, map);
});
//document.getElementById('address-1').addEventListener('click', function () {
// geocodeAddress1(geocoder, map);
//});
//document.getElementById('address-2').addEventListener('click', function () {
// geocodeAddress2(geocoder, map);
//});
//document.getElementById('address-2').addEventListener('click', function () {
// geocodeAddress3(geocoder, map);
//});
}
function geocodeAddress(geocoder, resultsMap) {
geocoder.geocode({ 'address': address }, function (results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
//function geocodeAddress1(geocoder, resultsMap) {
// geocoder.geocode({ 'address': address }, function (results, status) {
// if (status === 'OK') {
// resultsMap.setCenter(results[0].geometry.location);
// var marker = new google.maps.Marker({
// map: resultsMap,
// position: results[0].geometry.location
// });
// } else {
// alert('Geocode was not successful for the following reason: ' + status);
// }
// });
//}
//function geocodeAddress2(geocoder, resultsMap) {
// geocoder.geocode({ 'address': address }, function (results, status) {
// if (status === 'OK') {
// resultsMap.setCenter(results[0].geometry.location);
// var marker = new google.maps.Marker({
// map: resultsMap,
// position: results[0].geometry.location
// });
// } else {
// alert('Geocode was not successful for the following reason: ' + status);
// }
// });
//}
//function geocodeAddress3(geocoder, resultsMap) {
// geocoder.geocode({ 'address': address }, function (results, status) {
// if (status === 'OK') {
// resultsMap.setCenter(results[0].geometry.location);
// var marker = new google.maps.Marker({
// map: resultsMap,
// position: results[0].geometry.location
// });
// } else {
// alert('Geocode was not successful for the following reason: ' + status);
// }
// });
//}