Example #1
0
 function _getCoordinates($city, $state)
 {
     $coordinates = new Coordinate();
     $coordinates->where('city', $city)->where('state', $state)->get();
     if (!$coordinates->exists()) {
         $data = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . urlencode($city) . "," . urlencode($state) . "&sensor=false");
         $json_array = json_decode($data, true);
         $lat = $json_array['results'][0]['geometry']['location']['lat'];
         $long = $json_array['results'][0]['geometry']['location']['lng'];
         $c = new Coordinate();
         $c->city = $city;
         $c->state = $state;
         $c->lat = $lat;
         $c->lng = $long;
         $c->save();
     } else {
         $lat = $coordinates->lat;
         $long = $coordinates->lng;
     }
     return array($lat, $long);
 }