Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

how to get gps location android

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 location to the webserver. I realize that to get the gps signal you type in how often to find a position, but how do I write a program that can get the gps location and send its coordinates every x mins (even in the background when not and by a button press?

//in the on click

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, whatshouldIputhere?, 0, this);

and

public void onLocationChanged(Location location) {
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...