【发布时间】:2013-12-14 21:40:08
【问题描述】:
首先我应该说我不太了解 javascript 或 Google API V3。无论如何,我一直在尝试包含一个带有标记的谷歌地图,以指示我使用 infowindow 访问过的地方。我在互联网上找到了一个示例,并将其复制到我的页面。然后我尝试添加一个功能以在单击标记时放大到该位置。更改代码后,我设法使缩放工作非常好。但是,信息窗口不会打开。在我使用它时打开之前;
infowindow.setContent(contentString);
infowindow.open(map,marker);.
但缩放无法正常工作。改成这个后;
infowindow.setContent(this.html);
infowindow.open(map, this);
缩放效果很好。但是我丢失了信息窗口。谁能看到我如何更改代码以使缩放和信息窗口都工作。如果有人可以帮助我,我会很高兴。我可以使用下面的代码进行调整,还是需要完全重写?提前致谢
<script>
//<![CDATA[
// this variable will collect the html which will eventually be placed in the side_bar
var side_bar_html = "";
// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
var map = null;
function initialize() {
// create the map
var myOptions = {
zoom: 7,
center: new google.maps.LatLng(47.516231, 13.550072),
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
google.maps.event.addListener(map, 'click', function () {
infowindow.close();
});
// Add markers to the map
// add the points
var point = new google.maps.LatLng(48.208174, 16.373819);
var marker = createMarker(point, "Vienna", '<div style="font-size:12px;font-weight: bold;font-family:segoe ui, Trebuchet MS;">Vienna</div>')
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = side_bar_html;
}
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(150, 100)
});
// This function picks up the click and opens the corresponding info window
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
// A function to create the marker and set up the event window function
function createMarker(latlng, name, html) {
var contentString = html;
var marker = new google.maps.Marker({
position: latlng,
map: map,
zIndex: Math.round(latlng.lat() * -100000) << 5
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(this.html);
infowindow.open(map, this);
map.setZoom(10);
map.setCenter(marker.getPosition());
});
// save the info we need to use later for the side_bar
gmarkers.push(marker);
// add a line to the side_bar html
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + '<\/a><br>';
}
</script>
【问题讨论】:
-
为什么把
(map, marker)改成(map, this)?认为你应该只使用标记。 -
@putvande - 你为什么这么认为?他实际上应该使用
this -
答:
map.setCenter(this.getPosition()); map.setZoom(10);..
标签: javascript google-maps google-maps-api-3 infowindow