【问题标题】:Phonegap/Cordova geolocation alert what near a specific locationPhonegap/Cordova 地理定位提醒特定位置附近有什么
【发布时间】:2019-03-26 19:25:50
【问题描述】:

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

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

我也希望能够显示一些代码,但是在进行了无数次谷歌搜索后,我找不到任何东西,也不知道要查找什么。

我将使用 phonegap / cordova。

谁能指引我正确的方向?enter code here

【问题讨论】:

    标签: google-maps cordova geolocation phonegap


    【解决方案1】:

    听起来您正在寻找像这样的地理围栏插件:https://github.com/cowbell/cordova-plugin-geofence

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

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

    【讨论】:

      【解决方案2】:


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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-11
        • 2011-04-24
        • 1970-01-01
        • 2015-11-28
        • 2015-09-05
        相关资源
        最近更新 更多