Browse Source

Agregado de la clase para calcular distancias entre 2 coordenadas

Tambien obtiene las coordenadas de una direccion
gabriel 7 years ago
parent
commit
6da8c35487
1 changed files with 97 additions and 0 deletions
  1. 97 0
      Util/GeoDecode.php

+ 97 - 0
Util/GeoDecode.php

@@ -0,0 +1,97 @@
+<?php
+
+namespace MapBundle\Util;
+
+class GeoDecode
+{
+    /**
+     * Calcula la distancia cartografica (NO TOPOGRAFICA) entre 2 coordenadas.
+     * @param $lat1
+     * @param $lng1
+     * @param $lat2
+     * @param $lng2
+     * @param bool $km Me dice si quiero la distancia en kilometros. Por defecto esta en metros.
+     * @return float|int Retorna la distancia en metros
+     */
+    public function distanceGeoPoints($lat1, $lng1, $lat2, $lng2, $km = false)
+    {
+        // curvatura de la tierra
+        $earthRadius = 3958.75;
+
+        $dLat = deg2rad($lat2 - $lat1);
+        $dLng = deg2rad($lng2 - $lng1);
+
+        $a = sin($dLat / 2) * sin($dLat / 2) +
+            cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
+            sin($dLng / 2) * sin($dLng / 2);
+        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
+        $dist = $earthRadius * $c;
+
+        // from miles
+        $meterConversion = 1609;
+        $geopointDistance = $dist * $meterConversion;
+
+        return round(($km ? ($geopointDistance / 1000) : $geopointDistance), 3, PHP_ROUND_HALF_UP);
+    }
+
+    /**
+     * Busca la latitud y longitud de acuerdo a la direccion que se pasa como parametro.
+     * @param string $address Contiene la direccion.
+     * @param string $city Contiene la ciudad.
+     * @param string $state Contiene la  provincia.
+     * @param string $country Contiene el pais.
+     * @param string $region Contiene el codigo de la region.
+     * @return array Retorna un array con la latitud y longitud.
+     */
+    public function searchLatLng($address, $city, $state, $country, $region)
+    {
+
+        $url = "http://maps.google.com/maps/api/geocode/json?";
+        $add = "address=" . $address . "," . $city . "," . $state . "," . $country;
+        $add .= "&components=country:" . $region;
+        $arrAddress = explode(" ", $add);
+        $address = implode("+", $arrAddress);
+        $url .= $address;
+        $nc = 0;
+        while ($nc < 5) {
+            $json = $this->curl($url);
+            if (strtoupper($json['status']) == 'OVER_QUERY_LIMIT') {
+                sleep(1);
+            } else if (strtoupper($json['status']) == 'ZERO_RESULTS') {
+                break;
+            } else if (strtoupper($json['status']) == 'OK') {
+                break;
+            }
+            $nc++;
+        }
+        var_dump($json);
+        //        -32.030232, -61.220883
+        if (isset($json['results']) &&
+            isset($json['results'][0]) &&
+            isset($json['results'][0]['geometry']) &&
+            isset($json['results'][0]['geometry']['location']) &&
+            isset($json['results'][0]['geometry']['location']['lat']) &&
+            isset($json['results'][0]['geometry']['location']['lng'])
+        ) {
+            return ['address' => $address,
+                'lat' => $json['results'][0]['geometry']['location']['lat'],
+                'lng' => $json['results'][0]['geometry']['location']['lng']];
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * @param string $url Contiene la url a buscar.
+     * @return mixed Retorna un array con los datos del json.
+     */
+    private function curl($url)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_URL, $url);
+        $result = curl_exec($ch);
+
+        return json_decode($result, true);
+    }
+}