gpt4 book ai didi

javascript - 谷歌地图 API V3 : Zoom to State with Most Markers

转载 作者:行者123 更新时间:2023-12-02 19:56:52 25 4
gpt4 key购买 nike

我被要求将中心设置为我在 Google map 上放置最多标记的州的州级别。我正在处理的所有数据都是点的集合(纬度/经度)。

我意识到我可以对每个点进行地理定位调用(http://code.google.com/apis/maps/documentation/geocoding/index.html#GeocodingRequests - 请参阅:反向地理编码),然后对州进行计数以确定我应该显示哪些点,但是,由于每个 map 将有数百到数千个点,这是不切实际的。

我还能做些什么来实现这个目标吗?是否有任何可能类似的东西(例如/高浓度标记的中心)?

最佳答案

这里是一个示例页面,它实现了我之前提到的网格方法。它在概念上与布莱恩斯类似,但是,因为它只是一个简单的划分来确定网格中的哪个扇区,所以在大型数据集上可能会更快一点(但你确实会失去状态特异性):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Center</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<style>
#wrap {width:960px;margin-right:auto;margin-left:auto;position:relative;}
#map_canvas {width:100%;height:700px;}
table,td {border-collapse:collapse;border:thin #000 solid;}
</style>
</head>
<body>
<div id="wrap">
<div id="map_canvas"></div>
<div id="tabular"></div>
<script type="text/javascript">
function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
var map;
var placesToFilter=Array();
var myOptions = {zoom: 6,mapTypeControl: false,mapTypeId: google.maps.MapTypeId.ROADMAP};
//---Creating random data
for(i=0;i<500;i++){
var a=new Object();
a.lat=randomFromTo(2600,4900)/100;
a.lng=-randomFromTo(6600,12500)/100;
placesToFilter.push(a);
}
//---Get max and min latitude
var maxLat=placesToFilter[0].lat*1;
var minLat=placesToFilter[0].lat*1;
for (i=1;i<placesToFilter.length;i++) {
if (placesToFilter[i].lat*1>maxLat) {maxLat=placesToFilter[i].lat*1;}
if (placesToFilter[i].lat*1<minLat) {minLat=placesToFilter[i].lat*1;}
}
//---Get max and min longitude
var maxLng=placesToFilter[0].lng*1;
var minLng=placesToFilter[0].lng*1;
for (i=1;i<placesToFilter.length;i++) {
if (placesToFilter[i].lng*1>maxLng) {maxLng=placesToFilter[i].lng*1;}
if (placesToFilter[i].lng*1<minLng) {minLng=placesToFilter[i].lng*1;}
}
var s=8;//--------------------How many rows/columns the area gets gridded into
var latDelta=maxLat-minLat;
var lngDelta=maxLng-minLng;
var latStep=latDelta/s;
var lngStep=lngDelta/s;
var latBands=Array();
for(i=1;i<=s;i++){latBands.push(i*latStep);}
var lngBands=Array();
for(i=1;i<=s;i++){lngBands.push(i*lngStep);}
//---Keeping score in these arrays
var gridCount=new Array();
for(var x=0;x<s;x++){
for(var y=0;y<s;y++){
var cell=[x,y];
gridCount.push(cell);
}
}
for(var lt=0;lt<s;lt++){
for(var lg=0;lg<s;lg++){
gridCount[lt][lg]=0;
}
}

map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);

for(p=0;p<placesToFilter.length;p++){
//---Keeping track of which grid sector
var whichLat=Math.floor((placesToFilter[p].lat-minLat)/latStep);
var whichLng=Math.floor((placesToFilter[p].lng-minLng)/lngStep);
gridCount[whichLat][whichLng]++;
//---And placing the marker
var point=new google.maps.LatLng(placesToFilter[p].lat,placesToFilter[p].lng);
var marker = new google.maps.Marker({position: point,map: map});
}
//---Figuring out which cell 'won'
var checking=gridCount[0][0];
var rightLat;
var rightLng;
for(lt=0;lt<s;lt++){
for(lg=0;lg<s;lg++){
if(gridCount[lt][lg]>checking){
checking=gridCount[lt][lg];
rightLat=lt;
rightLng=lg;
}
}
}
//convert grid sector to lat/lng (center of sector)
var winningLat=maxLat-(rightLat*latStep)-(latStep/2);
var winningLng=minLng+(rightLng*lngStep)+(lngStep/2);
var newCenter=new google.maps.LatLng(winningLat,winningLng);
map.setCenter(newCenter);
showTable=true; //--------------this will display the table of data so you can see how many markers are in each sector
if(showTable){
var table='<table>';
for(row=0;row<s;row++){
table+='<tr>';
for(td=0;td<s;td++){
table+='<td>'+gridCount[row][td]+'</td>';
}
table+='</tr>';
}
table+='</table>';
document.getElementById('tabular').innerHTML=table;
}
</script>
</div>
</body>
</html>

关于javascript - 谷歌地图 API V3 : Zoom to State with Most Markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8494735/

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