how to get gps location android

前端 未结 5 1467
有刺的猬
有刺的猬 2020-12-08 17:32

I am trying to have a constant gps listener that will send its location (long and lat coordinates) to a web server every x mins. On a button click it will also send its loca

相关标签:
5条回答
  • 2020-12-08 18:03

    There are various methods you could use. You could use a separate thread that waits for x minutes and then sends the latest known location to the server. Or you use a Service that does more or less the same. As a third possibility you could also use a Handler.

    0 讨论(0)
  • 2020-12-08 18:08

    this code works for me (api 17)

    try {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
        lat = location.getLatitude();
            lon = location.getLongitude();
    
    
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-12-08 18:18

    You can create a thread that will run in background and every x minutes you get the actual position by calling a function that does that (Note that you want to make a function that get the x,y coordinates since you will use that on button click aswel). For the code you posted :

    locationManager.requestUpdates(provider, minTime, minDistance, intent);
    

    That means that your application will send a request to gps module every x min for the whatshouldIputhere?

    Good luck, Arkde

    0 讨论(0)
  • 2020-12-08 18:24

    I've got this working:

    private void _getLocation() {
        // Get the location manager
        LocationManager locationManager = (LocationManager) 
                getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        try {
            lat = location.getLatitude();
            lon = location.getLongitude();
        } catch (NullPointerException e) {
            lat = -1.0;
            lon = -1.0;
        }
    }
    

    It's simple. It gets the best available provider and gets its last known position.
    If you want it only with the GPS, try this.

    Hope it helps!

    EDITED:

    try this:

    private void _getLocation() {
        // Get the location manager
        LocationManager locationManager = (LocationManager) 
                getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        LocationListener loc_listener = new LocationListener() {
    
            public void onLocationChanged(Location l) {}
    
            public void onProviderEnabled(String p) {}
    
            public void onProviderDisabled(String p) {}
    
            public void onStatusChanged(String p, int status, Bundle extras) {}
        };
        locationManager
                .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
        location = locationManager.getLastKnownLocation(bestProvider);
        try {
            lat = location.getLatitude();
            lon = location.getLongitude();
        } catch (NullPointerException e) {
            lat = -1.0;
            lon = -1.0;
        }
    }
    

    This code gets the last known location and then do a request for the actual location.

    0 讨论(0)
  • 2020-12-08 18:24

    One method is to use Timer and TimerTask

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new SendLocationTask(), 0, 60000);
    
    class SendLocationTask extends TimerTask{
        public abstract void run(){
            // send position info here
        }
    }
    
    0 讨论(0)
提交回复
热议问题