谷歌地图API检查,如果在多个边界存在标志 [英] Google Maps API check if marker exists in multiple bounds

查看:208
本文介绍了谷歌地图API检查,如果在多个边界存在标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的界限将它们添加到地图之前就已经存在的标记。我发现,这个最好的方法是使用

map.getBounds()。包括(marker.getPosition())如果存在地图上的标记和虚假的,如果它不返回true。因此,对假的,我的标记添加到地图中。

这工作得很好,但是我有多个边界和 map.getBounds()似乎得到1边界。

下code设置每个界桩的循环,但检查标志,在短短1边界存在。如何检查,如果在我的所有边界存在的标记?

我检查的最后一个函数标记。

 函数findPlaces(){    VAR boundsArray =新的Array(); //我多边界数组    对于(VAR I = 0; I< boundsArray.length;我++){
      VAR边界= boundsArray [I]      //在这个边界进行搜索
      VAR请求= {
        边界:边界,
        关键字:的document.getElementById(关键字)值
      };      service.nearbySearch(请求,回调);
    }
  });
}
//返回的地方,并创建标记
回调函数(结果状态){
    如果(状态== google.maps.places.PlacesServiceStatus.OK){
       对于(VAR I = 0; I< results.length;我++){
       createMarker(结果[I]);
    }
}//创建标记
功能createMarker(地方){
    VAR的标记=新google.maps.Marker({
       位置:place.geometry.location
    });    。变种onMap = map.getBounds()包含(marker.getPosition());    //如果标记是不是在地图上,添加它
    如果(onMap === FALSE){
        marker.setMap(地图);
    }
}


解决方案

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

多边形支持多路径,所以你必须要为创建一个包含每个的LatLngBounds阵列由的LatLngBounds的4个顶点定义,这个数组应用到多边形的路径属性的LatLngs的数组。

这多边形现在可以使用 google.maps.geometry程式使用。 poly.containsLocation ,以确定是否一个位置位于任何的LatLngBounds的。

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()) 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.

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

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

解决方案

You may create 1 polygon of all these LatLngBounds.

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.

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

这篇关于谷歌地图API检查,如果在多个边界存在标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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