问题
I am working with a Google Map API and I have imported a kml layer into my code.My problem is that I don't know how to remove the 'Unknown Point Feature' information from the info window. Any suggestions? here is a screenshot of what I am talking about:
This is my code for importing the kml file:
var AI_url = 'https://drive.google.com/uc?export=download&id=0B2KR4Lz3foYEeEtfR0laWWM0LVk'
var AI_options = {
preserveViewport: true,
map: map
};
var AI_layer = new google.maps.KmlLayer(AI_url, AI_options);
回答1:
One option would be to set the suppressInfoWindows option of the KmlLayer, then add your own click listener that just displays the "name":
var AI_url = 'https://drive.google.com/uc?export=download&id=0B2KR4Lz3foYEeEtfR0laWWM0LVk'
var AI_options = {
preserveViewport: true,
suppressInfoWindows: true,
map: map
};
var AI_layer = new google.maps.KmlLayer(AI_url, AI_options);
google.maps.event.addListener(AI_layer,'click',function(e) {
infoWindow.setOptions({
content:"<b>"+e.featureData.name+"</b>",
pixelOffset:e.pixelOffset,
position:e.latLng
});
infoWindow.open(map);
});
proof of concept code snippet:
var geocoder;
var map;
var infoWindow = new google.maps.InfoWindow();
function initialize() {
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var AI_url = 'https://drive.google.com/uc?export=download&id=0B2KR4Lz3foYEeEtfR0laWWM0LVk'
var AI_options = {
preserveViewport: true,
suppressInfoWindows: true,
map: map
};
var AI_layer = new google.maps.KmlLayer(AI_url, AI_options);
google.maps.event.addListener(AI_layer, 'click', function(e) {
infoWindow.setOptions({
content: "<b>" + e.featureData.name + "</b>",
pixelOffset: e.pixelOffset,
position: e.latLng
});
infoWindow.open(map);
});
codeAddress("Calgary, Canada");
}
google.maps.event.addDomListener(window, "load", initialize);
function codeAddress(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.fitBounds(results[0].geometry.viewport);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
回答2:
The info window content is coming from the KML file so you would have to remove it from there, provided you have access to the file from the location it's being served of course.
回答3:
From the API: https://developers.google.com/maps/tutorials/kml/
var kmlOptions = {
**suppressInfoWindows: true,**
preserveViewport: false,
map: map
};
来源:https://stackoverflow.com/questions/28617292/how-to-remove-information-from-info-window-on-a-kmllayer