0

I was looking for a way to retrive all the addresses inside a polygon in a map (google map?). I saw in this post that someone said that is not possible.. I ask this: if is possible to retrive the address from a point on the map, why is not possible to do the same in a polygon? Can't you just: - set an hypoteticl grid in the area of the polygon - for every intersection point of the grid retrive the location address - store the address (if is already stored delete it).

Wouln't this method be workable? thanks Francesco

Fravarese
  • 1
  • 2

1 Answers1

0

Looking around I'm almost find a way to do that, merging up various methods.. I'm new in js, so my code is probably full of dumb stuff.. Anyway, I reached a point where I managed to retrive my unique addresses into an array, but can't complete the job beacuse I always get the error of OVER_QUERY_LIMIT, beacuse I send too many requests as explainedin this page

So I think there is no way to do this, unless you want to spend a lot in buying the billing to unlock higher quotas, and this will be a very hard hit beacuse a method like this will require thousands of request each call. Anyway, this is the code, inserted into the one found here , wich I got from Polygon Drawing and Getting Coordinates with Google Map API v3 Sorry again for the various newbie stuff you will find in it.

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);
    }
    });
}
Fravarese
  • 1
  • 2