Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. public function coordinates($set = false) {
  2.  
  3. $address = $this->city.' '.$this->street.' '.$this->building;
  4. $address = str_replace(' ', '+', $address);
  5. $apiKey = env('GOOGLE_MAPS_API_KEY', null);
  6. $counter = 0;
  7.  
  8. do {
  9. $results = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&key='.$apiKey));
  10.  
  11. $geo['lat'] = $results->results[0]->geometry->location->lat;
  12. $geo['lng'] = $results->results[0]->geometry->location->lng;
  13. $counter++;
  14. } while ($results->status != 'OK' && $counter <= 5);
  15.  
  16. if ($set) {
  17. $this->setAttribute('lat', $geo['lat']);
  18. $this->setAttribute('lng', $geo['lng']);
  19. }
  20.  
  21. return $geo;
  22. }
  23.  
  24. Wywołanie:
  25. $zmienna->coordinates(true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement