Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I stop the user waiting for a long time to get a location fix?
    primarykey
    data
    text
    <p>I am building a GPS Android application which gets the nearest places based on the user's current location. </p> <p>This is what my application does:</p> <ul> <li>Check if GPS or Network is available</li> <li>If neither is available then don't do anything. Else, we first check for GPS if it's there, if not then we check for Network. </li> <li>After using one of them, we get the current location and then send it off to the server. </li> <li>Once we retrieve data from the server and update the UI, we stop listening for further location updates. Once is enough, until they press the refresh button which starts this again.</li> </ul> <p>What I hope to do:</p> <ul> <li><p>If GPS or network fails to retrieve a location, for example, 2 minutes, then we switch providers. We don't want the user to wait too long. </p></li> <li><p>It would also be nice to be able to use both providers and then get the most accurate from that. I've taken a look at <a href="http://developer.android.com/guide/topics/location/strategies.html" rel="nofollow">http://developer.android.com/guide/topics/location/strategies.html</a> and I saw the isBetterLocation method. How would I integrate this method in my application? I'm having trouble understand how, where and when it should be called. I assume that the isBetterLocation() requires me to call both Network and GPS at the same time. It would be nice for my application to listen to both for accuracy. How do I do this? What if one of them isn't available?</p></li> </ul> <p>Here's parts of my code:</p> <pre><code>if(!GPSEnabled &amp;&amp; !networkEnabled) { Toast.makeText(this, "Error: This application requires a GPS or network connection", Toast.LENGTH_SHORT).show(); } else { if(GPSEnabled) { locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } else if(networkEnabled) { System.out.println("Getting updates from network provider"); locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); } } </code></pre> <p>This is the <code>onLocationChanged</code> method. I get the lat/lng values and then send them off to my server and then do appropriate stuff with it. </p> <pre><code>public void onLocationChanged(Location location) { //Get coordinates double lat = (location.getLatitude()); double lng = (location.getLongitude()); Log.d("MainActivity", "got location: " + lat + ": " + lng); //get nearest locations new GetLocations().execute(SharedVariables.root + SharedVariables.locationsController + SharedVariables.getNearestMethod + lat + "/" + lng); // Zoom in, animating the camera after the markers have been placed map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10)); System.out.println("lat = " + lat + ", lng = " + lng); //Stop listening for updates. We only want to do this once. locManager.removeUpdates(this); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload