检测用户何时进入圈子

问题描述

我正在开发基于Google地图的应用程序。

我在地图(https://ibb.co/PhZy0t6添加一个圆圈,并且一直试图检测用户是否输入了圆圈

用户是我添加用户当前位置的标记

我试图做到以下几点:

// Circle code:
        circle = mMap.addCircle(new CircleOptions()
                .center(new LatLng(addressLatLng.latitude,addressLatLng.longitude))
                .radius(70)
                .strokeColor(Color.RED)
        );


// The rest

        float[] distance = new float[2];

        Location.distanceBetween(userLocation.latitude,userLocation.longitude,circle.getCenter().latitude,circle.getCenter().longitude,distance);

        if (distance[0] <= circle.geTradius()) {
            Toast.makeText(this,"User Enter Circle",Toast.LENGTH_SHORT).show();

        }

但是没用。

感谢您的帮助

解决方法

所以最后感谢@Axiumin,我做到了:

@Override
public void onLocationChanged(Location location) {


    userLocationMarker.setPosition(new LatLng(location.getLatitude(),location.getLongitude()));


    Location userLocation = new Location("userLocation");
    userLocation.setLatitude(location.getLatitude());
    userLocation.setLongitude(location.getLongitude());

    Location circleLocation = new Location("circleLocation");
    circleLocation.setLatitude(circle.getCenter().latitude);
    circleLocation.setLongitude(circle.getCenter().longitude);


    float distance = userLocation.distanceTo(circleLocation);

    if (distance <= 70){ // If distance is less than 70 Meters


    }



}