简体   繁体   English

检查地图标记是否在选定范围内

[英]check if map markers are within selected bounds

I have a map with various markers and i need to be able to draw a rectangle on the map and select the markers which are within the rectangle bounds. 我有一个带有各种标记的地图,我需要能够在地图上绘制一个矩形并选择矩形边界内的标记。

So far i have found some great info here: How to get markers inside an area selected by mouse drag? 到目前为止,我在这里找到了一些很好的信息: 如何在鼠标拖动选择的区域内获取标记?

I have implemented the keymapzoom plugin ok. 我已经实现了keymapzoom插件。 like so 像这样

    $('#dispatcher').gmap3({action:'get'}).enableKeyDragZoom({
        boxStyle: {
          border: "dashed black",
          //backgroundColor: "red",
          opacity: 0.5
        },
        paneStyle: {
          backgroundColor: "gray",
          opacity: 0.2
        }
  });
var dz = $('#dispatcher').gmap3({action:'get'}).getDragZoomObject();
google.maps.event.addListener(dz, 'dragend', function (bnds) {
  alert(bnds);
});

This gives me the following (( lat,long ),( lat,long )) format from the alert(bnds); 这给了我以下(( lat,long ),( lat,long ))格式的警报(bnds);

I need to know how i can now check if any markers are within this? 我需要知道我现在如何检查是否有任何标记?

I already have an object that is storing the markers for another reason. 由于其他原因,我已经有一个存储标记的对象。 like: 喜欢:

    markers[name] = {};
    markers[name].lat = lati;
    markers[name].lng = longi;

which might be useful? 哪个可能有用?

I don't understand how to use the GLatLngBounds and containsLatLng(latlng:GLatLng) as suggested. 我不明白如何使用GLatLngBounds和containsLatLng(latlng:GLatLng)。

Your question is tagged with the v3 version of the Maps API, so I'll assume you are using that version (which you should as v2 is deprecated). 您的问题是使用Maps API的v3版本标记的,因此我假设您使用的是该版本(您应该弃用v2)。 Note that some classes and methods are named different than in your question. 请注意,某些类和方法的命名与您的问题不同。

Bounds are represented with the LatLngBounds class. Bounds由LatLngBounds类表示。 You can perform the contains method on an instance of that class to determine if a point lies within those bounds. 您可以对该类的实例执行contains方法,以确定某个点是否位于这些边界内。

If you have an object with all your markers, you can loop through them and check each marker, for example: 如果您有一个包含所有标记的对象,则可以遍历它们并检查每个标记,例如:

var bounds = new google.maps.LatLngBounds(sw, ne);
for (var a in markers) {
    if (bounds.contains(new google.maps.LatLng(markers[a].lat, markers[a].lng)) {
        // marker is within bounds
    }
}

On a side note, I would store the LatLng object in the markers object when creating them. 在旁注中,我会在创建它们时将LatLng对象存储在markers对象中。 That way you don't have to create them wherever you need. 这样您就不必在任何需要的地方创建它们。

Box/Rectangle Draw Selection in Google Maps Google地图中的框/矩形绘制选择

This was my solution.. 这是我的解决方案..

     google.maps.event.addListener(dz, 'dragend', function(e) { //important listener          
      for(var i = 0; i < markers.length; i++){ // looping through my Markers Collection       
        if(e.contains(markers[i].position))
            console.log("Marker"+ i +" - matched");
        }         
     });

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

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