1. function writeLatLng(){
  2. if($this->owner->Lat == NULL || $this->isChanged('adres')){
  3. usleep(500000); /* wait 0,5 second per request */
  4. $Adres = $this->owner->Adres;
  5. $Plaats = $this->owner->Plaats;
  6. $Provincie = $this->owner->Provincie;
  7. $Land = $this->owner->Land;
  8. $VolledigAdres = $Adres .'+'. $Plaats .'+'. $Provincie .'+'. $Land;
  9. $zoekAdres = str_replace(" ", "+", "$VolledigAdres");
  10. $gmURL = "http://maps.googleapis.com/maps/api/geocode/json?address=$zoekAdres&sensor=false";
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, $gmURL);
  13. curl_setopt($ch, CURLOPT_HEADER, 0);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
  16. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 100000);
  17. $data = curl_exec($ch);
  18. curl_close($ch);
  19. $json = json_decode($data);
  20. echo ("$gmURL <br>");
  21. $status = $json->status;
  22. if($status == "OVER_QUERY_LIMIT"){
  23. echo('OVER_QUERY_LIMIT <br>');
  24. usleep(500000);
  25. }else if($status == "ZERO_RESULTS"){
  26. echo('ZERO_RESULTS <br>');
  27. }else{
  28. echo('saved lat&lng <br>');
  29. $Latitude = $json->results[0]->geometry->location->lat;
  30. $Longtitude = $json->results[0]->geometry->location->lng;
  31. $this->owner->Lat = $Latitude;
  32. $this->owner->Lng = $Longtitude;
  33. if($this->owner->Provincie == NULL){
  34. foreach($json->results[0]->address_components as $v){
  35. if (in_array("administrative_area_level_1",$v->types)){
  36. $this->owner->Provincie = $v->long_name;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }