javascript - 难以在 Google map 上查找某个区域内是否存在某个位置

标签 javascript html google-maps-api-3 dom-events

我在 Google map 上绘制了一个多边形。但我无法验证该区域内是否存在一个点。我怎样才能实现这个目标?任何建议将不胜感激。

<!DOCTYPE html>
<html>
<head>
    <title>Polygon</title>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=my_API_key&sensor=false&libraries=geometry"></script>
    <script>
        var centerPoint = new google.maps.LatLng(22.65, 88.45);
        var centerPoint2 = new google.maps.LatLng(28.3, 86.3167);
        var centerPoint3 = new google.maps.LatLng(22.65, 88.45);
        var centerPoint4 = new google.maps.LatLng(20.6333, 84.3167);
        var centerPoint5 = new google.maps.LatLng(24.6333, 85.3167);
        function Initialize() {
            var mapProp = {
                center: centerPoint,
                zoom: 4,
                mapTypeId: google.maps.MapTypeId.HYBRID
            };
            var map = new google.maps.Map(document.getElementById("MyMap"), mapProp);
            var marker = new google.maps.Marker({
                                                  position: centerPoint,
                                                  animation: google.maps.Animation.BOUNCE
                                               });
    var myTrip = [centerPoint2, centerPoint3, centerPoint4];
    var flightPath = new google.maps.Polygon({
                        path: myTrip,
                        strokeColor: "#0000FF",
                        strokeOpacity: 0.2,
                        strokeWeight: 2,
                        fillColor: "#B0F0FF",
                        fillOpacity: 0.4,
                        editable: true
                        });
            marker.setMap(map);
    flightPath.setMap(map);
    var locationExists = new  google.maps.geometry.poly.containsLocation({
                                    point: centerPoint5,
                                    polygon: flightPath
                                });
    document.getElementById("MyData").innerHTML = locationExists;
         }
         google.maps.event.addDomListener(window, 'load', Initialize);
    </script>
</head>
<body>
    <div id="MyMap" style="height: 500px; width: 500px;"></div>
    <div id="MyData" style="height: 500px; width: 500px;"></div>
</body>

containsLocation() 使用是否正确?如果没有请提出建议。

enter image description here

最佳答案

使用google.maps.geometry.poly.containsLocation

注意:默认情况下不会加载几何库,您必须使用maps-api的libraries-parameter来加载它

关于javascript - 难以在 Google map 上查找某个区域内是否存在某个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379144/

相关文章:

javascript - Angular-Strap 的 "Aside"窗口的大小可以减小或增大吗?

html - 基本标签无法正常工作

javascript - Google Places API 在 Firefox 中无法运行

javascript - 更改鼠标悬停/移出时的图标失败

javascript - 修复了 PrettyPhoto 中的窗口大小

javascript - 为什么我的 Canvas 上什么也没有出现?

javascript - 为什么我返回的 JSON 响应被方括号 [] 包围?

javascript - 从 Google Drive 检索缩略图

javascript - 使谷歌地图跨越长页面

php - $_FILES 何时创建/清除 |与 javascript 重新加载的关系