简体   繁体   中英

Android Location Manager Takes A Long Time Finding Longitude and Latitude

The code I have works when it feels like it. It will either work straight away or will take a very long time, or not work at all. The code is as follow:

 locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 locationLis = new LocationListener() {
      @Override
      public void onLocationChanged(Location location) {
           lat= location.getLatitude();
           log = location.getLongitude();
           locationManager.removeUpdates(locationLis);
      }
      @Override
      public void onStatusChanged(String s, int i, Bundle bundle){
      }
      @Override
      public void onProviderEnabled(String s) {
      }
      @Override
      public void onProviderDisabled(String s) {
           System.out.println("Not Found");
      }
 };

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationLis);

The device I am using to test is a Nexus 7 (2012)

Rather than use the GPS_PROVIDER I changed it to NETWORK_PROVIDER .

This used the network to get the location rather than the GPS which is what took so long. The code was:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationLis);

and now reads:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationLis);

That may be because your GPS is not getting fixed. Check for the GPS icon, It may be blinking. Thats purely because the GPS is not getting Fixed.

So, I strongly recommend you to test the app under Open Sky . This helps in fixing the GPS faster. And while testing under open sky, you can see that the GPS icon will stop blinking and remains steady.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM