0
我正在嘗試在我的頁面上創建帶有標記的Google地圖。我有一個無序列表,其中每個項目都有緯度,經度和標題的數據屬性。使用JQuery我拉這些值,並在地圖上爲列表中的每個項目生成一個標記。一切似乎都可以正常工作,除非谷歌地圖在地圖周圍平移時不會加載切片。添加事件監聽器會導致Google地圖凍結
這是我如何初始化地圖:
var map;
// initialize google map
$(function() {
var myOptions = {
zoom: 10,
center: new google.maps.LatLng(CoordinatesLat, CoordinatesLong),
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
}
// initiate map
map = new google.maps.Map($("#map")[0], myOptions);
// when map is loaded, add events and behaviors to it
google.maps.event.addListenerOnce(map, 'tilesloaded', addEventsToMap(".event")); //commenting this line prevents GMAP problems
});
僅供參考 - 我用maps.event.addListener()之前,地圖將暫時工作,然後變得完全沒有反應。將其更改爲maps.event.addListenerOnce()可以阻止凍結,但仍然存在加載問題。
這是回調,顯然在那裏我做錯了什麼:
//add events from event list to map. add behavior to events ie. mouseover
function addEventsToMap(selector) {
$(selector).each(function (i, $e) {
$e = $($e);
var latlng;
if ($e.attr("data-geo-lat") && $e.attr("data-geo-long")) {
latlng = new google.maps.LatLng($e.attr("data-geo-lat"), $e.attr("data-geo-long"));
$e.marker = new google.maps.Marker({
position: latlng,
map: map,
title: $e.attr("data-title")
});
google.maps.event.addListener($e.marker, 'click', function() { window.location = $e.attr("data-href"); });
google.maps.event.addListener($e.marker, 'mouseover', function() { $e.addClass("event-hover"); });
google.maps.event.addListener($e.marker, 'mouseout', function() { $e.removeClass("event-hover"); });
//when mouse hovers over item in list, center map on it's marker
$e.mouseenter(function() {
map.panTo(latlng);
});
}
});
}
任何想法可能是什麼造成的?
直接顯示#map元素嗎?在隱藏該點的元素中啓動地圖時遇到問題。 –
是地圖元素立即顯示;不隱藏。 – wycleffsean