gpt4 book ai didi

javascript - 谷歌地图为什么在添加新标记时整个页面都会重新绘制

转载 作者:行者123 更新时间:2023-11-29 18:23:05 25 4
gpt4 key购买 nike

我有一个 map 应用程序,我有一些标记会在 document.ready() 时加载,还有一些标记会在我单击按钮时加载,但是当我单击按钮时,标记会添加到文档中。 ready() 丢失了。

单击按钮时,整个页面都会提交并重绘。这也导致我的 map 在缩放和滚动时移动得非常慢。有人可以帮忙吗。下面是我的代码。我想让页面运行得更快,我不想在每次 ajax 请求时都重新绘制它。

代码

  <script type="text/javascript">


function initialize() {
var mapOptions = {
center: new google.maps.LatLng(10.670044,-61.515305),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);


var criminal = new google.maps.MarkerImage('resources/icons/police_new.ico',
new google.maps.Size(100,106),
new google.maps.Point(0,0),
new google.maps.Point(50,50)
);

var companyPos = new google.maps.LatLng(10.670044,-61.515305);
var companyMarker = new google.maps.Marker({
position: companyPos,
map: map,
icon: criminal,
title:"First Criminal"
});


$.ajax({
type:'GET',
async:false,
global:'false',
url:'getListOfCrimeHotSpot.htm',
headers : {Accept: 'application/json'},
dataType: 'json'
}).done(function(hotspot){

$.each(hotspot, function(i,h){
var icon = 'resources/icons/information.ico';

new google.maps.Marker({
position:new google.maps.LatLng(h.lat,h.lng),
map:map,
icon: new google.maps.MarkerImage(icon, new google.maps.Size(100, 106), new google.maps.Point(0, 0), new google.maps.Point(50, 50)),
title: 'Crime Rating : ' + h.crimeLevel+' @ ' +h.crimeLevelCount+' / ' + h.totalNumberOfCrimes
});
});
});



return map;


}//end initialize


var global_citizens;

$(document).ready(function(){
map = initialize();


$('#startTracking').on('click',function(){
$.each(global_citizens, function(i, c) {
console.log(c.name );

});
});



$('#getCitizens').on('click', function() {
/*var mapOptions = {
center: new google.maps.LatLng(10.670044, -61.515305),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};*/
//map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

$.ajax({
type: 'GET',
async : false,
global: 'false',
url: 'getListOfMarkers.htm',
headers : {Accept: 'application/json'},
dataType: 'json'
}).done(function(citizens) {
global_citizens = citizens;

var markerSource = [
null,
null,
'resources/icons/criminal_new.ico',
'resources/icons/victim_new.ico',
'resources/icons/suspect_new.ico'
];
$.each(citizens, function(i, c) {
//console.log(c.name + ' | ' + c.socialSecurityNumber + ' | ' + c.lat+ ' | ' +c.lng);
var icon = markerSource[c.citizenType];
if(markerSource) {
new google.maps.Marker({
position: new google.maps.LatLng(c.lat, c.lng),
map: map,
icon: new google.maps.MarkerImage( icon, new google.maps.Size(100, 106), new google.maps.Point(0, 0), new google.maps.Point(50, 50) ),
title: c.name +'-' +c.socialSecurityNumber
});
}
});
});
});

});
</script>

html

<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:90%">

<!-- MAP GOES IN HERE -->

</div>

<div id="toolbar">
<button class="btn" id="getCitizens" onclick="" type="button">Get Citizens</button>
<button class="btn" id="startTracking" onclick="" type="button">Start Tracking</button>
</div>
</body>

最佳答案

以下行导致了问题(在此函数内:$('#getCitizens').on('click', function() {).

var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

每次单击 #getCitizens 时,您都会创建一张新 map 。请改用先前实例化的 map (初始化函数返回的 map )。您不必以这种方式重新加载 map 。

关于javascript - 谷歌地图为什么在添加新标记时整个页面都会重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16394137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com