GeoDecode.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace MapBundle\Util;
  3. class GeoDecode
  4. {
  5. /**
  6. * Calcula la distancia cartografica (NO TOPOGRAFICA) entre 2 coordenadas.
  7. * @param $lat1
  8. * @param $lng1
  9. * @param $lat2
  10. * @param $lng2
  11. * @param bool $km Me dice si quiero la distancia en kilometros. Por defecto esta en metros.
  12. * @return float|int Retorna la distancia en metros
  13. */
  14. public function distanceGeoPoints($lat1, $lng1, $lat2, $lng2, $km = false)
  15. {
  16. // curvatura de la tierra
  17. $earthRadius = 3958.75;
  18. $dLat = deg2rad($lat2 - $lat1);
  19. $dLng = deg2rad($lng2 - $lng1);
  20. $a = sin($dLat / 2) * sin($dLat / 2) +
  21. cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
  22. sin($dLng / 2) * sin($dLng / 2);
  23. $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
  24. $dist = $earthRadius * $c;
  25. // from miles
  26. $meterConversion = 1609;
  27. $geopointDistance = $dist * $meterConversion;
  28. return round(($km ? ($geopointDistance / 1000) : $geopointDistance), 3, PHP_ROUND_HALF_UP);
  29. }
  30. /**
  31. * Busca la latitud y longitud de acuerdo a la direccion que se pasa como parametro.
  32. * @param string $address Contiene la direccion.
  33. * @param string $city Contiene la ciudad.
  34. * @param string $state Contiene la provincia.
  35. * @param string $country Contiene el pais.
  36. * @param string $region Contiene el codigo de la region.
  37. * @return array Retorna un array con la latitud y longitud.
  38. */
  39. public function searchLatLng($address, $city, $state, $country, $region)
  40. {
  41. $url = "http://maps.google.com/maps/api/geocode/json?";
  42. $add = "address=" . $address . "," . $city . "," . $state . "," . $country;
  43. $add .= "&components=country:" . $region;
  44. $arrAddress = explode(" ", $add);
  45. $address = implode("+", $arrAddress);
  46. $url .= $address;
  47. $nc = 0;
  48. while ($nc < 5) {
  49. $json = $this->curl($url);
  50. if (strtoupper($json['status']) == 'OVER_QUERY_LIMIT') {
  51. sleep(1);
  52. } else if (strtoupper($json['status']) == 'ZERO_RESULTS') {
  53. break;
  54. } else if (strtoupper($json['status']) == 'OK') {
  55. break;
  56. }
  57. $nc++;
  58. }
  59. var_dump($json);
  60. // -32.030232, -61.220883
  61. if (isset($json['results']) &&
  62. isset($json['results'][0]) &&
  63. isset($json['results'][0]['geometry']) &&
  64. isset($json['results'][0]['geometry']['location']) &&
  65. isset($json['results'][0]['geometry']['location']['lat']) &&
  66. isset($json['results'][0]['geometry']['location']['lng'])
  67. ) {
  68. return ['address' => $address,
  69. 'lat' => $json['results'][0]['geometry']['location']['lat'],
  70. 'lng' => $json['results'][0]['geometry']['location']['lng']];
  71. } else {
  72. return null;
  73. }
  74. }
  75. /**
  76. * @param string $url Contiene la url a buscar.
  77. * @return mixed Retorna un array con los datos del json.
  78. */
  79. private function curl($url)
  80. {
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  83. curl_setopt($ch, CURLOPT_URL, $url);
  84. $result = curl_exec($ch);
  85. return json_decode($result, true);
  86. }
  87. }