【问题标题】:How to get addresses (geocoding informations) from polygon in maps?如何从地图中的多边形获取地址(地理编码信息)?
【发布时间】:2017-10-22 07:22:26
【问题描述】:

我正在寻找一种方法来检索地图中多边形内的所有地址(谷歌地图?)。我在this post 看到有人说这是不可能的.. 我问这个:如果可以从地图上的某个点检索地址,为什么不能在多边形中做同样的事情?难道你不能: - 在多边形区域中设置一个hypoteticl网格 - 为网格的每个交点检索位置地址 - 存储地址(如果已经存储,请删除它)。

这种方法不可行吗?谢谢弗朗西斯科

【问题讨论】:

    标签: maps geocoding


    【解决方案1】:

    环顾四周,我几乎找到了一种方法,合并了各种方法。我是 js 新手,所以我的代码可能充满了愚蠢的东西。 无论如何,我设法将我的唯一地址检索到一个数组中,但由于我总是收到 OVER_QUERY_LIMIT 的错误,所以我无法完成这项工作,因为我按照解释发送了太多请求in this page

    所以我认为没有办法做到这一点,除非你想花很多钱购买账单来解锁更高的配额,这将是一个非常严重的打击,因为这样的方法每次调用都需要数千个请求. 无论如何,这是代码,插入到找到的 here 中,我从 Polygon Drawing and Getting Coordinates with Google Map API v3 得到的 再次为您将在其中找到的各种新手内容感到抱歉。

    var arrAddress=[]; //global
    var arrCoord=[]; //global
    
    function selPoly(shape) { //after create polygon do the job
    
      var latlngStr=[];
      var lat,lng;
      var polCoords = [];
    
    posstr = "" + selectedShape.position;
    if (typeof selectedShape.position == 'object') {
          posstr = selectedShape.position.toUrlValue();
        }
    
    if (typeof selectedShape.getPath == 'function') {
         var mycoord="";  
         var partlng, partlng;
      for (var i = 0; i < selectedShape.getPath().getLength(); i++) {
        // .toUrlValue(5) limits number of decimals, default is 6 but can do more
        mycoord=selectedShape.getPath().getAt(i).toUrlValue();
        latlngStr = mycoord.split(',');
        //create array with polygon path
        polCoords.push(new google.maps.LatLng(latlngStr[0] , latlngStr[1]));
        //add 0s to lat 
        lat=latlngStr[0];
        partlat = latlngStr[0].substring(latlngStr[0].indexOf(".") + 1);
        switch(partlat.length) {
            case 4:
                lat =lat +"00";
                break;
            case 5:
                lat =lat +"0";
                break;
        } 
            //add 0s to lng 
            lng=latlngStr[1];
            partlng = latlngStr[1].substring(latlngStr[1].indexOf(".") + 1);
            switch(partlng.length) {
                case 4:
                    lng =lng +"00";
                    break;
                case 5:
                    lng =lng +"0";
                    break;
            } 
            arrCoord.push(lat+","+lng); 
             }        
    };
    
    
    //find higher and lower lat and lng in the polygon, converting lan e lng to number
    var maxlat=0, maxlng=0;minlat=99999999,minlng=9999999;
    var latlngStr=[];
    
    for(var i=0;i<arrCoord.length;i++) {
        latlngStr = arrCoord[i].split(',');
        console.log(latlngStr);
        lat = parseInt(latlngStr[0].replace(".", ""));
        console.log(lat);
        lng = parseInt(latlngStr[1].replace(".", ""));
            if (lat>maxlat){
                maxlat=lat;
                };
    
                if (lng>maxlng){
                maxlng=lng;
                };
    
                if (lat<minlat){
                minlat=lat;
                };
    
                if (lng<maxlng){
                minlng=lng;
                };
    
    }; 
    
    //create new polygon with that path
    var polygon = new google.maps.Polygon({
              paths: polCoords,
              strokeColor: '#FF0000',
              strokeOpacity: 0.8,
              strokeWeight: 2,
              fillColor: '#FF0000',
              fillOpacity: 0.35
            });
    
    //move from (minlan,minlng) every 100 horizontal until reach maxlan, then add 200 to lng and star again. 
    
    var coordinate;
    var newlat="",newlng="";
    for (var i=minlat;i<maxlat;i+=100){
            for (var j=minlng;j<maxlng;j+=200){
    //reconvert lat e lng from number to string
                newlat=i.toString();
                newlng=j.toString();
                newlat=newlat.substring(0,newlat.length-6) + "." +newlat.substring(newlat.length-6,6);
                newlng=newlng.substring(0,newlng.length-6) + "." +newlng.substring(newlng.length-6,6);
    
    coordinate=newlat +"," + newlng;
    //for every point find out if is inside the polygon
    var isWithinPolygon = polygon.containsLatLng(parseFloat(newlat),parseFloat(newlng));
            if(isWithinPolygon){
                newgeocodeLatLng(function(addr){
               if (arrAddress.length==0){arrAddress.push(addr);} else{
                        for (var i =0; i<arrAddress.length;i++){
                        var ok=0;
    ///if address (retrive only the street name) found from point is already in the array, don't put in it. i'm sure there is a better ay to do it....
                        if (arrAddress[i].split(',')[0]==addr.split(',')[0]){
                                    ok=1;};
                                    }; 
    
                                    if(ok==0){arrAddress.push(addr);
                                    };
                    };
             },coordinate); //end newgeocodelatlng
            }; //end  if iswithpolygon
        };//end for j
    };//end for i
    
    };
    
    
    
    function newgeocodeLatLng(callback,mycoord) {   
    var address;
     var latlngStr = mycoord.split(',');
     getReverseGeocodingData(function(addr){
         address=addr;
         callback(address);
        //  console.log("address4=" + address);
     },latlngStr[0], latlngStr[1]);
    };  
    
    
    
    function getReverseGeocodingData(callback,lat, lng) {
        var address="";
        var latlng = new google.maps.LatLng(lat, lng);
        // This is making the Geocode request
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
    
    if (status !== google.maps.GeocoderStatus.OK) {
            alert(status);
        }
        // This is checking to see if the Geoeode Status is OK before proceeding
        if (status == google.maps.GeocoderStatus.OK) {
            callback(results[0].formatted_address);
        }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 2020-01-16
      • 2014-07-03
      相关资源
      最近更新 更多