开发者

general gps question in android

I've been messing around with gps functionality for a few weeks now and I have a problem. My goal is to save the gps location on a button click, however I hear that it takes some time for the gps to listen to the satellites to get your precise location. My program takes less than a second to record its location, making me believe its not actually taking its time. The location it records is pretty far off, and sometimes it doesn't even listen for new locations anymore, it just tells me I am at a place I was a few hours ago. What should I do, right now I call requestLocationupdates(Location,0,0,loc_listener) right after I click the button, should I be putting this elsewhere?(such as right after I declare my Location Manager?)

I would really appreciate any help. Thanks

This is in my on create method

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    loc_listener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub
            float locationAccuracy = location.getAccuracy();
            if (locationAccuracy > 150) {
                gpsStatus.setImageResource(gpsColor[0]);
            } else if (locationAccuracy > 100) {
            开发者_运维知识库    gpsStatus.setImageResource(gpsColor[1]);
            } else {
                gpsStatus.setImageResource(gpsColor[2]);
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub

        }
    };

I call this in my onbuttonclick method

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 30000, 0,
                loc_listener);

and

holder.put("lat", location.getLatitude());
holder.put("lon", location.getLongitude());

What should I be doing instead? Thanks


hey i think it is useful to you http://mobiforge.com/developing/story/using-google-maps-android


Why aren't you using the function:

public void onLocationChanged(Location location) {
   // TODO Auto-generated method stub
}

to know when the location changes? You can track it yourself anytime there is a change and use the tracked Location when you click the button instead of requesting updates again.

From the docs:

http://developer.android.com/reference/android/location/LocationListener.html#onStatusChanged%28java.lang.String,%20int,%20android.os.Bundle%29

onStatusChanged is for when the GPS goes online/offline/temporarily unavailable/etc


You need to layer in your search in order to get the best position. Right now you are solely using the GPS provider and utilizing the Network provider to try and locate your location. Layered GPS location is described in Obtaining User's Location section "Flow for obtaining user location"

Now pertaining to your question, on button click:

  • You need to first check the cache first and see if that cache is too old to use before. Use your best provider to do so. Here is the skeleton code for you to start with


LocationManager lm = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);  
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = lm.getBestProvider(criteria, true);
Location locate = lm.getLastKnownLocation(bestProvider);

if (locate != null) {
    // if current location is already within time to expire apart, then get current location
    // Check if we have cached location in the last 2 minutes, use that cached if we have one
    if(System.currentTimeMillis() - locate.getTime() > TIME_TO_EXPIRE) {
        // cache is too old and we assume the user has moved since then, initiate GPS location update
    } else {
        // cache is not too old, we could call the locationListener here itself
        locationListener.onLocationChanged(locate);
    }
}  else {
    // Use GPS if no known cache is available. Wait for 7.5 secs for GPS lock
}
  • If we don't get the cache that is not too old, we need to start listening for updates, let's start with the GPS

lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        MIN_TIME, 
        MIN_DISTANCE, 
        locationListener);
  • You need to time the GPS search and see if you actually lock and get location update within reasonable time bound. If within certain time limit, you don't get your GPS lock and location update, you could assume that the GPS cannot obtain your position and proceed searching your location using the Network provider. You could use Timer.schedule to schedule the timeout before going to the next layer of location search.
    • If you obtain a lock to GPS and get a location update, you want to first compare if this location is indeed better than the previous one that you have obtained via getLastKnownLocation. The Android Developer's link above has a a sample on how to determine that on section "Maintaining a current best estimate"
    • Otherwise, if certain time has elapsed, you may decide to go listening for update on based on NetworkProvider. If this is not an option for you app, you need to alert the user that the current location cannot be located.
  • If you decide to look for your location based on NetworkProvider then you would put the next line of code

// remove the previous listeners based on GPS
lm.removeUpdates(locationListener);

// now request a new one based on the network
lm.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 
        MIN_TIME, 
        MIN_DISTANCE, 
        locationListener);
  • You would then again initiate a timer. If a location hasn't been acquired after the timer has elapsed, then you would have to report that the location cannot be acquired to the user or use the old cached location (if it is available) as you have exhausted all of your options to get a position fix


If I were you I'd use MyLocationOverlay, which is meant for getting the user's location.

You can then use getMyLocation() when it gets the location.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜