4

so for a project in school I am trying to simulate where students live in our town since official data is not available obviously due to privacy concerns. I started looking for a generator that works by zip code, radius or county but I haven't been able to find any (commercially or free) I would love it to be free, but I might be able to secure some funding for a license. If I find random generators, they cannot be limited to a ZIP code or city to produce real addresses randomly.

A good idea I found was here: https://stackoverflow.com/a/12289969/1778542

Based on that I would pick the city center's long\lat coordinates, find out the outskirts coordinates to create a plane, then randomly generate long\lat coordinates within the plane, feed them back in to have Google approximate the addresses for it. One concern that was raised (and I try to avoid) is that Google doesn't use verified addresses, rather approximations.

Does anyone have a hint where to find such a generator or a sleeker way to use GMaps?

Thanks a million!

GP

Community
  • 1
  • 1
Phill
  • 525
  • 6
  • 15

1 Answers1

0

I use this code in one of my Laravel Seeder it gets a random street name in Romania provided that you give it the location area and Town, It works by getting the latitude and longitude for that area and then randomly adds a radius of 2 Kilometers, after that it makes another request to google api, and from that it extracts a random street name.

I don't know if this will help you, adjusting this code can generate a real address provided that you give a first good location to look;

Here is the code:

 protected function getRandomStreetNameFromCity($judet, $city){
        $kmRange = 2;
        $initalLocation = [];
        $randomLocation= [];
        $randomKmval = mt_rand(1, $kmRange) / mt_getrandmax();

        // Poor Man Lat and Lng 
        //Latitude: 1 deg = 110.574 km
        //Longitude: 1 deg = 111.320*cos(latitude) km

                $guzzelCl = new Client();
        $guzelReq = $guzzelCl->request('GET', 'http://maps.googleapis.com/maps/api/geocode/json?address=Romania,'.$judet.','.$city.'&sensor=false', [
            'verify' => false,   
        ]);



        if($guzelReq->getStatusCode() == 200){
           $arrJson = json_decode($guzelReq->getBody(), true);
            while (count($arrJson['results']) <= 0){
            $judet= $this->getNewJudet();
            $city = $this->getNewOras();
            $guzelReq = $guzzelCl->request('GET', 'http://maps.googleapis.com/maps/api/geocode/json?address=Romania,'.$judet.','.$city.'&sensor=false', [
            'verify' => false,   
        ]);
         $arrJson = json_decode($guzelReq->getBody(), true);      
           }


           $initalLocation = $arrJson['results'][0]['geometry']['location'];
           }



        $plusMinus = $this->generateRandomString(1);


        $randomExp = [ 1 => $tempLat = eval("return (1 / (110.574 ".$plusMinus." ".$randomKmval." )+ ".$initalLocation['lat']." );"),
            2 => eval('return ('.$initalLocation['lng'].' '.$plusMinus.' 1/111.320*cos($tempLat));'),
            ];

        $guzelReq = $guzzelCl->request('GET', 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$randomExp[1].','.$randomExp[2], [
            'verify' => false,   
        ]);
        return explode(',', json_decode($guzelReq->getBody(), true)['results'][0]['formatted_address'])[0];



 }   


 protected function getNewJudet(){
     //This is a administrative type of location named 'judet' Romania is divided in a number bellow 50 of this  
     return array_rand($this->judetOras, 1);
 }

 protected function getNewOras(){
     //This is a Town String
     return $this->judetOras[$iterateJud = array_rand($this->judetOras, 1)][array_rand($this->judetOras[$iterateJud], 1)];
 }


 protected function generateRandomString($length = 10) {
    $characters = '-+';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}   
Andrei O.
  • 116
  • 1
  • 1
  • 5