Android: location detection by Cellular Network an

2019-08-27 03:30发布

问题:

I would like to build a app which will provide current location using cellular Network and WiFi.

I like to get location from both provider simultaneously and then decide which provider give me best.

Now i am getting location from Network

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationlistenerforNetwork = new mylocationlistenerNetwork();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationlistenerforNetwork);

private class mylocationlistenerNetwork implements LocationListener {
      @Override
      public void onLocationChanged(Location location) {
          if (location != null) {
          Log.d("LOCATION CHANGED", location.getLatitude() + " ");
          Log.d("LOCATION CHANGED", location.getLongitude() + " ");
          Toast.makeText(LocationActivity.this,"latitude: "+
              location.getLatitude() + "longitude: " + location.getLongitude()
              + " Provider:" + location.getProvider() + " Accuracy:" + location.getAccuracy(),
              Toast.LENGTH_LONG).show();

          }
      }
      @Override
      public void onProviderDisabled(String provider) {
      }
      @Override
      public void onProviderEnabled(String provider) {
      }
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {}
      }

I also want location from WiFi. Please help.

回答1:

See LocationManager

There are these providers:

  • GPS_PROVIDER
  • NETWORK_PROVIDER
  • PASSIVE_PROVIDER

From docs:

public static final String NETWORK_PROVIDER

Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.

There is no separate provider for WiFi. Using NETWORK_PROVIDER you are determining location also base on WiFi access points.