【问题标题】:How to check if lat , long is inside my geofence lat,long,radius如何检查 lat , long 是否在我的地理围栏内 lat,long,radius
【发布时间】:2020-04-07 15:03:15
【问题描述】:

我正在尝试在城市内制作圆形地理围栏,我希望这个地理围栏的圆形宽度为 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】:

    最简单的方法是检查中心经纬度到当前经度经度之间的距离,看看距离是否

     fusedLocationClient.lastLocation
                .addOnSuccessListener { location : Location? ->
    
                    val results = FloatArray(1)
                    Location.distanceBetween(
                        40.6882657,
                        -73.9398756,
                        location!!.latitude,
                        location.longitude,
                        results
                    )
                    val distanceInMeters = results[0]
                    val isWithin2km = distanceInMeters < 2000
                    if(isWithin2km){
                        navigateTo()
                    }else{
                        startActivity(Intent(this,ActivityGeoFenceError::class.java))
                        finish()
                    }
                }
    

    【讨论】:

    • 如果您已经自己解决了问题,请考虑将您自己的答案标记为正确。它可以帮助其他开发人员了解您的答案,知道它解决了您遇到的问题。谢谢!
    • @CoffeeBreak 你能检查我的问题吗
    【解决方案2】:

    基于@CoffeeBreak 的方法,也可以使用实例方法distanceTo 以使其不那么冗长。类似这样的扩展函数形式:

    fun Location.checkIsInBound(radius: Int,center:Location):Boolean
            =  this.distanceTo(center)<radius
    
    fusedLocationClient.lastLocation
            .addOnSuccessListener { location : Location? ->
            val isWithin2km = location?.checkIsInBound(2000,center) }
    

    除了使用 Location API 的 distanceTo,您还可以使用 Google Map Utility Library 的 SphericalUtilcomputeDistanceBetween,但它需要 LatLng 作为输入。

    【讨论】:

    • 你能看看我的帖子吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多