簡體   English   中英

Google Maps API V3:使用大多數標記縮放到狀態

[英]Google Maps API V3: Zoom to State with Most Markers

我已經被要求設置中心並縮放到我在Google地圖上放置最多標記的狀態的狀態級別。 我正在使用的所有數據都是一組點(緯度/經度)。

我意識到我可以針對每個點進行地理定位調用( http://code.google.com/apis/maps/documentation/geocoding/index.html#GeocodingRequests - 請參閱:反向地理編碼),然后計算狀態以確定哪個我應該展示一下,因為每張地圖會有數百到數千個點,這是不切實際的。

還有什么我可以做到的嗎? 有什么可能是相似的(高濃度標記的前/中心)?

這是一個想法。 請參閱此SO問題 第一個答案有一個XML文件的鏈接,其中包含所有狀態邊界的多邊形坐標。 您還可以簡化多邊形,因此沒有那么多頂點。

當一個標記添加到地圖上,您可以檢查,看它是否在使用類似的算法50個陣列中的一個存在

更新:我發布的原始功能不是在javascript中。 這是一個Javascript和它的工作小提琴

/*
* state == array of Google LatLng objects.
* lat == latitude to test
* lng == longitude to test
*/
function pointInPolygon(state, lat, lng) {
    var polyCount = state.length;
    var oddNodes = false;
    var j = 0;
    for (var i = 0; i < polyCount; i++) {
        j++;
        if (j == polyCount) {
            j = 0;
        }
        latitudeBoundry = state[i].lat();
        longitudeBoundry = state[i].lng();
        latitudeBoundry2 = state[j].lat();
        longitudeBoundry2 = state[j].lng();
        if ((latitudeBoundry > lat && latitudeBoundry <= lat 
             || latitudeBoundry2 > lat && latitudeBoundry <= lat)) {
            if (longitudeBoundry + (lat - latitudeBoundry)
                / (latitudeBoundry2 - latitudeBoundry) 
                * (longitudeBoundry2 - longitudeBoundry) > lng) {
                oddNodes = !oddNodes
            }
        }
    }
    return oddNodes;
}

如果它存在增加一個計數器。

找到標記最多的狀態后,可以通過創建邊界對象來設置縮放。

//the polyArray is the array of points for the target state.
var bounds = new google.maps.LatLngBounds();
for ( var i = 0; i < polyArray.length; i++ )
{
  bounds.extend( polyArray[ i ] );
}

//set the map viewport
map.fitBounds(bounds)

我不知道這將如何明智地實現性能,但它應該比反向地理編碼快得多。

這是一個示例頁面,它實現了我之前提到的網格方法。 它在概念上與Bryans類似,但是,因為它只是一個簡單的划分來確定網格中的哪個扇區,它在大型數據集上可能會快一點(但你確實失去了狀態特性):

<!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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM