浏览代码

Merge branch 'mapas#2' into 'master'

Mapas#2

See merge request interlink-sa/flowdat3/vendors/leafletbundle!2
Jean Sumara Leopoldo 4 年之前
父节点
当前提交
2cb3b11867
共有 4 个文件被更改,包括 82 次插入19 次删除
  1. 1 0
      .gitignore
  2. 39 0
      Controller/GeocodeController.php
  3. 3 0
      Resources/config/routing.yml
  4. 39 19
      Resources/public/js/leaflet-map-widget.js

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.idea

+ 39 - 0
Controller/GeocodeController.php

@@ -0,0 +1,39 @@
+<?php
+
+
+namespace LeafletBundle\Controller;
+
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Request;
+
+class GeocodeController extends Controller
+{
+    /**
+     * @param Request $request
+     * @return JsonResponse
+     */
+    public function getGeocodeAction(Request $request)
+    {
+        $address = $request->query->get('address', '');
+        $latLng = $request->query->get('latLng', '');
+        $keyGoogleApi = "AIzaSyDYPe_h1T_5ThFNZukx05FJY21IAejj_LA";
+        $url = "https://maps.googleapis.com/maps/api/geocode/json?key={$keyGoogleApi}";
+
+        if(isset($latLng) && $latLng != null){
+            $url = $url."&latlng={$latLng}";
+        }else{
+            $newAddress = str_replace(' ', '+', $address);
+            $url = $url."&address={$newAddress}";
+        }
+
+        $cookies = $this->get('request_stack')->getMasterRequest()->cookies->all();
+        $result = json_decode($this->get('webservice')->makeGetRequest($url, 'GET', [], [], $cookies));
+
+        $response = new JsonResponse();
+        $response->setData($result);
+
+        return $response;
+    }
+}

+ 3 - 0
Resources/config/routing.yml

@@ -0,0 +1,3 @@
+api_geocode:
+  path:      /api/geocode
+  defaults:  { _controller: LeafletBundle:Geocode:getGeocode }

+ 39 - 19
Resources/public/js/leaflet-map-widget.js

@@ -6,7 +6,7 @@ $(document).ready(function () {
     geocoder = new google.maps.Geocoder();
     geocoder = new google.maps.Geocoder();
 
 
     map.addControl(new L.Control.Search({
     map.addControl(new L.Control.Search({
-        sourceData: googleGeocoding,
+        sourceData: apiGoogleGeocoding,
         formatData: formatJSON,
         formatData: formatJSON,
         markerLocation: true,
         markerLocation: true,
         autoType: true,
         autoType: true,
@@ -78,17 +78,30 @@ function setDataValue() {
     }
     }
 }
 }
 
 
-function googleGeocoding(text, callResponse) {
-    geocoder.geocode({address: text}, callResponse);
+function apiGoogleGeocoding(text, callResponse){
+    $.ajax({
+        type: "GET",
+        data: {
+            address: text
+        },
+        url: '/api/geocode',
+        success: function (retorno){
+            callResponse(retorno);
+        },
+        dataType: "json"
+    });
 }
 }
 
 
 function formatJSON(rawjson) {
 function formatJSON(rawjson) {
     var json = {},
     var json = {},
         key;
         key;
-    for (var i in rawjson) {
-        key = rawjson[i].formatted_address;
-        loc = new L.latLng(rawjson[i].geometry.location.lat(), rawjson[i].geometry.location.lng());
-        json[key] = loc;
+
+    if(rawjson != null && rawjson.results){
+        for (var i in rawjson.results) {
+            key = rawjson.results[i].formatted_address;
+            loc = new L.latLng(rawjson.results[i].geometry.location.lat, rawjson.results[i].geometry.location.lng);
+            json[key] = loc;
+        }
     }
     }
 
 
     return json;
     return json;
@@ -101,18 +114,25 @@ function formatJSON(rawjson) {
  * @param callback Funcion de callback el primer parametro contiene el status y el segundo un objeto json.
  * @param callback Funcion de callback el primer parametro contiene el status y el segundo un objeto json.
  */
  */
 function geocodeLatLng(lat, lng, callback) {
 function geocodeLatLng(lat, lng, callback) {
-    var latlng = {lat: lat, lng: lng};
-    geocoder.geocode({'location': latlng}, function (results, status) {
-        if (status == google.maps.GeocoderStatus.OK) {
-            if (results[0]) {
-                callback(true, {
-                    complete: results[0].formatted_address
-                });
-            } else {
-                callback(false, status.status);
+    $.ajax({
+        type: "GET",
+        data: {
+            latLng: lat + "," + lng
+        },
+        url: '/api/geocode',
+        success: function (result){
+            if(result.status == google.maps.GeocoderStatus.OK){
+                if(result.results[0]){
+                    callback(true, {
+                        complete: result.results[0].formatted_address
+                    });
+                }else{
+                    callback(false, result.status);
+                }
+            }else{
+                callback(false, result.status);
             }
             }
-        } else {
-            callback(false, status.status);
-        }
+        },
+        dataType: "json"
     });
     });
 }
 }