Explorar o código

Convert address to Location with lat, lng and formatted_address using
google api.

Maximiliano Schvindt %!s(int64=6) %!d(string=hai) anos
pai
achega
14dcb81e0d

+ 40 - 2
src/FTTHBundle/Controller/CaptivePortalController.php

@@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Response;
 use FTTHBundle\Entity\ONU;
 use FTTHBundle\Entity\PreSale;
+use MapBundle\Entity\Location;
 use WebserviceBundle\Form\Type\RemoteClientType;
 use MapBundle\Form\Type\RemoteMapType;
 use FOS\RestBundle\Controller\Annotations\RouteResource;
@@ -48,8 +49,8 @@ class CaptivePortalController extends Controller
 
                     if($onu->getClientId() == $sale->getClientId() && $onu->getProfile() == $sale->getProfile()) {
                         $result = true;
-                        $em->remove($sale);
-                        $em->flush();
+                        //$em->remove($sale);
+                        //$em->flush();
 
                     }
                 }
@@ -67,6 +68,34 @@ class CaptivePortalController extends Controller
     }
 
     private function updateOnu($clientId, $profile, $onu, $address, $em) {
+
+        if(!is_null($address) && $this->getParameter('googlemaps_api_key')) {
+            $key = $this->getParameter('googlemaps_api_key');
+            $addr = urlencode($address);
+            $data = $this->getSslPage("https://maps.googleapis.com/maps/api/geocode/json?address={$addr}&key={$key}");
+
+            try {
+                $data = json_decode($data,true);
+            } catch(\Exception $e) {
+                $data = array();
+            }
+
+            if($data['status'] == 'OK') {
+                $geoInformation = $data['results'][0];
+                $extraData = array();
+                $extraData['formatted_address'] = $geoInformation['formatted_address'];
+                $extraData['lat'] = $geoInformation['geometry']['location']['lat'];
+                $extraData['lng'] = $geoInformation['geometry']['location']['lng'];
+                $extraData['zoom'] = 18;
+                
+                $location = new Location();
+                $location->setJsonExtraData($extraData);
+                
+                $onu->setLocation($location);
+                $onu->setJsonExtraData($extraData);
+            }
+        } 
+
         $onu->setClientId($clientId);
         $onu->setProfile($profile);
 
@@ -74,4 +103,13 @@ class CaptivePortalController extends Controller
         $em->flush();
     }
 
+    function getSslPage($url) {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        $result = curl_exec($ch);
+        curl_close($ch);
+        return $result;
+    }
+
 }

+ 4 - 0
src/FTTHBundle/Resources/public/captive_portal/js/main.js

@@ -62,6 +62,10 @@
     $('#reload').on('click', function() {
         window.location = window.location.pathname;
     });
+
+    $(".radioPreSale").on('click', function() {
+        console.log($(this).attr('data-address'));
+    });
     
     
 

+ 2 - 2
src/FTTHBundle/Resources/views/Portal/index.html.twig

@@ -78,7 +78,7 @@
                     {% endif %}
 					
                     {% if preSales|length > 0 %}
-                    <div class="wrap-input2 validate-input" style="border-bottom: none" data-validate = "Select one option">
+                    <div class="wrap-input2 validate-input" style="border-bottom: none; font-size:12px" data-validate = "Select one option">
                         <table class="table">
                             <thead>
                                 </tr>
@@ -91,7 +91,7 @@
                             <tbody>
                                 {% for preSale in preSales %}
                                     <tr>
-                                        <td><input type="radio" name="preSale" value="{{preSale.getId()}}"></td>
+                                        <td><input type="radio" name="preSale" value="{{preSale.getId()}}" class="radioPreSale" data-address="{{preSale.getAddress()}}"></td>
                                         <td>{{preSale.getProfile().getName()}}</td>
                                         <td>{{preSale.getAddress()}}</td>
                                         <td>{{preSale.getDate()|date('Y-m-d')}}</td>