如何检查设备是否已通过GoogleMaps的职位?

时间:2017-12-20 13:49:50

标签: android google-maps geolocation geofencing android-geofence

我正在使用LocationListener GoogleMaps API来处理地图。在我的应用程序中,这绘制了用户行进的路线。我想在用户处于他们去过的位置时设置警报。但我不知道是否存在为我这样做的服务,或者我是否拥有所有的工具。我不明白地理定位的概念。

因此,当用户通过跟踪的路线时,我希望提醒用户。

private class FollowMeLocationSource implements LocationSource, LocationListener {

    private OnLocationChangedListener onLocationChangedListener;
    private LocationManager locationManager;
    private LocationListener locationListener;

    private final Criteria criteria = new Criteria();
    private String bestAvailableProvider;

     /* Updates are restricted to one every 10 seconds, and only when
     * movement of more than 10 meters has been detected.*/
     private final long minTime = 2000;
     private final float minDistance = 5;

     private FollowMeLocationSource() {

         locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
         getBestAvailableProvider();
         // Specify Location Provider criteria
         criteria.setAccuracy(Criteria.ACCURACY_FINE);
         criteria.setPowerRequirement(Criteria.POWER_LOW);
         criteria.setAltitudeRequired(true);
         criteria.setBearingRequired(true);
         criteria.setSpeedRequired(true);
         criteria.setCostAllowed(true);

         locationManager.requestLocationUpdates(bestAvailableProvider,minTime,minDistance,this);
     }

    private void getBestAvailableProvider() {
        /* The preffered way of specifying the location provider (e.g. GPS, NETWORK) to use
         * is to ask the Location Manager for the one that best satisfies our criteria.
         * By passing the 'true' boolean we ask for the best available (enabled) provider. */
        bestAvailableProvider = locationManager.getBestProvider(criteria, true);
        Log.i(TAG,"bestAvailableProvider: " + bestAvailableProvider);
    }

    /* Activates this provider. This provider will notify the supplied listener
     * periodically, until you call deactivate().
     * This method is automatically invoked by enabling my-location layer. */
    @Override
    public void activate(OnLocationChangedListener listener) {
        Log.i(TAG,"activate");
        // We need to keep a reference to my-location layer's listener so we can push forward
        // location updates to it when we receive them from Location Manager.
        onLocationChangedListener = listener;

        // Request location updates from Location Manager
        if (bestAvailableProvider != null) {
            //locationManager.requestLocationUpdates(bestAvailableProvider, minTime, minDistance, this);
            Log.i(TAG,"activate, bestProvider != null");
            locationManager.requestLocationUpdates(bestAvailableProvider,minTime,minDistance,this);
        } else {
            Log.i(TAG,"activate, bestProvider == null");
            // (Display a message/dialog) No Location Providers currently available.
        }
    }

    /* Deactivates this provider.
    * This method is automatically invoked by disabling my-location layer. */
    @Override
    public void deactivate() {
        Log.i(TAG,"deactivate");
        // Remove location updates from Location Manager
        locationManager.removeUpdates(this);

        onLocationChangedListener = null;
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i(TAG,"onLocationChanged. Latitude: " + location.getLatitude() + " - Longitude: " + location.getLongitude());
        /* Push location updates to the registered listener..
         * (this ensures that my-location layer will set the blue dot at the new/received location) */
        if (onLocationChangedListener != null) {
            onLocationChangedListener.onLocationChanged(location);
        }

        /* ..and Animate camera to center on that location !
         * (the reason for we created this custom Location Source !) */
        listaRota.add(location);

        if ( listaRota.size() == 1 ) {
            mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude())).title("Inicio"));
        }

        if ( listaRota.size() >= 2 ) {
            drawPolyLineOnMap();
        }

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()),15));
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Log.i(TAG,"onStatusChanged: " + s + ", Estado: " + i);
    }

    @Override
    public void onProviderEnabled(String s) {
        Log.i(TAG,"onProviderEnabled: " + s);
    }

    @Override
    public void onProviderDisabled(String s) {
        Log.i(TAG,"onProviderDisabled: " + s);
    }
}

public void drawPolyLineOnMap() {

    List<LatLng> list = new ArrayList<>();

    for( Location l : listaRota ) {
        list.add(new LatLng(l.getLatitude(),l.getLongitude()));
    }

    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.color(Color.BLUE);
    polylineOptions.width(15);
    polylineOptions.addAll(list);

    mMap.clear();
    mMap.addPolyline(polylineOptions);
}

1 个答案:

答案 0 :(得分:0)

该服务为Geofence Monitoring,并且有很多使用它的示例和教程(例如this one)。