简体   繁体   English

如何设置谷歌 Map 的中心和缩放以覆盖从 Wordpress 数据库生成的所有标记

[英]How to set center and zoom of Google Map to cover all markers generated from Wordpress database

I've managed to cobble together a script that pulls lat/long & names for markers from a MySQL database, this is via Wordpress WPDB get_results query.我设法拼凑了一个脚本,该脚本从 MySQL 数据库中提取标记的纬度/经度和名称,这是通过 Wordpress WPDB get_results 查询。

The data is then parsed via JSON and the markers are added to the map.然后通过 JSON 解析数据,并将标记添加到 map。 This works fine and the markers get added to the map.这工作正常,标记被添加到 map。

However, I'm also trying to auto-center and auto-zoom to fit all the markers into the field of view, this part of the code doesn't seem to work, it just sets the map center to 0.0,0.0.但是,我也在尝试自动居中和自动缩放以使所有标记适合视野,这部分代码似乎不起作用,它只是将 map 中心设置为 0.0,0.0。

Does anybody know where I'm going wrong?有人知道我哪里出错了吗? I've taken a look at many of the other questions on here and tried for hours to tweak this to get it to work with little success.我已经查看了这里的许多其他问题,并尝试了几个小时来调整它以使其工作但收效甚微。

Any help would be greatly appreciated, thanks!任何帮助将不胜感激,谢谢!

        <?php
        global $wpdb;
        $data = $wpdb->get_results( 
        "
        SELECT marker_name, marker_lat, marker_long 
        FROM rwc_web_gurus
        WHERE var_type = 'marker' 
        "
        );
    ?>
    <div id="map"></div>
    <script type='text/javascript'>
     function initMap() {

        var latlng = new google.maps.LatLng(<?php echo $focuslatcurrent; ?>, <?php echo $focuslongcurrent; ?>); // default location
        var myOptions = {
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false,
        };

        //INITIALISE MAP MARKERS
        var map = new google.maps.Map(document.getElementById('map'),myOptions);
        var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
        var json=JSON.parse('<?php echo(json_encode($data));?>');
        for( var o in json ){

            lat =json[ o ].marker_lat;
            lng=json[ o ].marker_long;
            name=json[ o ].marker_name;

            marker = new google.maps.Marker({
                position: new google.maps.LatLng(lat,lng),
                name:name,
                map: map
            }); 

            // extend bounds
            var bounds = new google.maps.LatLngBounds();
            for(i=0;i<marker.length;i++) {
               bounds.extend(marker[i].getPosition());
            }


            //set center and zoom
            google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
              this.setZoom(map.getZoom()-1);

              if (this.getZoom() > 15) {
                this.setZoom(15);
              }
            });
            map.fitBounds(bounds);

            google.maps.event.addListener( marker, 'click', function(e){
                infowindow.setContent( this.name );
                infowindow.open( map, this );
            }.bind( marker ) );
        }
    }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=<?php echo $apikeycurrent; ?>&callback=initMap">
    </script>

You should define bounds object and call fitBounds() outside of the for loop, because currently you define new instance of bounds and apply fitBounds() on each iteration.您应该定义边界 object 并在for循环之外调用fitBounds() ,因为目前您定义了新的边界实例并在每次迭代中应用 fitBounds()。

Do it this way这样做

//INITIALISE MAP MARKERS
var map = new google.maps.Map(document.getElementById('map'),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, lng;
var json=JSON.parse('<?php echo(json_encode($data));?>');
var bounds = new google.maps.LatLngBounds();
for( var o in json ){
    lat =json[ o ].marker_lat;
    lng=json[ o ].marker_long;
    name=json[ o ].marker_name;

    marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat,lng),
        name:name,
        map: map
    }); 

    // extend bounds
    bounds.extend(marker.getPosition());

    //set center and zoom
    google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
        this.setZoom(map.getZoom()-1);

        if (this.getZoom() > 15) {
            this.setZoom(15);
        }
    });

    google.maps.event.addListener( marker, 'click', function(e){
            infowindow.setContent( this.name );
            infowindow.open( map, this );
    }.bind( marker ));
} 
map.fitBounds(bounds);

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM