本文介绍了当标记位于Google地图中的多边形地理围栏之外时,地理围栏中不会显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有geofencing的代码,其中多边形的形状是在那里拖动一个标记的一侧多边形它给予警报,但我需要它应该给页面加载时自动警报,而不是手动拖动到外面。
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
code
<head>
<meta charset="utf-8">
<title>GMaps.js — 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>
推荐答案
我认为应该这样做。所有Gmaps.js都在做的是
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);
});
});
}
因此,将您的标记分配给变量。在加载时,触发每个人的'dragend'事件:
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');
这篇关于当标记位于Google地图中的多边形地理围栏之外时,地理围栏中不会显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!