Phonegap / Cordova地理位置警报可警告特定位置附近的内容

时间:2019-03-26 19:25:51

标签: google-maps cordova geolocation phonegap

我需要一些指导来找出当我的位置位于指定位置附近时如何发出警报。

我想那会用到谷歌地图和地理位置,但是我不确定。

我也希望能够显示一些代码,但是经过无数次Google搜索之后,我什么也找不到,而且我也不知道要寻找什么。

我将使用phonegap / cordova。

有人可以引导我朝正确的方向吗?enter code here

2 个答案:

答案 0 :(得分:1)

听起来您正在寻找像这样的地理围栏插件:slightly modified from original

这将使您可以监视设备何时进入和退出设置的自定义地理围栏/位置区域。

使用cordova plugin add cordova-plugin-geofence将其添加到您的项目中,并按照其自述文件中的使用指南进行操作。

答案 1 :(得分:0)


//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);
        }
    }