我需要一些指导,以了解当我的位置位于指定位置附近时如何触发警报。
我想那时会使用谷歌地图和地理定位,但我不确定。
我也希望能够显示一些代码,但在做了无数的谷歌搜索后,我找不到任何东西,我不知道该寻找什么。
我将使用phonegap / cordova。
任何人都可以指导我朝正确的方向发展吗?enter code here
听起来你正在寻找像这样的geofence插件:https://github.com/cowbell/cordova-plugin-geofence
这样,您就可以监控设备何时进出您设置的自定义地理围栏/位置区域。
使用cordova plugin add cordova-plugin-geofence
将其添加到您的项目中,并按照其自述文件中的使用指南进行操作。
//Convert Destination Address to lat lng values
var specificlocation = {
lat: -13.26589,
lng: 98.365297
}
function onDeviceReady() {
//onDeviceReaddy
navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
}
// user Current Position
function displayAndWatch(position) {
setCurrentPosition(position);
watchCurrentPosition();
}
function setCurrentPosition(pos) {
var image = 'img/ic_CurrentLocationmap.png';
currentPositionMarker = new google.maps.Marker({
icon: image,
map: map,
position: new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
title: "Current Location"
});
map.panTo(new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}
//Watch User/phone current location
function watchCurrentPosition() {
var positionTimer = navigator.geolocation.watchPosition(
function (position) {
setMarkerPosition(
currentPositionMarker,
position
);
});
}
function setMarkerPosition(marker, position) {
marker.setPosition(
new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude)
);
var center = {
lat: position.coords.latitude,
lng: position.coords.longitude
}
map.setCenter(center);
//Check distance between specificlocation and user/phone current location
var distance = DistanceBetweenTwoPoints(center.lat, center.lng, specificlocation.lat,
specificlocation.lng, "K")
//with in 100 Meters
if (distance < 0.1) {
navigator.notification.confirm(
'You are Reached specificlocation Address', // message
onConfirmReached, //Callback
'AppName', // title
['No', 'Yes'] // buttonLabels
);
}
function locError(error) {
// the current position could not be located
}
function onConfirmReached(buttonIndex) {
if (buttonIndex == 2) {
//Alert result
}
}
//Find Distance between Two coordinations
function DistanceBetweenTwoPoints(lat1, lon1, lat2, lon2, unit) {
try {
unit = "K"
var radlat1 = Math.PI * lat1 / 180
var radlat2 = Math.PI * lat2 / 180
var radlon1 = Math.PI * lon1 / 180
var radlon2 = Math.PI * lon2 / 180
var theta = lon1 - lon2
var radtheta = Math.PI * theta / 180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(
radtheta);
dist = Math.acos(dist)
dist = dist * 180 / Math.PI
dist = dist * 60 * 1.1515
//Calculate Kilometer
if (unit == "K") {
dist = dist * 1.609344
}
//Calculate Miles
if (unit == "N") {
dist = dist * 0.8684
}
return dist;
} catch (err) {
console.log(err);
}
}