Как проверить, находится ли lat, long внутри моей геозоны? Lat, long, radius - PullRequest
0 голосов
/ 07 апреля 2020

Я пытаюсь создать круговую геозону внутри города, хочу, чтобы эта геозона была круговой шириной 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
                    }

Как узнать, находится ли пользователь в этом заданном c соотношении

1 Ответ

0 голосов
/ 07 апреля 2020

Самый простой подход - проверить расстояние между центральным лат длинным и текущим лат длинным и посмотреть, составляет ли расстояние <2000 мтс </p>

 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()
                }
            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...