请帮助,我真的很困惑,在谷歌地图上成功搜索地点后添加事件点击标记。我尝试了一些项目,添加事件点击标记和工作但不使用方法搜索的地方,这意味着使用硬编码标记位置,对于这种情况,我希望有人可以帮助我这是我的代码。
function initMap() {
var mapOptions = {
center: new google.maps.LatLng(0.7893, 113.9213),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var input = document.getElementById('search_location_input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
// marker
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
for (var i = 0; i < markers.length; i++) {
var data = markers[i];
(function(marker, data) {
google.maps.event.addListener(marker, "click", function(e) {
console.log(data.title);
});
})(markers, data);
}
});
}
答案 0 :(得分:1)
map.fitBounds(bounds);
for (var i = 0; i < markers.length; i++) {
markers[i].index = i; //add index property
var data = markers[i];
// var latitude = data.position.lat();
// var longitude = data.position.lng();
google.maps.event.addListener(markers[i],'click', function(e) {
var title = markers[this.index].title;
var latitude = markers[this.index].position.lat();
var longitude = markers[this.index].position.lng();
console.log(this.index);
console.log(title);
console.log(latitude);
console.log(longitude);
});
}
答案 1 :(得分:0)
我在循环addListener
中形成标记后立即向标记添加了places.forEach(function(place) {...})
。
var map;
function initMap() {
var mapOptions = {
center: new google.maps.LatLng(0.7893, 113.9213),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var input = document.getElementById('search_location_input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
// marker
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
})
markers.push(marker);
/*adding event addlistner*/
google.maps.event.addListener(marker, "click", function(e) {
console.log(place.name);
});
if (place.geometry.viewport) {
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}