我在使用谷歌地图 API V3 时遇到问题。似乎每次我加载我的页面时,地图都会加载(包括标记),然后它会快速重新加载并删除所有标记。我错过了什么吗?我究竟做错了什么?
这是一个例子: http ://www.PaulPeelen.com/wp-content/uploads/2010/04/SafariScreenSnapz001.mov
这是我的代码:
<script type="text/javascript">
<!--
var hash = "{{$sHashLocal}}";
var webroot = "{{$webroot}}";
var map;
function initialize() {
var latlng = new google.maps.LatLng(59.32045, 18.066902);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
return map;
}
function getMarkerInfo (infowindow, rack_id)
{
// Get all the racks
$.ajax({
type: "GET",
url: webroot+"ajax/getRack/"+hash+"/"+rack_id,
cache: false,
dataType: "html",
success: function(html) {
$("#rack_"+rack_id).html();
infowindow.setContent(html);
}
});
}
$(document).ready(function () {
map = initialize();
function attachSecretMessage(marker, rack_id) {
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(50,50)
});
google.maps.event.addListener(marker, 'click', function(){
var ret = '<div id="rack_'+rack_id+'" class="rackDiv"><div class="rackDivLoading"><img src="theme/images/loader-small.gif" border="0"/><span>Hämtar data</span></div></div>';
infowindow.setContent(ret);
infowindow.open(map,marker);
getMarkerInfo(infowindow,rack_id);
});
}
function addPlacemark (lat,lng,title, rack_id) {
var image = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
new google.maps.Size(32, 32),
new google.maps.Point(0,0),
new google.maps.Point(0, 32));
var shadow = new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png',
new google.maps.Size(37, 34),
new google.maps.Point(0,0),
new google.maps.Point(-4, 34));
var location = new google.maps.LatLng(lat,lng);
var marker = new google.maps.Marker({
position: location,
map: map,
icon: image,
shadow: shadow});
marker.setTitle(title);
attachSecretMessage(marker, rack_id);
}
// Get all the racks
$.ajax({
type: "GET",
url: webroot+"ajax/getRacks/"+hash,
cache: false,
dataType: "xml",
success: function(xml) {
// Add the results
$(xml).find("station").each(function () {
rack_id = $(this).find("rack_id").text();
title = $(this).find("description").text();
longitute = parseFloat($(this).find("longitute").text());
latitude = parseFloat($(this).find("latitude").text());
addPlacemark(latitude, longitute, title, rack_id);
});
}
});
$("#addMark").click(function () {
addPlacemark (59.32045, 18.066902);
});
// Set size
setPageSize();
});
//-->
</script>