2010-05-12 17 views
55

uso el siguiente código para establecer marcadores en mi mapa:Google API v3 MAPA: Centro y zoom sobre los marcadores que aparecen

var latLngs = [] 
    $.each(locations.markers, function(i, m){ 
    var myLatLng = new google.maps.LatLng(m.latitude, m.longitude); 
    latLngs[i] = myLatLng           
    var marker = new google.maps.Marker({ 
    position: myLatLng, 
    map: map, 
    shadow: shadow, 
    icon: image, 
    shape: shape, 
    title: m.city, 
    zIndex: i 
    }); 
    }) 

Los marcadores aparecen en mi mapa. Ahora me gustaría centrarme en & ampliar el mapa en estos marcadores. ¿Cómo puedo lograr esto? que he intentado:

map.fitBounds(getBoundsForLatLngs(latLngs)); 

El console.log de LatLng da a cabo:

[(46.793182, 7.146903) { b=46.793182, more...}, (46.8077779, 7.1709386) { b=46.8077779, more...}] 

pero no parecen funcionar, y me da ningún error en la consola. ¿Qué estoy haciendo mal?

Respuesta

126

He encontrar también esta corrección que zooms to fit all markers

LatLngList: una serie de casos de latLng, por ejemplo:

// "map" is an instance of GMap3 

var LatLngList = [ 
        new google.maps.LatLng (52.537,-2.061), 
        new google.maps.LatLng (52.564,-2.017) 
       ], 
    latlngbounds = new google.maps.LatLngBounds(); 

LatLngList.forEach(function(latLng){ 
    latlngbounds.extend(latLng); 
}); 

// or with ES6: 
// for(var latLng of LatLngList) 
// latlngbounds.extend(latLng); 

map.setCenter(latlngbounds.getCenter()); 
map.fitBounds(latlngbounds); 
+0

esto centra el mapa en un punto que quiero centrar el mapa en todos los puntos mostrados – meo

+9

¡intente esto! http://blog.shamess.info/2009/09/29/zoom-to-fit-all-markers-on-google-maps-api-v3/ – vsync

+0

funcionó, tuve que usar LatLngBounds como su ejemplo. Pasé mi matriz directamente a 'fitBounds()'. Gracias por su rápido apoyo – meo

2

Pruebe esta función .... funciona ...

$(function() { 
     var myOptions = { 
      zoom: 10, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
     var latlng_pos=[]; 
     var j=0; 
     $(".property_item").each(function(){ 
      latlng_pos[j]=new google.maps.LatLng($(this).find(".latitude").val(),$(this).find(".longitude").val()); 
      j++; 
      var marker = new google.maps.Marker({ 
       position: new google.maps.LatLng($(this).find(".latitude").val(),$(this).find(".longitude").val()), 
       // position: new google.maps.LatLng(-35.397, 150.640), 
       map: map 
      }); 
     } 
     ); 
     // map: an instance of google.maps.Map object 
     // latlng: an array of google.maps.LatLng objects 
     var latlngbounds = new google.maps.LatLngBounds(); 
     for (var i = 0; i < latlng_pos.length; i++) { 
      latlngbounds.extend(latlng_pos[ i ]); 
     } 
     map.fitBounds(latlngbounds); 



    }); 
17

En caso de que prefiera el estilo más funcional:

// map - instance of google Map v3 
// markers - array of Markers 
var bounds = markers.reduce(function(bounds, marker) { 
    return bounds.extend(marker.getPosition()); 
}, new google.maps.LatLngBounds()); 

map.setCenter(bounds.getCenter()); 
map.fitBounds(bounds); 
+0

Simple y elegante. Funciona muy bien, gracias! –

Cuestiones relacionadas