Android的 - 需要增加100多geofencesAndroid、geofences

2023-09-05 04:58:00 作者:浅︶﹋★、色调

我开发一个应用程序,用户可以设置多个位置。我得到的成功,显示通知,当用户获得输入或使用地理围栏离开特定区域。

现在,有情况,我需要提供监控所有保存的位置,也可以是上百多。我读过在给定链路的的你可以有多个活动geofences,具有100个设备用户的限制。的

有没有办法每个设备用户添加超过100 geofences?

谢谢!

解决方案 有一个ArrayList所有要监视+100的geofences。 听位置更新。

当你得到一个位置更新,如果所有geofences的ArrayList的是100多,除去所有geofences进行监测,然后使用harvesine公式计算出100最近geofences:

 公共静态最后的双R = 6372.8; //在公里

公共静态双半正矢(双LAT1,双lon1,双LAT2,双lon2){
  双DLAT = Math.toRadians(LAT2  -  LAT1);
  双dLon = Math.toRadians(lon2  -  lon1);
  LAT1 = Math.toRadians(LAT1);
  LAT2 = Math.toRadians(LAT2);

  双A = Math.sin(DLAT / 2)* Math.sin(DLAT / 2)+ Math.sin(dLon / 2)* Math.sin(dLon / 2)* Math.cos(LAT1)* Math.cos( LAT2);
  双C = 2 * Math.asin(的Math.sqrt(一));
  返回R *℃;
}
 
Android L安装教程 图文介绍

这会给你两个位置之间的距离。之后,你可以说距离与地理栅栏区域半径知道,如果是区域内进行比较。

注意:这个距离将是公里,如果你的半径是米,然后就用1000这样它会转换成米乘半正矢方法结果

参考

开始监测100最近geofences的结果列表。

这将允许您随时监控根据您的位置在100附近geofences。之所以能够监控100多个,因为它会改变的,监测地理围栏地区的100最近的地理栅栏区域。

I'm developing an application where user can set multiple locations. I get succeed to show notifications when user get Enter or Leave specific region using GeoFencing.

Now, there is situation that i need to provide monitoring for all saved locations and it can be hundreds and more. I've read at the given link "You can have multiple active geofences, with a limit of 100 per device user."

Is there any way add more then 100 geofences per device user ?

Thanks!

解决方案

Have an ArrayList with all the geofences you want to monitor +100. Listen to location updates.

When you get a location update, if the ArrayList of all your geofences is more than 100, remove all geofences been monitored and then calculate the 100 nearest geofences using the harvesine formula:

public static final double R = 6372.8; // In kilometers

public static double haversine(double lat1, double lon1, double lat2, double lon2) {
  double dLat = Math.toRadians(lat2 - lat1);
  double dLon = Math.toRadians(lon2 - lon1);
  lat1 = Math.toRadians(lat1);
  lat2 = Math.toRadians(lat2);

  double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
  double c = 2 * Math.asin(Math.sqrt(a));
  return R * c;
}

That will give you the distance between the two locations. After that you could compare that distance with the geofence region radius to know if is inside the region.

Note: This distance will be in kilometers if your radius is on meters then just multiply the haversine method result with 1000 so that it's converted to meters.

Reference

Start monitoring the result list of the 100 nearest geofences.

This will allow you to always monitor the 100 nearest geofences based on your location. Been able to monitor more than 100 since it will change the monitoring geofence regions always to the 100 nearest geofence regions.

 
精彩推荐
图片推荐