如何检查lat,long是否在我的地理围栏lat,long,radius之内

问题描述 投票:0回答:1

我正在尝试在城市内制作圆形地理围栏,我希望此地理围栏的宽度为2公里,我想知道用户是否在此地理围栏内

我做了什么

 private fun checkIfInsideGeoFence(){
        geofenceList.add(Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId("New York")

                // Set the circular region of this geofence.
                .setCircularRegion(
                    40.6882657,
                    -73.9398756,
                    2000F)

                // Create the geofence.
                .build())

        geofencingClient.addGeofences(getGeofencingRequest(),geofencePendingIntent).addOnSuccessListener {
            fusedLocationClient.lastLocation
                .addOnSuccessListener { location : Location? ->

                    // Got last known location, here I need to check if its inside the geofence
                }
        }.addOnFailureListener{

        }

    }

    private fun getGeofencingRequest(): GeofencingRequest {
        return GeofencingRequest.Builder().apply {
            setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            addGeofences(geofenceList)
        }.build()
    }

    private val geofencePendingIntent: PendingIntent by lazy {
        //Here I dont use a brodcast receiver since I dont need to know if the user enters or leaves the area, just if its inside
        PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }

我需要知道里面

 fusedLocationClient.lastLocation
                    .addOnSuccessListener { location : Location? ->

                        // Got last known location, here I need to check if its inside the geofence
                    }

如何知道用户是否处于特定比例

android google-maps kotlin google-maps-api-3 android-geofence
1个回答
0
投票
最简单的方法是检查中心经纬度到当前经纬度之间的距离,并查看该距离是否<2000 mts
© www.soinside.com 2019 - 2024. All rights reserved.