我目前有一个带有侧边栏的googlemap画布,其中有链接,点击后,将用户带到标记和infowindow的latlong位置。加载地图时,它会居中,但是当他们点击链接时,地图不会平移到中心,标记位于中间。
我尝试在我的标记函数中添加了map.setCenter,但这使得地图和标记消失,我的控制台中没有错误。任何人都可以引导我到一段代码或教程可以帮助吗?这是我的示例代码:
<script type="text/javascript">
var side_bar_html = "";
var gmarkers = [];
var map = null;
function initialize() {
var myOptions = {
zoom: 12,
center: new google.maps.LatLng(46.18363372751015,12.4530029296875),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
var point = new google.maps.LatLng(46.18363372751015,12.4530029296875);
var marker = createMarker(point,"example 1","location 1 info")
var point = new google.maps.LatLng(39.31517545076218, 35.25238037109375);
var marker = createMarker(point,"example 2","location 2 info")
document.getElementById("marker_list").innerHTML = side_bar_html;
}
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(150,50),
});
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
function createMarker(latlng, name, html) {
var contentString = html;
var marker = new google.maps.Marker({
position:latlng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
gmarkers.push(marker);
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';
}
</script>
答案 0 :(得分:0)
如果您希望地图在单击时在标记上居中,则需要编写代码来执行此操作。默认情况下,它会平移,以便InfoWindow可见。改变这个:
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
类似于:
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}