Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/09/2011, 12:20
Avatar de morfasto
morfasto
 
Fecha de Ingreso: julio-2011
Ubicación: Lima
Mensajes: 291
Antigüedad: 13 años, 6 meses
Puntos: 8
Gmap3 y Json (como unirlos?)

Hola, que tal?

Tengo una pagina web en la que se genera una ruta de google maps y se guarda en una base de datos. El problema es que la ruta se genera escribiendo el inicio y el destino y luego dando un click en generar ruta. Lo que necesito es que la ruta se genere haciendo click derecho en el mapa en el inico y fin de la ruta y que despues se genere sola.

Tengo 2 scripts, uno que genera la ruta escribiendo el inicio y el destino y que la guarda y otro script que genera la ruta haciendo click en el mapa, pero no la guarda.

Lo que necesito es hacer que el script que genera la ruta dando click en el mapa pueda gardarla en la base de datos para despues ser vista por otro usuario.

Este es el script que hace que se genere la ruta dando click derecho en el mapa:

Código Javascript:
Ver original
  1. <html>    
  2.   <head>
  3.     <script type="text/javascript" src="jquery-1.6.1.min.js"></script>        
  4.     <script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
  5.     <script type="text/javascript" src="gmap3.min.js"></script>
  6.     <style>
  7.       #container{
  8.         position:relative;
  9.         height:700px;
  10.       }
  11.       #directions{
  12.         position:absolute;
  13.         width: 23%;
  14.         right:1%;
  15.         height: 690px;
  16.         overflow:auto;
  17.       }
  18.       #googleMap{
  19.         border: 1px dashed #C0C0C0;
  20.         width: 75%;
  21.         height: 700px;
  22.       }
  23.       #menu{
  24.         background-color: #FFFFFF;
  25.         width:170px;
  26.         height:60px;
  27.         padding:0px;
  28.         border:1px;
  29.         cursor:pointer;
  30.         border-left:1px solid #cccccc;
  31.         border-top:1px solid #cccccc;
  32.         border-right:1px solid #676767;
  33.         border-bottom:1px solid #676767;
  34.       }
  35.       #menu .item{
  36.         font-family: arial,helvetica,sans-serif;
  37.         font-size: 12px;
  38.         text-align:left;
  39.         line-height: 30px;
  40.         border-left:0px;
  41.         border-top:0px;
  42.         border-right:0px;
  43.         padding-left:30px;
  44.         background-repeat: no-repeat;
  45.         background-position: 4px center;
  46.       }
  47.       #menu .item.itemA{
  48.         background-image: url(images/icon_greenA.png);
  49.       }
  50.       #menu .item.itemB{
  51.         background-image: url(images/icon_greenB.png);
  52.       }
  53.       #menu .item.hover{
  54.         background-color: #d6e9f8;
  55.       }
  56.       #menu .item.separator{
  57.         border-bottom:1px solid #cccccc;
  58.       }
  59.     </style>
  60.    
  61.     <script type="text/javascript">
  62.    
  63.       /**************************************************
  64.        *                      Menu
  65.        **************************************************/
  66.        
  67.       function Menu($div){
  68.         var that = this,
  69.             ts = null;
  70.        
  71.         this.$div = $div;
  72.         this.items = [];
  73.        
  74.         // create an item using a new closure
  75.         this.create = function(item){
  76.           var $item = $('<div class="item '+item.cl+'">'+item.label+'</div>');
  77.           $item
  78.             // bind click on item
  79.             .click(function(){
  80.               if (typeof(item.fnc) === 'function'){
  81.                 item.fnc.apply($(this), []);
  82.               }
  83.             })
  84.             // manage mouse over coloration
  85.             .hover(
  86.               function(){$(this).addClass('hover');},
  87.               function(){$(this).removeClass('hover');}
  88.             );
  89.           return $item;
  90.         };
  91.         this.clearTs = function(){
  92.           if (ts){
  93.             clearTimeout(ts);
  94.             ts = null;
  95.           }
  96.         };
  97.         this.initTs = function(t){
  98.           ts = setTimeout(function(){that.close()}, t);
  99.         };
  100.       }
  101.      
  102.       // add item
  103.       Menu.prototype.add = function(label, cl, fnc){
  104.         this.items.push({
  105.           label:label,
  106.           fnc:fnc,
  107.           cl:cl
  108.         });
  109.       }
  110.      
  111.       // close previous and open a new menu
  112.       Menu.prototype.open = function(event){
  113.         this.close();
  114.         var k,
  115.             that = this,
  116.             offset = {
  117.               x:0,
  118.               y:0
  119.             },
  120.             $menu = $('<div id="menu"></div>');
  121.            
  122.         // add items in menu
  123.         for(k in this.items){
  124.           $menu.append(this.create(this.items[k]));
  125.         }
  126.        
  127.         // manage auto-close menu on mouse hover / out
  128.         $menu.hover(
  129.           function(){that.clearTs();},
  130.           function(){that.initTs(3000);}
  131.         );
  132.        
  133.         // change the offset to get the menu visible (#menu width & height must be defined in CSS to use this simple code)
  134.         if ( event.pixel.y + $menu.height() > this.$div.height()){
  135.           offset.y = -$menu.height();
  136.         }
  137.         if ( event.pixel.x + $menu.width() > this.$div.width()){
  138.           offset.x = -$menu.width();
  139.         }
  140.        
  141.         // use menu as overlay
  142.         this.$div.gmap3({
  143.           action:'addOverlay',
  144.           latLng: event.latLng,
  145.           content: $menu,
  146.           offset: offset
  147.         });
  148.        
  149.         // start auto-close
  150.         this.initTs(5000);
  151.       }
  152.      
  153.       // close the menu
  154.       Menu.prototype.close = function(){
  155.         this.clearTs();
  156.         this.$div.gmap3({action:'clear', name:'overlay'});
  157.       }
  158.      
  159.      
  160.       /**************************************************
  161.        *                      Main
  162.        **************************************************/
  163.    
  164.       $(function(){
  165.      
  166.         var $map = $('#googleMap'),
  167.             menu = new Menu($map),
  168.            
  169.             current,  // current click event (used to save as start / end position)
  170.             m1,       // marker "from"
  171.             m2,       // marker "to"
  172.             center = [48.85861640881589, 2.3459243774414062];
  173.        
  174.         // update marker
  175.         function updateMarker(marker, isM1){
  176.           if (isM1){
  177.             m1 = marker;
  178.           } else {
  179.             m2 = marker;
  180.           }
  181.           updateDirections();
  182.         }
  183.        
  184.         // add marker and manage which one it is (A, B)
  185.         function addMarker(isM1){
  186.           // clear previous marker if set
  187.           var clear = {action:'clear', name:'marker', tag:''};
  188.           if (isM1 && m1) {
  189.             clear.tag = 'from';
  190.             $map.gmap3(clear);
  191.           } else if (!isM1 && m2){
  192.             clear.tag = 'to';
  193.             $map.gmap3(clear);
  194.           }
  195.           // add marker and store it
  196.           $map.gmap3({
  197.             action:'addMarker',
  198.             latLng:current.latLng,
  199.             options:{
  200.               draggable:true,
  201.               icon:new google.maps.MarkerImage('http://maps.gstatic.com/mapfiles/icon_green' + (isM1 ? 'A' : 'B') + '.png')
  202.             },
  203.             tag: (isM1 ? 'from' : 'to'),
  204.             events: {
  205.               dragend: function(marker){
  206.                 updateMarker(marker, isM1);
  207.               }
  208.             },
  209.             callback: function(marker){
  210.               updateMarker(marker, isM1);
  211.             }
  212.           });
  213.         }
  214.        
  215.         // function called to update direction is m1 and m2 are set
  216.         function updateDirections(){
  217.           if (!(m1 && m2)){
  218.             return;
  219.           }
  220.           $map.gmap3({
  221.             action:'getRoute',
  222.             options:{
  223.               origin:m1.getPosition(),
  224.               destination:m2.getPosition(),
  225.               travelMode: google.maps.DirectionsTravelMode.DRIVING
  226.             },
  227.             callback: function(results){
  228.               if (!results) return;
  229.               $map.gmap3({ action: 'setDirections', directions:results});
  230.             }
  231.           });
  232.         }
  233.        
  234.         // MENU : ITEM 1
  235.         menu.add('Direction to here', 'itemB',
  236.           function(){
  237.             menu.close();
  238.             addMarker(false);
  239.           });
  240.        
  241.         // MENU : ITEM 2
  242.         menu.add('Direction from here', 'itemA separator',
  243.           function(){
  244.             menu.close();
  245.             addMarker(true);
  246.           })
  247.        
  248.        
  249.        
  250.         // INITIALIZE GOOGLE MAP
  251.         $map.gmap3(
  252.           { action: 'init',
  253.             options:{
  254.               center:center,
  255.               zoom: 5
  256.             },
  257.             events:{
  258.               rightclick:function(map, event){
  259.                 current = event;
  260.                 menu.open(current);
  261.               },
  262.               click: function(){
  263.                 menu.close();
  264.               },
  265.               dragstart: function(){
  266.                 menu.close();
  267.               },
  268.               zoom_changed: function(){
  269.                 menu.close();
  270.               }
  271.             }
  272.           },
  273.           // add direction renderer to configure options (else, automatically created with default options)
  274.           { action:'addDirectionsRenderer',
  275.             preserveViewport: true,
  276.             draggable: true,
  277.             markerOptions:{
  278.               visible: false
  279.             }
  280.           },
  281.           // add a direction panel
  282.           { action:'setDirectionsPanel',
  283.             id : ''
  284.           }
  285.         );
  286.       });
  287.     </script>  
  288.  
  289.   </head>
  290.    
  291.   <body>
  292.     <div id="container">
  293.       <div id="directions"></div>
  294.       <div id="googleMap"></div>
  295.      
  296.     </div>
  297.   </body>
  298. </html>

El codigo lo obtuve de [URL="http://gmap3.net/examples/context-menu.html"]aqui[/URL]