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

前端 未结 8 1522
隐瞒了意图╮
隐瞒了意图╮ 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:26

    Here is the class LocationFinder to find the GPS location. This class will call MyLocation, which will do the business.

    LocationFinder

    public class LocationFinder extends Activity {
    
        int increment = 4;
        MyLocation myLocation = new MyLocation();
    
        // private ProgressDialog dialog;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.intermediat);
            myLocation.getLocation(getApplicationContext(), locationResult);
    
            boolean r = myLocation.getLocation(getApplicationContext(),
                locationResult);
    
            startActivity(new Intent(LocationFinder.this,
            // Nearbyhotelfinder.class));
                GPSMyListView.class));
            finish();
        }
    
        public LocationResult locationResult = new LocationResult() {
    
            @Override
            public void gotLocation(Location location) {
                // TODO Auto-generated method stub
                double Longitude = location.getLongitude();
                double Latitude = location.getLatitude();
    
                Toast.makeText(getApplicationContext(), "Got Location",
                    Toast.LENGTH_LONG).show();
    
                try {
                    SharedPreferences locationpref = getApplication()
                        .getSharedPreferences("location", MODE_WORLD_READABLE);
                    SharedPreferences.Editor prefsEditor = locationpref.edit();
                    prefsEditor.putString("Longitude", Longitude + "");
                    prefsEditor.putString("Latitude", Latitude + "");
                    prefsEditor.commit();
                    System.out.println("SHARE PREFERENCE ME PUT KAR DIYA.");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
    
        // handler for the background updating
    
    }
    

    MyLocation

    public class MyLocation {
    
        Timer timer1;
        LocationManager lm;
        LocationResult locationResult;
        boolean gps_enabled=false;
        boolean network_enabled=false;
    
        public boolean getLocation(Context context, LocationResult result)
        {
            //I use LocationResult callback class to pass location value from MyLocation to user code.
            locationResult=result;
            if(lm==null)
                lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    
            //exceptions will be thrown if provider is not permitted.
            try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
            try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
    
            //Toast.makeText(context, gps_enabled+" "+network_enabled,     Toast.LENGTH_LONG).show();
    
            //don't start listeners if no provider is enabled
            if(!gps_enabled && !network_enabled)
                return false;
    
            if(gps_enabled)
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
            if(network_enabled)
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
            timer1=new Timer();
    
    
            timer1.schedule(new GetLastLocation(), 10000);
        //    Toast.makeText(context, " Yaha Tak AAya", Toast.LENGTH_LONG).show();
            return true;
        }
    
        LocationListener locationListenerGps = new LocationListener() {
            public void onLocationChanged(Location location) {
                timer1.cancel();
                locationResult.gotLocation(location);
                lm.removeUpdates(this);
                lm.removeUpdates(locationListenerNetwork);
            }
            public void onProviderDisabled(String provider) {}
            public void onProviderEnabled(String provider) {}
            public void onStatusChanged(String provider, int status, Bundle extras) {}
        };
    
        LocationListener locationListenerNetwork = new LocationListener() {
            public void onLocationChanged(Location location) {
                timer1.cancel();
                locationResult.gotLocation(location);
                lm.removeUpdates(this);
                lm.removeUpdates(locationListenerGps);
            }
            public void onProviderDisabled(String provider) {}
            public void onProviderEnabled(String provider) {}
            public void onStatusChanged(String provider, int status, Bundle extras) {}
        };
    
        class GetLastLocation extends TimerTask {
            @Override
    
            public void run() {
    
                //Context context = getClass().getgetApplicationContext();
                 Location net_loc=null, gps_loc=null;
                 if(gps_enabled)
                     gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                 if(network_enabled)
                     net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
                 //if there are both values use the latest one
                 if(gps_loc!=null && net_loc!=null){
                     if(gps_loc.getTime()>net_loc.getTime())
                         locationResult.gotLocation(gps_loc);
                     else
                         locationResult.gotLocation(net_loc);
                     return;
                 }
    
                 if(gps_loc!=null){
                     locationResult.gotLocation(gps_loc);
                     return;
                 }
                 if(net_loc!=null){
                     locationResult.gotLocation(net_loc);
                     return;
                 }
                 locationResult.gotLocation(null);
            }
        }
    
        public static abstract class LocationResult{
            public abstract void gotLocation(Location location);
        }
    }
    

提交回复
热议问题