How to get Latitude and Longitude of the mobile device in android?

前端 未结 8 1424
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 16:09

How do I get the current Latitude and Longitude of the mobile device in android using location tools?

8条回答
  •  粉色の甜心
    2020-11-22 16:18

    you can got Current latlng using this

    `

      public class MainActivity extends ActionBarActivity {
      private LocationManager locationManager;
      private String provider;
      private MyLocationListener mylistener;
      private Criteria criteria;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
                 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                  // Define the criteria how to select the location provider
                  criteria = new Criteria();
                  criteria.setAccuracy(Criteria.ACCURACY_COARSE);   //default
    
                  // user defines the criteria
    
                  criteria.setCostAllowed(false); 
                  // get the best provider depending on the criteria
                  provider = locationManager.getBestProvider(criteria, false);
    
                  // the last known location of this provider
                  Location location = locationManager.getLastKnownLocation(provider);
    
                  mylistener = new MyLocationListener();
    
                  if (location != null) {
                      mylistener.onLocationChanged(location);
                  } else {
                      // leads to the settings because there is no last known location
                      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                      startActivity(intent);
                  }
                  // location updates: at least 1 meter and 200millsecs change
                  locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
                String a=""+location.getLatitude();
                Toast.makeText(getApplicationContext(), a, 222).show();
    
    }
    
    private class MyLocationListener implements LocationListener {
    
          @Override
          public void onLocationChanged(Location location) {
            // Initialize the location fields
    
    
    
              Toast.makeText(MainActivity.this,  ""+location.getLatitude()+location.getLongitude(),
                        Toast.LENGTH_SHORT).show()  
    
          }
    
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
              Toast.makeText(MainActivity.this, provider + "'s status changed to "+status +"!",
                        Toast.LENGTH_SHORT).show();
          }
    
          @Override
          public void onProviderEnabled(String provider) {
              Toast.makeText(MainActivity.this, "Provider " + provider + " enabled!",
                Toast.LENGTH_SHORT).show();
    
          }
    
          @Override
          public void onProviderDisabled(String provider) {
              Toast.makeText(MainActivity.this, "Provider " + provider + " disabled!",
                Toast.LENGTH_SHORT).show();
          }
      }         
    

    `

提交回复
热议问题