elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  google maps añadiendo campo de busqueda..
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: google maps añadiendo campo de busqueda..  (Leído 2,034 veces)
Kase


Desconectado Desconectado

Mensajes: 1.288


programa bonito ¬¬


Ver Perfil WWW
google maps añadiendo campo de busqueda..
« en: 12 Agosto 2012, 12:55 pm »

tengo este codigo jquery

Citar
django.jQuery(function($){
   $.fn.gmapAdmin = function(options){
      var defaults = {   
         'zoom':8,   
         'lat':'53.311',
         'lng':'-6.24',
         'map_elem' : '#id_map',
         'delete_elem' : '#id_delete',
      };
      
      var options = $.extend(defaults, options);
      var value = null;
      var marker = null;
      var map = null;
      var latlng = null;
      var that = this;
      
      var set_field = function(latlng){
          if(marker){
              $(that).val(latlng.lat()+","+latlng.lng());
          }
      };
      
      var get_field = function(){
          if($(that).val()){
             return $(that).val().split(",");
          }else{
             return "";
          }
      };
      
      var remove_field = function(){
          if(marker!==""){
             $(that).removeAttr("value");
          }
      };
      
      var remove_marker = function(){
          marker.setMap(null);
          marker = null;
      };
      
      var set_marker = function(latlng){
          if(!marker){
              marker = new google.maps.Marker({
                map:map,
                draggable:true,
                position: latlng,
             });
             google.maps.event.addListener(marker, 'click', function(new_location) {
                 map.setZoom(13);
                map.setCenter(new_location.latLng);
             });
             google.maps.event.addListener(marker, 'dragend', function(new_location) {
                set_field(new_location.latLng);
             });
          }else{
              marker.setPosition(latlng)
          }
      };   
      
      latlng = new google.maps.LatLng(options.lat,options.lng);
        map = new google.maps.Map(document.getElementById($(options.map_elem).attr('id')),{
         zoom: options.zoom,
         center : latlng,
         mapTypeId: google.maps.MapTypeId.ROADMAP,
      });

      // If there is a value in the field, load it onto the map
      if(get_field()!==""){
         set_marker(new google.maps.LatLng(get_field()[0],get_field()[1]));
      }
      /* Listeners */
      google.maps.event.addListener(map, 'rightclick', function(new_location) {
          set_marker(new_location.latLng);
         set_field(new_location.latLng);
      });
      $(options.delete_elem).click(function(){
         if(marker!==""){
            remove_marker();
            remove_field();
         }
      });
      
   };
});


y este html y script que ejecutan el mapa
Citar
               <input type="hidden" name="geolocation" id="id_geolocation" />
               <div class="map_canvas_wrapper" style="display:inline-block;">
                  <div id="map_id_geolocation" style="width:800px;height:500px"></div>
                  <script>
                     django.jQuery(function($) {
                        $("#id_geolocation").gmapAdmin({
                              'zoom': 5,
                              'lat': '22.213709',
                              'lng': '-98.381109',
                              'map_elem': '#map_id_geolocation',
                              'delete_elem': '#map_delete_id_geolocation',
                        });
                     });
                  </script>
               </div>

lo que me gustaria hacer es añadir una funcion:


Citar
<input type="text" id="address" value=""/>
<a href='javascript:search();'>buscar</a>

donde search  recargue el mapa buscando la direccion pasada...

tengo 2 problemas, el primero es que dependo forzosamente de ese jquery que puse al principio
y el segundo es que no se leerlo.. XD

creo que me quede en la v2 de google maps.. donde esta el geocoder.geocode()?  x__x  no identifico donde se esta cargando el mapa ¬¬  olvidenlo, geocoder es para otra cosa.. XD

edit:
ya encontre donde se crea el map... XD lo marke en rojo

y esta es la funcion que necesito añadir

Citar
   geocoder = new google.maps.Geocoder();

  function codeAddress() {
    //In this case it gets the address from an element on the page, but obviously you  could just pass it to the method instead
    var address = document.getElementById("address").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //In this case it creates a marker, but you can get the lat and lng from the location.LatLng
        map.setCenter(results[0].geometry.location);  //con esto centro el mapa
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

pero no se cual es el problema.. segun yo, map deberia ser global... :/


« Última modificación: 12 Agosto 2012, 13:20 pm por Kase » En línea

Kase


Desconectado Desconectado

Mensajes: 1.288


programa bonito ¬¬


Ver Perfil WWW
Re: google maps añadiendo campo de busqueda..
« Respuesta #1 en: 13 Agosto 2012, 07:38 am »

solucionado...  pase horas para darme cuenta que la variable map  era local y no global...
a quien se le ocurre setear un objeto como local cuando este no es recuperable.. y por lo normal es necesario extenderse y usarse ¬¬


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Google Maps en 3D
Noticias
wolfbcn 0 2,038 Último mensaje 27 Abril 2010, 21:28 pm
por wolfbcn
Google celebra el mes del orgullo gay añadiendo un arco iris a los términos ...
Noticias
wolfbcn 1 2,046 Último mensaje 20 Junio 2011, 21:06 pm
por WHK
Google cierra el grifo gratuito al acceso intensivo a la API de Google Maps
Noticias
wolfbcn 2 3,515 Último mensaje 28 Octubre 2011, 13:53 pm
por [u]nsigned
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines