【问题标题】:How to Get Bounds and Change Bounds to Remove or add list of Marker Google Map如何获取边界和更改边界以删除或添加标记 Google 地图列表
【发布时间】:2013-11-07 10:23:15
【问题描述】:

我陷入了需要获取边界和更改边界的情况,因此我可以在我的 Google 地图下方的添加列表中添加和删除标记信息

与缩放更改相同,我需要添加因为我使用标记管理器

我需要一些关于如何实现这种类型的帮助,因为我是谷歌地图的新手

这是我的代码

<script type="text/javascript">
    var geocoder;
    var map;
    var placesList;

    function initialize() {
        var minZoomLevel = 4;
        var zooms = 7;
        geocoder = new google.maps.Geocoder();

        map = new google.maps.Map(document.getElementById('map'), {
            zoom: minZoomLevel,
            center: new google.maps.LatLng(38.50, -90.50),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        // Bounds for North America
        var strictBounds = new google.maps.LatLngBounds(
     new google.maps.LatLng(15.70, -160.50),
     new google.maps.LatLng(68.85, -55.90)
   );

        // Listen for the dragend event


  google.maps.event.addListener(map, 'dragend', function () {
            if (strictBounds.contains(map.getCenter())) return;

            // We're out of bounds - Move the map back within the bounds

            var c = map.getCenter(),
         x = c.lng(),
         y = c.lat(),
         maxX = strictBounds.getNorthEast().lng(),
         maxY = strictBounds.getNorthEast().lat(),
         minX = strictBounds.getSouthWest().lng(),
         minY = strictBounds.getSouthWest().lat();

            if (x < minX) x = minX;
            if (x > maxX) x = maxX;
            if (y < minY) y = minY;
            if (y > maxY) y = maxY;

            map.setCenter(new google.maps.LatLng(y, x));
        });


        // Limit the zoom level
        google.maps.event.addListener(map, 'zoom_changed', function () {
            if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
        });

         placesList = document.getElementById('places');
    }
    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
    function codeAddress() {
        var infowindow = new google.maps.InfoWindow();
        $.getJSON('/Dashboard/LoadWorkerList', function (address) {
            $.each(address, function () {
                var currVal = this["AddressLine1"];
                geocoder.geocode({ 'address': currVal }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var marker = new google.maps.Marker({
                            map: map,
                            icon: iconBase + 'man.png',
                            position: results[0].geometry.location,
                            title: currVal


                        })
                        placesList.innerHTML += '<li>' + currVal + '</li>';


                        google.maps.event.addListener(marker, 'click', (function (marker, i) {
                            return function () {
                                infowindow.setContent(currVal);
                                infowindow.open(map, marker);
                            }
                        })(marker, currVal));
                        address.push(marker);




                }
                else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                    setTimeout(codeAddress, 2000);
                }
                else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        });
    });
    return false;
}

window.onload = function () {
    initialize();
    codeAddress();
}

截图如下

找到一个列表https://developers.google.com/maps/documentation/javascript/examples/place-search-pagination

我想制作 Google 地图中的列表

提前致谢:D

【问题讨论】:

    标签: javascript jquery html google-maps google-maps-api-3


    【解决方案1】:

    将位置存储为&lt;li&gt;-elements 的属性:

    $('#places').append($('<li/>')
                          .text(currVal)
                           .data('location',results[0].geometry.location));
    

    ...然后您以后可以遍历 &lt;li/&gt; 元素并使用 LatLngBounds.contains() 检查边界是否包含该位置(如果不包含该位置,则隐藏 &lt;li&gt;

    例子:

            google.maps.event.addListener(map,'bounds_changed',function(){
              $('#places li').css('display',  function(){
                  return (map.getBounds().contains($(this).data('location')))
                          ? ''
                          : 'none';
                });
            });
    

    注意:在$.getJSON-callback 结束时,您应该触发bounds_changed-event 以应用列表过滤:

    google.maps.event.trigger(map,'bounds_changed');
    

    (或在创建元素时通过设置元素的显示直接应用过滤)

    【讨论】:

    • 我想添加一个 onchange 先生,你能帮我吗?
    • 在上面添加了一个例子。
    • 先生,我刚接触谷歌地图,我应该在哪里插入这组代码。对不起,我是新手 :(
    • 我遇到了错误Uncaught TypeError: Cannot call method 'lat' of undefined
    • 博士。 Molle 经过试验,我删除了错误,但是当涉及到边界时,当最后添加的标记超出边界时,另一个标记的整个列表也被删除
    猜你喜欢
    • 2019-10-09
    • 2015-08-08
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多