GeoDecode.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (isset($json['results']) &&
  60. isset($json['results'][0]) &&
  61. isset($json['results'][0]['geometry']) &&
  62. isset($json['results'][0]['geometry']['location']) &&
  63. isset($json['results'][0]['geometry']['location']['lat']) &&
  64. isset($json['results'][0]['geometry']['location']['lng'])
  65. ) {
  66. return ['address' => $address,
  67. 'lat' => $json['results'][0]['geometry']['location']['lat'],
  68. 'lng' => $json['results'][0]['geometry']['location']['lng']];
  69. } else {
  70. return null;
  71. }
  72. }
  73. /**
  74. * @param string $url Contiene la url a buscar.
  75. * @return mixed Retorna un array con los datos del json.
  76. */
  77. private function curl($url)
  78. {
  79. $ch = curl_init();
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  81. curl_setopt($ch, CURLOPT_URL, $url);
  82. $result = curl_exec($ch);
  83. return json_decode($result, true);
  84. }
  85. }