Google MAP API v3: Center & Zoom on displayed markers

后端 未结 4 845
囚心锁ツ
囚心锁ツ 2020-11-29 01:12

I use the following code to set markers on my map:

  var latLngs = []
  $.each(locations.markers, function(i, m){
   var myLatLng = new google.maps.LatLng(m.         


        
4条回答
  •  無奈伤痛
    2020-11-29 01:39

    Try this function....it works...

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

提交回复
热议问题