Google Maps API 检查标记是否存在于多个边界中 [英] Google Maps API check if marker exists in multiple bounds

查看:30
本文介绍了Google Maps API 检查标记是否存在于多个边界中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将标记添加到地图之前,我正在尝试检查边界中是否已经存在标记.我发现最好的方法是使用

I'm trying to check if markers already exist in my boundaries before adding them to the map. I found that the best way to this is by using

map.getBounds().contains(marker.getPosition()) 如果标记存在于地图上,则返回 true,否则返回 false.因此,如果为 false,我将标记添加到地图中.

map.getBounds().contains(marker.getPosition()) which returns true if the marker exists on the map and false if it doesn't. So on false, I add the marker to the map.

这很好用,但是我有多个边界并且 map.getBounds() 似乎有 1 个边界.

This works fine however I have multiple boundaries and map.getBounds()seems to get 1 boundary.

下面的代码在循环中的每个边界中设置标记,但检查标记是否仅存在于 1 个边界中.如何检查标记是否存在于我的所有边界中?

The code below sets markers in each boundary in a loop but checks if markers exists in just 1 boundary. How can I check if markers exist in all my boundaries?

我正在检查最后一个函数中的标记.

I'm checking markers in the last function.

function findPlaces() {      

    var boundsArray = new Array(); //my array of multiple boundaries  

    for (var i = 0; i < boundsArray.length; i++) {
      var boundary = boundsArray[i];                  

      // Perform search over this boundary
      var request = {
        bounds: boundary,
        keyword: document.getElementById("keyword").value
      };

      service.nearbySearch(request, callback); 
    }
  });
}


//Returns places and create markers
function callback(results, status) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) {
       for (var i = 0; i < results.length; i++) {
       createMarker(results[i]);    
    }     
}   

//create markers
function createMarker(place) {                          
    var marker = new google.maps.Marker({
       position: place.geometry.location
    });

    var onMap = map.getBounds().contains(marker.getPosition());

    //if marker is not on the map, add it               
    if (onMap === false) {          
        marker.setMap(map);
    }
}

推荐答案

您可以创建所有这些 LatLngBounds 的 1 个多边形.

You may create 1 polygon of all these LatLngBounds.

多边形支持多个路径,因此您所要做的就是为每个 LatLngBounds 创建一个数组,其中包含一个由 LatLngBounds 的 4 个顶点定义的 LatLngs 数组,并将该数组应用于多边形的路径属性.

Polygons support multiple Paths, so all you have to to is create an array which contains for every LatLngBounds an array with the LatLngs defined by the 4 vertices of the LatLngBounds and apply this array to the paths-property of the Polygon.

此多边形现在可以与 google.maps.geometry 一起使用.poly.containsLocation 以确定某个位置是否位于任何 LatLngBounds 内.

This Polygon now may used with google.maps.geometry.poly.containsLocation to determine if a location is located within any of the LatLngBounds.

这篇关于Google Maps API 检查标记是否存在于多个边界中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆