android - getLastKnownLocation always returns null -
after reviewed articles on stackoverflow, not solutions question posting similar type of issue here. usual, getting null in return of getlastknownlocation
. have implemented locationlistener well. below code.
for information, have checked both providers (network , gps) in below code , returning false if check value of sbestprovider getting "network". how coming not know.
hope, out me.
quick response appreciated. friends.
permissions.
<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"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" />
java code
public class activitymarkinglocation extends activity implements locationlistener { googlemap omap ; locationmanager olocationmanager; connectiondetector oconnectiondetector ; location olocation; button obtngetcurrentlocation; string sbestprovider; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_marking_location); olocationmanager = (locationmanager) getsystemservice(context.location_service); criteria ocriteria = new criteria(); sbestprovider = olocationmanager.getbestprovider(ocriteria, false); boolean b =olocationmanager.isproviderenabled(locationmanager.gps_provider); system.out.println("gps status " + b); boolean c = olocationmanager.isproviderenabled(locationmanager.network_provider); system.out.println("network status " + c); system.out.println("provider " + sbestprovider); olocation = olocationmanager.getlastknownlocation(sbestprovider); system.out.println("provider : " + sbestprovider); system.out.println("locationmanager : " + olocationmanager); if (olocation == null) system.out.println("location null " ); else system.out.println("location not null"); obtngetcurrentlocation = (button) findviewbyid(r.id.btngetcurrentlocation); obtngetcurrentlocation.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { olocation = olocationmanager.getlastknownlocation(sbestprovider); if (olocation != null) system.out.println("location found"); if (olocation != null) system.out.println("position : " + olocation.getlatitude() + " " + olocation.getlongitude()); } }); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); olocationmanager.requestlocationupdates(sbestprovider, 600, 10, this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_landing_screen, menu); return true; } public boolean isgooglemapsinstalled() { try { applicationinfo info = getpackagemanager().getapplicationinfo("com.google.android.apps.maps", 0 ); return true; } catch(packagemanager.namenotfoundexception e) { return false; } } @override public void onlocationchanged(location arg0) { // todo auto-generated method stub olocation = arg0; system.out.println("location changed"); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
edit: try solution you
are running app on emulator or on mobile phone ? if running on emulator have simulate gps input vitual device. extended answer you connect console open command line , type:
telnet localhost 5554
you can use geo command set latitude, longitude , if needed altitude on device passed programs using gps location provider. see link above further instructions.
the specific command run in console is:
geo fix <longitude value> <latitude value>
maybe helpful link here
Comments
Post a Comment