瀏覽代碼

Se agrega funcionalidad y vista para crear elementos.

Maximiliano Schvindt 8 年之前
父節點
當前提交
ea65fa1c01

+ 22 - 1
Controller/LeafletController.php

@@ -15,13 +15,34 @@ class LeafletController extends Controller
     {
 
         $adminPool = $this->get('sonata.admin.pool');
+        $uid = time();
 
         return $this->render('LeafletBundle:Leaflet:create_vector.html.twig', array(
             'base_template' => $adminPool->getTemplate('layout'),
             'admin_pool' => $adminPool,
-            'admin' => $adminPool->getAdminByClass("MapBundle\Entity\Map")
+            'admin' => $adminPool->getAdminByClass("MapBundle\Entity\Map"),
+            'uid' => $uid
             ));
 
 
+    }
+
+    public function saveVectorAction(Request $request)
+    {
+        print_r($request->request->all());
+
+        die;
+
+        // $adminPool = $this->get('sonata.admin.pool');
+        // $uid = time();
+
+        // return $this->render('LeafletBundle:Leaflet:create_vector.html.twig', array(
+        //     'base_template' => $adminPool->getTemplate('layout'),
+        //     'admin_pool' => $adminPool,
+        //     'admin' => $adminPool->getAdminByClass("MapBundle\Entity\Map"),
+        //     'uid' => $uid
+        //     ));
+
+
     }
 }

+ 4 - 0
Resources/config/routing/admin.xml

@@ -7,4 +7,8 @@
     <route id="admin_leaflet_vector_create" path="/admin/leaflet/vector/create">
         <default key="_controller">LeafletBundle:Leaflet:createVector</default>
     </route>
+    
+    <route id="admin_leaflet_vector_save" path="/admin/leaflet/vector/save">
+        <default key="_controller">LeafletBundle:Leaflet:saveVector</default>
+    </route>
 </routes>

File diff suppressed because it is too large
+ 7501 - 7501
Resources/public/leaflet/leaflet-src.js


+ 73 - 20
Resources/views/Leaflet/create_vector.html.twig

@@ -1,7 +1,3 @@
-{#{% extends "@BaseAdminBundle/Resources/views/standard_layout.html.twig" %}#}
-
-
-
 {% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
 
 {% block actions %}
@@ -16,14 +12,21 @@
 {% include 'LeafletBundle:Leaflet:resources.html.twig' %}
 
 <script type="text/javascript">
-
-    var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
-        osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors';
-        osm = L.tileLayer(osmUrl, { maxZoom: 18, attribution: osmAttrib }),
-        
-        map = new L.Map('map', { center: new L.LatLng(51.505, -0.04), zoom: 13 }),
-        drawnItems = L.featureGroup().addTo(map);
-
+    
+    window.SONATA_CONFIG.USE_ICHECK = false;
+    
+    var elements = new Array();
+    var uid = {{uid}};
+    var layer_id = 1;
+    var element_id = 1;
+
+    $( document ).ready(function() {});
+
+    var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
+            osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
+            osm = L.tileLayer(osmUrl, { maxZoom: 18, attribution: osmAttrib }),
+            map = new L.Map('map', { center: new L.LatLng(51.505, -0.04), zoom: 13 }),
+            drawnItems = L.featureGroup().addTo(map);
     L.control.layers({
         'osm': osm.addTo(map),
         "google": L.tileLayer('http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}', {
@@ -47,15 +50,63 @@
     }));
 
     map.on(L.Draw.Event.CREATED, function (event) {
+        
         var layer = event.layer;
-        console.log(event);
-        console.log(drawnItems);
-
+        
+        layer.layerType = event.layerType;
         drawnItems.addLayer(layer);
+        
+        
+        drawnItems.eachLayer(function(layer) {
+            layer._mi_id = uid + "_" + layer._leaflet_id;
+            
+        });
+        
+    });
+    
+    map.on(L.Draw.Event.DELETED, function (event) {
+
+        var layers = event.layers;
+        layers.eachLayer(function(layer) {
+            console.log("DELETE: " + layer._mi_id + "=>" + layer._leaflet_id);
+        });
+
+       
+    });
+
+function saveData()  {
+    var vectorData = new Array();
+    drawnItems.eachLayer(function(layer) {
+
+        if(layer.layerType == "marker") {
+            data = [layer._latlng];
+        } else if (layer.layerType == "circle") {
+            data = [layer._latlng, layer._mRadius];
+        } else if (layer.layerType == "polyline") {
+            data = [layer._latlngs];
+        } else {
+            data = [layer._latlngs[0]];
+        }
 
-        var data = drawnItems.toGeoJSON();
-        console.log(data);
+        object = {'_mi_id': layer._mi_id, 'layer_type': layer.layerType, 'data': data, 'layer_id': layer_id};
+        vectorData.push(object);
+    });
+
+    console.log(vectorData);
+    sendData = {'layerId' : layer_id, 'vectors' : vectorData};
+    var json = JSON.stringify(sendData); 
+
+    $.ajax({
+        type: "POST",
+        url: "{{ url ('admin_leaflet_vector_save') }}",
+        data: json,
+        contentType: "application/json; charset=utf-8",
+        dataType: "json",
+        success: function(msg) {
+            console.log(msg);
+        }
     });
+}
 
 </script>
 
@@ -70,10 +121,12 @@
                         <h3 class="box-title">Create Vector</h3>
                     </div>
                     <div class="box-body">
-                    {#<div class="clearfix" style="width:100%; margin-top:50px">#}
                         <div id="map" style="margin:0 auto; width: 80%; height: 600px; border: 1px solid #ccc"></div>
-                    {#</div>#}
-                        {#MAP#}
+                        <br />
+                        <button onclick="saveData()" class="btn btn-block btn-primary" type="button">Guardar vectores</button>
+                        <br />
+                        <div id="datamaps">
+                        </div>
                     </div>
                 </div>
             </div>