Android: location detection by Cellular Network and WiFi

倖福魔咒の 提交于 2019-12-13 03:18: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.



来源:https://stackoverflow.com/questions/11592520/android-location-detection-by-cellular-network-and-wifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!