android - Accessing GPS is not working. Application uses network to getting location -
my app doesnt want use gps , using network location instead. checked analysing latitude , longtitude, they're little bit random (like network location).
if use app using gps (e.g endomondo sports tracker) see characteristic mark, means, gps working:
when launch application, there's no mark @ all.
i'm using following code (code lars vogella, source: http://www.vogella.com/articles/androidlocationapi/article.html):
import android.app.activity; import android.content.context; import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.widget.textview; import android.widget.toast; public class showlocationactivity extends activity implements locationlistener { private textview latitutefield; private textview longitudefield; private locationmanager locationmanager; private string provider; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); latitutefield = (textview) findviewbyid(r.id.textview02); longitudefield = (textview) findviewbyid(r.id.textview04); // location manager locationmanager = (locationmanager) getsystemservice(context.location_service); // define criteria how select locatioin provider -> use // default criteria criteria = new criteria(); provider = locationmanager.getbestprovider(criteria, false); location location = locationmanager.getlastknownlocation(provider); // initialize location fields if (location != null) { system.out.println("provider " + provider + " has been selected."); onlocationchanged(location); } else { latitutefield.settext("location not available"); longitudefield.settext("location not available"); } } /* request updates @ startup */ @override protected void onresume() { super.onresume(); locationmanager.requestlocationupdates(provider, 400, 1, this); } /* remove locationlistener updates when activity paused */ @override protected void onpause() { super.onpause(); locationmanager.removeupdates(this); } @override public void onlocationchanged(location location) { int lat = (int) (location.getlatitude()); int lng = (int) (location.getlongitude()); latitutefield.settext(string.valueof(lat)); longitudefield.settext(string.valueof(lng)); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { toast.maketext(this, "enabled new provider " + provider, toast.length_short).show(); } @override public void onproviderdisabled(string provider) { toast.maketext(this, "disabled provider " + provider, toast.length_short).show(); } }
of course added permissions in androidmanifest.xml
file:
<uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.internet" />
any ideas?
solution: can use gps
instead of selecting best provider deciding criteria.
example, replace:
locationmanager.requestlocationupdates(provider, 400, 1, this);
with:
locationmanager.requestlocationupdates(locationmanager.gps_provider, 400, 1, this);
explaination:
depending on application's use case, have choose specific location provider i.e. locationmanager.gps_provider
or locationmanager.network_provider
alternatively, can provide input criteria such accuracy, power requirement, monetary cost, , on, , let android decide closest match location provider
// retrieve list of location providers have fine accuracy, no monetary cost, etc criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setcostallowed(false); string providername = locmanager.getbestprovider(criteria, true); //and can make location update request selected best provider locationmanager.requestlocationupdates(provider, 400, 1, this);
have @ how use locationmanager , how specify criteria , how getbestprovider method works reference
Comments
Post a Comment