java - Longitude and Latitude showing 0.0 -
when click button shows latitude , longitude of 0.0. ideas why? followed online tutorial.
if there better way current location of user please let me know , sure it. p.s. not have knowledge in java , android.
thanks everyone. issue fact did not have permissions in correct area. help!
package com.example.android.getgpslocation; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.textview; public class mainactivity extends actionbaractivity { button btnshowlocation; gpstracker gps; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btnshowlocation= (button) findviewbyid(r.id.show_location); btnshowlocation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { gps = new gpstracker(mainactivity.this); if(gps.cangetlocation()){ double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); final textview mtextview = (textview) findviewbyid(r.id.location); mtextview.settext("latitude "+latitude+" longitude "+longitude); }else{ gps.showsettingsalert(); } } }); } }
gpstracker.java
package com.example.android.getgpslocation; import android.app.alertdialog; import android.app.service; import android.bluetooth.bluetoothclass; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.ibinder; import android.provider.settings; import java.security.provider; /** * created alex on 7/1/2015. */ public class gpstracker extends service implements locationlistener{ private final context context; boolean isgpsenabled = false; boolean cangetlocation = false; boolean isnetworkenabled = false; location location; double latitude; double longitude; private static final long min_distance_change_for_updates = 10; private static final long min_distance_bw_updates = 1000 * 60 * 1; protected locationmanager locationmanager; public gpstracker(context context){ this.context=context; getlocation(); } public location getlocation(){ try{ locationmanager = (locationmanager) context.getsystemservice(location_service); isgpsenabled = locationmanager.isproviderenabled(locationmanager.gps_provider); isnetworkenabled = locationmanager.isproviderenabled(locationmanager.network_provider); if(!isgpsenabled && !isnetworkenabled){ }else{ this.cangetlocation = true; if(isnetworkenabled){ locationmanager.requestlocationupdates(locationmanager.network_provider, min_distance_change_for_updates, min_distance_bw_updates, this); } if(locationmanager != null){ location = locationmanager.getlastknownlocation(locationmanager.network_provider); if(location !=null){ latitude=location.getlatitude(); longitude=location.getlongitude(); } } } if (isgpsenabled){ if (location == null){ locationmanager.requestlocationupdates(locationmanager.gps_provider, min_distance_bw_updates, min_distance_change_for_updates, this); if (locationmanager != null){ location = locationmanager.getlastknownlocation(locationmanager.gps_provider); if(location!=null){ latitude = location.getlatitude(); longitude=location.getlongitude(); } } } } }catch (exception e){ e.printstacktrace(); } return location; } public void stopusinggps(){ if (locationmanager != null) { locationmanager.removeupdates(gpstracker.this); } } public double getlatitude(){ if(location !=null){ latitude=location.getlatitude(); } return latitude; } public double getlongitude(){ if(location !=null){ longitude=location.getlongitude(); }return longitude; } public boolean cangetlocation(){ return this.cangetlocation; } public void showsettingsalert(){ alertdialog.builder alertdialog = new alertdialog.builder(context); alertdialog.settitle("gps settings"); alertdialog.setmessage("gps not enabled, please enable"); alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { intent intent = new intent(settings.action_location_source_settings); context.startactivity(intent); } }); alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog.show(); } @override public void onlocationchanged(location location) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } @override public ibinder onbind(intent intent) { return null; } }
android manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.getgpslocation" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.internet"/> <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
locationmanager outdated api. use newer fusedlocationproviderapi
Comments
Post a Comment