gpt4 book ai didi

javascript - 当标记超出给定区域时如何收到警报

转载 作者:行者123 更新时间:2023-11-28 00:40:20 25 4
gpt4 key购买 nike

我是谷歌地图新手,我想在标记不是给定区域时发出警报,我已经创建了 map ,并且添加了一个圆圈来表示标记可以允许移动的区域,

enter image description here

这是我的代码

<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">


<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>
<script type="text/javascript">

// Basic Variables
var map, marker, myLatlng;
var Location;
var LAT = 6.9342150;
var LONG = 79.8919810;



function loadMap() {

myLatlng = new google.maps.LatLng(LAT, LONG);
var mapOptions = {
zoom: 14,
center: myLatlng
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});


// To add the marker to the map, use the 'map' property


marker = new google.maps.Marker({
position: myLatlng,
map: map,
// This allows me to drag the mark through map
draggable: true,
// Bounce the marker when it adds to the Map
animation: google.maps.Animation.DROP,
title: "Hello World!"


});

var CicleOption = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
center: myLatlng,
radius: 1000
};

new google.maps.Circle(CicleOption);

// To add the marker to the map, call setMap();
marker.setMap(marker);


}
</script>
</head>
<body onload="loadMap()">
<div id="map-canvas" style="height:350px;"></div>
</body>

我想在标记从圆圈中消失时收到警报,

标记可拖动

谢谢。

最佳答案

我为您提供了一个解决方案,请注意第 65 至 77 行。当标记放置在圆外时,它会再次移动到圆的中间,并且 map 再次居中。

<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">


<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>
<script type="text/javascript">

// Basic Variables
var map, marker, myLatlng;
var Location;
var LAT = 6.9342150;
var LONG = 79.8919810;

function loadMap() {

myLatlng = new google.maps.LatLng(LAT, LONG);
var mapOptions = {
zoom: 14,
center: myLatlng
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});


// To add the marker to the map, use the 'map' property


marker = new google.maps.Marker({
position: myLatlng,
map: map,
// This allows me to drag the mark through map
draggable: true,
// Bounce the marker when it adds to the Map
animation: google.maps.Animation.DROP,
title: "Hello World!"


});

var CicleOption = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
center: myLatlng,
radius: 1000
};

new google.maps.Circle(CicleOption);

// To add the marker to the map, call setMap();
marker.setMap(map);


// THIS IS ADDED

google.maps.event.addListener(marker,'dragend',function(event) {

var currPos = new google.maps.LatLng(event.latLng.k, event.latLng.B);

var dist = getDistance(currPos, CicleOption.center);

if(dist > CicleOption.radius){
alert('Marker is outside');
marker.setPosition(CicleOption.center);
map.setCenter(CicleOption.center);
}

});

}

var rad = function(x) {
return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // returns the distance in meter
};

</script>
</head>
<body onload="loadMap()">
<div id="map-canvas" style="height:350px;"></div>
</body>

此外,您可以使用 google-maps-lib 进行检查:

var cirlce = new google.maps.Circle(options);
var bounds = circle.getBounds();

var currPos = new google.maps.LatLng(lat, lng);
bounds.contains(currPos);

该方法包含返回一个 bool 值,指示点(currPos)是否在圆内。

以下是一个示例:http://jsfiddle.net/kaiser/wzcst/

关于javascript - 当标记超出给定区域时如何收到警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28022759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com