问题
The info window and marker is not shown for the places which is already marked by google. for example: if you run the code and click anywhere , a marker and a info window will appear as a result , but this doesn't happen for the places which are alreasy marked such as "The Brunswick","Open Air Theatre" etc.
<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js">
</script>
<script>
var map;
var myCenter=new google.maps.LatLng(51.508742,-0.120850);
function initialize(){
var mapProp = {
center:myCenter,
zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
}
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map,
});
var infowindow = new google.maps.InfoWindow({
content: 'Latitude: ' + location.lat() + ' Longitude: ' + location.lng()
});
infowindow.open(map,marker);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:1000px;height:1000px;"></div>
</body>
</html>
回答1:
When you click on such a place(POI) there will no click be triggered on the map.
When you don't want to hide the POI's, you must trigger the click on your own.
Possible approach(see: How to get a click event when a user clicks a (business) place on the map )
//keep a reference to the original setPosition-function
var fx = google.maps.InfoWindow.prototype.setPosition;
//override the built-in setPosition-method
google.maps.InfoWindow.prototype.setPosition = function () {
//this property isn't documented, but as it seems
//it's only defined for InfoWindows opened on POI's
if (this.logAsInternal) {
google.maps.event.addListenerOnce(this, 'map_changed',function () {
var map = this.getMap();
//the infoWindow will be opened, usually after a click on a POI
if (map) {
//trigger the click
google.maps.event.trigger(map, 'click', {latLng: this.getPosition()});
//hide the default-infoWindow of the POI
this.setMap(null);
}
});
}
//call the original setPosition-method
fx.apply(this, arguments);
};
Demo: http://jsfiddle.net/doktormolle/1yrpm98q/
回答2:
Hide those POIs:
styles: [{
featureType: "poi",
elementType: "labels",
stylers: [{
visibility: "off"
}]
}]
working code snippet:
var map;
var myCenter = new google.maps.LatLng(51.508742, -0.120850);
function initialize() {
var mapProp = {
center: myCenter,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [{
featureType: "poi",
elementType: "labels",
stylers: [{
visibility: "off"
}]
}]
};
map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
}
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map,
});
var infowindow = new google.maps.InfoWindow({
content: 'Latitude: ' + location.lat() + ' Longitude: ' + location.lng()
});
infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#googleMap {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="http://maps.google.com/maps/api/js"></script>
<div id="googleMap"></div>
来源:https://stackoverflow.com/questions/28186720/i-am-not-able-to-place-markers-on-the-places-which-is-already-marked-by-google-w