title: Get accurate current location from Network Provider
body: <p>I use the following code to get Current Location from a Network provider in my application:</p>
  
  <pre><code>LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
  boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  if(network_enabled){
  Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  </code></pre>
  
  <p>But it gives a location approximately 300-700 meters away from my actual location.</p>
  
  <p><strong>This is expected from a Network provider. But the problem is:</strong></p>
  
  <blockquote>
    <p>with only this Network provider enabled, and no GPS, I openned Foursquare
    application where it shows my current location exactly where I am. Now
    when I come back to my application, it shows the accurate current
    location or say the same location which Foursquare showed.</p>
  </blockquote>
  
  <p>Same thing happens with Google apps like Navigator, Maps etc..,</p>
  
  <p>How can this be done? How are other apps able to get the exact location, based on just the Network provider?</p>
  
  <p><strong>Complete Code:</strong></p>
  
  <pre><code>public class MyLocationActivity extends Activity implements LocationListener {
      private LocationManager mgr;
      private String best;
      Location location;
      public static double myLocationLatitude;
      public static double myLocationLongitude;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
          Criteria criteria = new Criteria();
          best = mgr.getBestProvider(criteria, true);
          location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
          dumpLocation(location);
      }
  
      public void onLocationChanged(Location location) {
  
          dumpLocation(location);
      }
  
      public void onProviderDisabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      public void onProviderEnabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      public void onStatusChanged(String provider, int status, Bundle extras) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      protected void onPause() {
  
          super.onPause();
          mgr.removeUpdates(this);
      }
  
      @Override
      protected void onResume() {
  
          super.onResume();
          mgr.requestLocationUpdates(best, 15000, 10, this);
      }
  
      private void dumpLocation(Location l) {
  
          if (l != null){
  
              myLocationLatitude = l.getLatitude();
              myLocationLongitude = l.getLongitude();
          }
      }
  }
  </code></pre>
  
  <p>Thank You</p>