Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function coordinates($set = false) {
- $address = $this->city.' '.$this->street.' '.$this->building;
- $address = str_replace(' ', '+', $address);
- $apiKey = env('GOOGLE_MAPS_API_KEY', null);
- $counter = 0;
- do {
- $results = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&key='.$apiKey));
- $geo['lat'] = $results->results[0]->geometry->location->lat;
- $geo['lng'] = $results->results[0]->geometry->location->lng;
- $counter++;
- } while ($results->status != 'OK' && $counter <= 5);
- if ($set) {
- $this->setAttribute('lat', $geo['lat']);
- $this->setAttribute('lng', $geo['lng']);
- }
- return $geo;
- }
- Wywołanie:
- $zmienna->coordinates(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement