Android Google Map how to check if the gps location is inside the circle

后端 未结 4 1019
小蘑菇
小蘑菇 2020-12-12 17:49

I\'m trying to detect if a user is in the radius of a Marker , using the users gps location. I have the marker\'s coordinates, but I don\'t know how to calculate whether the

4条回答
  •  盖世英雄少女心
    2020-12-12 18:12

    The accepted solution of Daniel Nugent isn't so good anymore because setOnMyLocationChangeListener is deprecated now.

    Here is current way how to do it in fragment - change getActivity() with this for Activity:

    private boolean mLocationPermissionGranted;
    
    // The geographical location where the device is currently located. That is, the last-known
    // location retrieved by the Fused Location Provider.
    private Location mLastKnownLocation;
    private long UPDATE_INTERVAL = 10 * 1000;  /* 10 secs */
    private long FASTEST_INTERVAL = 2000; /* 2 sec */
    private FusedLocationProviderClient mFusedLocationProviderClient;
    
    private void addLocationChangeListener(){
            // Construct a FusedLocationProviderClient.
            mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
    
            // Create the location request to start receiving updates
            LocationRequest mLocationRequest = new LocationRequest();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(UPDATE_INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            //mLocationRequest.setMaxWaitTime(0);
            //mLocationRequest.setSmallestDisplacement(0);
    
            // Create LocationSettingsRequest object using location request
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
            builder.addLocationRequest(mLocationRequest);
            LocationSettingsRequest locationSettingsRequest = builder.build();
    
            // Check whether location settings are satisfied
            // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
            SettingsClient settingsClient = LocationServices.getSettingsClient(getActivity());
            settingsClient.checkLocationSettings(locationSettingsRequest);
    
    
            // new Google API SDK v11 uses getFusedLocationProviderClient(this)
            if (mLocationPermissionGranted) {
                mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest,
                        new LocationCallback() {
                            @Override
                            public void onLocationResult(LocationResult locationResult) {
                                // do work here
                                Location location = locationResult.getLastLocation();
                            }
                        },
                        Looper.myLooper());
            } else {
                getLocationPermission();
            }
        }
    

提交回复
热议问题