如何根据位置经纬度限制用户登录

How to restrict user login based on location latitude longitude

我正在开发一个考勤应用程序。当用户到达办公室时它会登录用户,否则会显示一条消息 "not in office"。因此,为此我需要获取用户位置并需要检查其是否在办公室经度纬度值之间。只有当用户在办公室的经度纬度值之间时,用户才能登录。如何开发这个应用程序。

创建一个由办公区域定义的地理围栏(按照 this 了解如何操作)例如,填充名为 mGeofenceList:

的列表对象
mGeofenceList.add(new Geofence.Builder()
    // Set the request ID of the geofence. This is a string to identify this
    // geofence.
    .setRequestId(entry.getKey())

    .setCircularRegion(
            entry.getValue().latitude,
            entry.getValue().longitude,
            Constants.GEOFENCE_RADIUS_IN_METERS
    )
    .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
            Geofence.GEOFENCE_TRANSITION_EXIT)
    .build());

每次登录时获取用户的位置并检查它是否属于geofence.If里面,让him/her登录否则说不在区域。使用此公式检查:

更新 公式解释: 如果你知道圆的半径和它的中心是什么,你可以通过该公式得到圆内或圆外的任一点。如果从点到中心的距离大于半径则它在外面,反之亦然。

如果有帮助,您还可以查看 this 方法。

简单!

获取用户位置后,如果用户在您提到的范围内,则可以检查与办公室原点的距离(纬度,经度),只需标记即可。

float[] results = new float[1];
Location.distanceBetween(currentlatitude, currentlongitude, originLat, originLon, results);
float distanceInMeters = results[0];
boolean isWithinRange = distanceInMeters < 3000; 

if (isWithinRange) {
    //write your code what ever you wanna perform
}