Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - alerts are not showing in geofencing when marker is outside of polygon geofence in google maps

i have code for geofencing , in that polygon shape is there while dragging one marker out side the polygon its giving alert , but i need it should give alert automatically when page loads instead of dragging manually to outside.

code

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this should do it. All the Gmaps.js is doing under the hood is

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

So assign your markers to variables. On load, trigger the 'dragend' event on each of them:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...