your programing

Google MAP API v3 : 표시된 마커의 중앙 및 확대 / 축소

lovepro 2021. 1. 5. 19:46
반응형

Google MAP API v3 : 표시된 마커의 중앙 및 확대 / 축소


다음 코드를 사용하여지도에 마커를 설정합니다.

  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
   });
  }) 

마커가 내지도에 표시됩니다. 이제이 마커에서지도를 중앙 및 확대 / 축소하고 싶습니다. 어떻게하면 되나요? 나는 시도했다 :

map.fitBounds(getBoundsForLatLngs(latLngs));

latLngs의 console.log는 다음을 제공합니다.

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

그러나 작동하지 않는 것 같고 콘솔에 오류가 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?


또한 모든 마커에 맞게 확대 / 축소되는 이 수정 사항을 찾았습니다.

LatLngList : latLng 인스턴스의 배열입니다 . 예 :

// "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); 

더 기능적인 스타일을 선호하는 경우 :

// 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);

이 기능을 사용해보십시오 .... 작동합니다 ...

$(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 );



    });

디스플레이 마커의 중앙 및 자동 줌용

// map: an instance of google.maps.Map object

// latlng_points_array: an array of google.maps.LatLng objects

var latlngbounds = new google.maps.LatLngBounds( );

    for ( var i = 0; i < latlng_points_array.length; i++ ) {
        latlngbounds.extend( latlng_points_array[i] );
    }

map.fitBounds( latlngbounds );

참조 URL : https://stackoverflow.com/questions/2818984/google-map-api-v3-center-zoom-on-displayed-markers

반응형