Example usage for android.location LocationManager getBestProvider

List of usage examples for android.location LocationManager getBestProvider

Introduction

In this page you can find the example usage for android.location LocationManager getBestProvider.

Prototype

public String getBestProvider(Criteria criteria, boolean enabledOnly) 

Source Link

Document

Returns the name of the provider that best meets the given criteria.

Usage

From source file:li.barter.fragments.AbstractBarterLiFragment.java

public boolean isLocationServiceEnabled() {
    LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    String provider = lm.getBestProvider(criteria, true);
    return ((provider != null) && !LocationManager.PASSIVE_PROVIDER.equals(provider));
}

From source file:net.freifunk.android.discover.Main.java

private LatLng getLocation() {
    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();//w w  w .  j a v  a  2s  . c  om

    } else { // Google Play Services are available

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);
        return new LatLng(location.getLatitude(), location.getLongitude());
    }
    return new LatLng(0, 0);
}

From source file:org.akvo.flow.activity.GeoshapeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.geoshape_activity);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mFeatures = new ArrayList<>();
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    mFeatureMenu = findViewById(R.id.feature_menu);
    mFeatureName = (TextView) findViewById(R.id.feature_name);
    mClearPointBtn = findViewById(R.id.clear_point_btn);
    mClearPointBtn.setOnClickListener(mFeatureMenuListener);
    findViewById(R.id.add_point_btn).setOnClickListener(mFeatureMenuListener);
    findViewById(R.id.clear_feature_btn).setOnClickListener(mFeatureMenuListener);
    findViewById(R.id.properties).setOnClickListener(mFeatureMenuListener);

    mAllowPoints = getIntent().getBooleanExtra(ConstantUtil.EXTRA_ALLOW_POINTS, true);
    mAllowLine = getIntent().getBooleanExtra(ConstantUtil.EXTRA_ALLOW_LINE, true);
    mAllowPolygon = getIntent().getBooleanExtra(ConstantUtil.EXTRA_ALLOW_POLYGON, true);
    mManualInput = getIntent().getBooleanExtra(ConstantUtil.EXTRA_MANUAL_INPUT, true);

    initMap();//from   w ww .  j a va 2 s  . c om

    String geoJSON = getIntent().getStringExtra(ConstantUtil.GEOSHAPE_RESULT);
    if (!TextUtils.isEmpty(geoJSON)) {
        load(geoJSON);
    } else {
        // If user location is known, center map
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = manager.getBestProvider(criteria, true);
        if (provider != null) {
            Location location = manager.getLastKnownLocation(provider);
            if (location != null) {
                LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 10));
            }
        }
    }
}

From source file:org.cesar.geofencesdemo.ui.activities.MainActivity.java

private void setUpMapIfNeeded() {

    // Do a null check to confirm that we have not already instantiated the
    // map./*from ww w.  j  av a 2 s .  c  om*/
    if (mGoogleMap == null) {
        mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mGoogleMap != null) {
            // The Map is verified. It is now safe to manipulate the map.
            mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap.setOnCameraChangeListener(this);

            LatLng latLng = null;
            // If we have a previous location set, we go there
            if (mSimplegeofence != null) {
                latLng = new LatLng(mSimplegeofence.getLatitude(), mSimplegeofence.getLongitude());
            } else {
                // If we don't have a previous location set, we try to go to
                // the last known position, if it's
                // not possible, then we go to the 0.0, 0.0 location
                LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                String bestProvider = locationManager.getBestProvider(new Criteria(), true);
                Location location = locationManager.getLastKnownLocation(bestProvider);
                if (location != null) {
                    latLng = new LatLng(location.getLatitude(), location.getLongitude());
                } else {
                    latLng = new LatLng(0, 0);
                }
            }
            updateCamera(latLng);
        }
    }
}

From source file:be.artoria.belfortapp.fragments.MapFragment.java

private GeoPoint getCurrentLocation() {
    // Get the location manager
    final LocationManager locationManager = (LocationManager) getActivity().getApplicationContext()
            .getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default//ww w.jav a2 s  .c  o  m
    final Criteria criteria = new Criteria();
    final String provider = locationManager.getBestProvider(criteria, false);
    final Location loc = locationManager.getLastKnownLocation(provider);

    if (loc != null) {
        return new GeoPoint(loc.getLatitude(), loc.getLongitude());
    } else {
        return new GeoPoint(DataManager.BELFORT_LAT, DataManager.BELFORT_LON);
    }
}

From source file:com.cloudbees.gasp.activity.GaspLocationsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_locations);

    GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    LocationManager locationManager;
    String svcName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) getSystemService(svcName);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);//from   w  w  w. j  a  va  2 s  .co  m
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    String provider = locationManager.getBestProvider(criteria, true);

    Location location = locationManager.getLastKnownLocation(provider);
    Log.i(TAG, "CURRENT LOCATION");
    Log.i(TAG, "Latitude = " + location.getLatitude());
    Log.i(TAG, "Longitude = " + location.getLongitude());

    if (location != null) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Geocoder gc = new Geocoder(this, Locale.getDefault());

        if (!Geocoder.isPresent())
            Log.i(TAG, "No geocoder available");
        else {
            try {
                List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
                StringBuilder sb = new StringBuilder();
                if (addresses.size() > 0) {
                    Address address = addresses.get(0);

                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                        sb.append(address.getAddressLine(i)).append(" ");

                    sb.append(address.getLocality()).append("");
                    sb.append(address.getPostalCode()).append(" ");
                    sb.append(address.getCountryName());
                }
                Log.i(TAG, "Address: " + sb.toString());
            } catch (IOException e) {
                Log.d(TAG, "IOException getting address from geocoder", e);
            }
        }
    }

    map.setMyLocationEnabled(true);

    LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
    CameraPosition cameraPosition = new CameraPosition.Builder().target(myLocation).zoom(16).bearing(0).tilt(0)
            .build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    new LocationMapper().execute();
}

From source file:com.findcab.driver.activity.Signup.java

/**
 * ?GPS?/*from ww  w. j  av a2 s . c  o  m*/
 */
private void initLocation() {

    LocationManager locationManager;
    String serviceName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) this.getSystemService(serviceName);
    // ?
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {

        lat = location.getLatitude();
        lng = location.getLongitude();

    }
}

From source file:com.findcab.driver.activity.Signup.java

private void initView() {
    context = this;
    back = (Button) findViewById(R.id.back);
    back.setOnClickListener(this);
    start = (Button) findViewById(R.id.start);
    start.setOnClickListener(this);

    edit_name = (EditText) findViewById(R.id.name);
    edit_mobile = (EditText) findViewById(R.id.mobile);
    edit_password = (EditText) findViewById(R.id.password);

    checkBox = (CheckBox) findViewById(R.id.checkBox);

    item = (TextView) findViewById(R.id.item);
    item.setOnClickListener(this);

    if (initGPS()) {

        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) this.getSystemService(serviceName);
        // ?/* ww  w  .  j a  va2 s .co  m*/
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
        }
    }
}

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void registerForGPS() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);//from  w  ww  . ja v a2  s.  com
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    LocationManager locationManager = ((LocationManager) getSystemService(Context.LOCATION_SERVICE));
    String provider = locationManager.getBestProvider(criteria, true);

    if (provider != null) {
        locationManager.requestLocationUpdates(provider, MIN_LOCATION_TIME, MIN_LOCATION_DISTANCE,
                qiblaManager);
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_LOCATION_TIME,
            MIN_LOCATION_DISTANCE, qiblaManager);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_LOCATION_TIME,
            MIN_LOCATION_DISTANCE, qiblaManager);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null) {
        location = ((LocationManager) getSystemService(Context.LOCATION_SERVICE))
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    if (location != null) {
        qiblaManager.onLocationChanged(location);
    }

}

From source file:com.example.s_geomapsample.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample_map);

    mLatitude = (TextView) findViewById(R.id.latitude);
    mAccuracy = (TextView) findViewById(R.id.accuracy);
    mLongitude = (TextView) findViewById(R.id.longitude);

    mProvider = (TextView) findViewById(R.id.t01);

    mHandler = new Handler();

    // mApp = (ApplicationClass) getApplication();
    // mApp.setOnNotifiedMessageListener(mNotifiedMessageListener);
    // mLocationManager = new LocationServiceManager(getApplication());

    upReceiver = new UpdateReceiver();
    intentFilter = new IntentFilter();
    intentFilter.addAction("UPDATE_ACTION");
    registerReceiver(upReceiver, intentFilter);

    upReceiver.registerHandler(updateHandler);

    // ?/*  w ww . ja  v a2 s.c  o  m*/
    Button setParamButton = (Button) findViewById(R.id.param);
    setParamButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mGeoFenceManager = new GeoFenceManager(MainActivity.this);
            HashMap<String, Object> defaultMap = new HashMap<String, Object>();
            {
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_CLIENTID, "JSZ3a03eb052f46|E7Tg6");
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_TMID, "t1000_1");
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_SECRET, "eNphiW-zYeDhnj37IL86bJeijmA");
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_TNTP, "1");
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_TATP, "1");
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_NCURL,
                        "https://test-mlp.its-mo.com/mlp/v1_0/request/SearchNotifyConditionList.php");
                defaultMap.put(GeoFenceManager.REQUEST_PARAM_KEY_AIURL,
                        "https://test-mlp.its-mo.com/mlp/v1_0/request/SearchAreaInfoList.php");
                defaultMap.put("SAVE_INTERVAL", "2");
            }

            int result = mGeoFenceManager.setParams(defaultMap, 1);
            if (result == GeoFenceManager.SET_PARAMS_RESULT_CODE_SUCCESS) {
                Toast.makeText(MainActivity.this, "SET_PARAMS_RESULT_CODE_SUCCESS", Toast.LENGTH_LONG).show();
            } else if (result == GeoFenceManager.SET_PARAMS_RESULT_CODE_NO_REQUIRED) {
                Toast.makeText(MainActivity.this, "SET_PARAMS_RESULT_CODE_NO_REQUIRED", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });

    // Area
    Button updateButton = (Button) findViewById(R.id.update);
    updateButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            int updateAreaInfomation = mGeoFenceManager.updateAreaInfomation();
            Toast.makeText(MainActivity.this, "updateAreaInfomation = " + updateAreaInfomation,
                    Toast.LENGTH_LONG).show();
            // UpdateAreaAsync areaAsync = new
            // UpdateAreaAsync(MainActivity.this, mGeoFenceManager);
            // areaAsync.execute();
        }
    });
    // GPS???
    Button gpsButton = (Button) findViewById(R.id.gps_button);
    gpsButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LocationServiceManager locationServiceManager = new LocationServiceManager(MainActivity.this);
            locationServiceManager.startLocationService();
        }
    });

    // Google Play Services???????
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if (status != ConnectionResult.SUCCESS) {
        // Google Play Services ?????
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    } else {
        // Google Play Services ???
        // activity_main.xml?SupportMapFragment????
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // fragment?GoogleMap object?
        gMap = fm.getMap();

        // Google Map?MyLocation???
        gMap.setMyLocationEnabled(true);
        gMap.setIndoorEnabled(true);
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.setTrafficEnabled(true);

        // ?LOCATION_SERVICE?LocationManager object?
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // retrieve provider?criteria object?
        Criteria criteria = new Criteria();

        // Best provider?????
        String provider = locationManager.getBestProvider(criteria, true);

        // ???
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, this);

    }

}