当Google地图中的标记超出多边形地理围栏时,警报不会显示在地理围栏中

时间:2017-03-28 08:22:08

标签: javascript google-maps google-maps-api-3 gmaps.js

我有地理围栏的代码,因为多边形的形状是将多边形拖出一边放在它给出警告的边上,但我需要它在页面加载时自动发出警报而不是手动拖到外面。

<head>
    <meta charset="utf-8">
    <title>GMaps.js &mdash; Geofences</title>
    <script type="text/javascript"
            src="https://maps.googleapis.com/maps/api/js?sensor=true&key=AIzaSyDci4vYApOxVdKqwlpXSv9h77AcWbNuzmQ&libraries=drawing">
    </script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script src="../gmaps.js"></script>
    <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="examples.css" />
</head>
    <body onload="runMaps()">
        <script>

            var map;

            $(document).ready(function () {
                map = new GMaps({
                    el: '#map',
                    lat: -12.043333,
                    lng: -77.028333
                });
                var path = [
                      [-12.040397656836609, -77.03373871559225],
                      [-12.040248585302038, -77.03993927003302],
                      [-12.050047116528843, -77.02448169303511],
                      [-12.044804866577001, -77.02154422636042]
                ];
                var drawingManager = new google.maps.drawing.DrawingManager({
                    drawingMode: google.maps.drawing.OverlayType.MARKER,
                    drawingControl: true,
                    drawingControlOptions: {
                        position: google.maps.ControlPosition.TOP_CENTER,
                        drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
                    },
                    markerOptions: { icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png' },
                    circleOptions: {
                        fillColor: '#ffff00',
                        fillOpacity: 1,
                        strokeWeight: 5,
                        clickable: false,
                        editable: true,
                        zIndex: 1
                    }
                });
                drawingManager.setMap(map);


                polygon = map.drawPolygon({
                    paths: path,
                    strokeColor: '#BBD8E9',
                    strokeOpacity: 1,
                    strokeWeight: 3,
                    fillColor: '#BBD8E9',
                    fillOpacity: 0.6
                });
                map.addMarker({
                    lat: -12.043333,
                    lng: -77.028333,
                    draggable: true,
                    fences: [polygon],
                    outside: function (marker, fence) {
                        alert('This marker has been moved outside of its fence');
                    }
                });
                map.addMarker({
                    lat: -12.041111,
                    lng: -77.021111,
                    draggable: true,
                    fences: [polygon],
                    outside: function (marker, fence) {
                        alert('This marker has been moved outside of its fence');
                    }
                });
            });

        </script>
        <div id="map" style="height:800px"></div>
    </body>

1 个答案:

答案 0 :(得分:0)

我认为应该这样做。所有的Gmaps.js都在做什么

if (marker.fences) {
    google.maps.event.addListener(marker, 'dragend', function() {
      self.checkMarkerGeofence(marker, function(m, f) {
        outside(m, f);
      });
    });
}

https://github.com/hpneo/gmaps/blob/master/gmaps.js#L851

因此,将您的标记分配给变量。在加载时,触发每个上的'dragend'事件:

var marker1 = map.addMarker({
    lat: -12.043333,
    lng: -77.028333,
    draggable: true,
    fences: [polygon],
    outside: function (marker, fence) {
        alert('This marker has been moved outside of its fence');
    }
});

var marker2 = map.addMarker({
    lat: -12.041111,
    lng: -77.021111,
    draggable: true,
    fences: [polygon],
    outside: function (marker, fence) {
        alert('This marker has been moved outside of its fence');
    }
});

google.maps.event.trigger(marker1, 'dragend');
google.maps.event.trigger(marker2, 'dragend');