GPS Coordinates on Test Developer Phone - android

My code is taking coordinates of a position at one given time, and then displaying Lat/Long coordinates. I think my code is right, but where the problem lies is giving GPS permission to the phone. I am using a Samsung Nexus S, and I have all proper drivers installed, but there is no network on the phone. I was told that GPS should still work and should be able to retrieve coordinates. I have the, "Use GPS Satellites" setting enabled, and have given the permission in the manifest, as seen here in the manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
I threw in all possible location permissions just in case I was missing something, hopefully that isn't a problem.
As for my code I have this in the onCreate method (class? I'm not sure of its proper name)
double[] gps = getGPS();
TextView tv = (TextView) this.findViewById(R.id.gpsCoordinates);
tv.setText("Latitude: " + gps[0] + "\nLongitude: " + gps[1]);
And then a private method in the GPS class to actually get the coordinates
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
I believe I pulled this code off of stackoverflow and a few other forums and then tweaked it a bit.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.LOCATION"></uses-permission>
Add above permissions.May be this will help you.

You need to put the following permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Ref:
http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION

Related

Trying to get latitude and latitude with Network provider

I am trying to get my current coordinates with network provider and not gps provider.
I was able to figure out the solution for that but I am a bit confused with the concept in this scenario.
Working Code
Here's my code for getting my coordinates:
public void getLocation(){
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
if(appUtils.isOnline()){
try{
Geocoder geocoder = new Geocoder(
MainActivity.this.getApplicationContext(),
Locale.getDefault());
Location locationNetwork = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
List<Address> list;
if(locationNetwork!=null){
Toast.makeText(context,"Network Available!!",Toast.LENGTH_LONG).show();
list = geocoder.getFromLocation(locationNetwork.getLatitude(),locationNetwork.getLongitude(),3);
if(list!=null&&list.size()>0){
latitude = list.get(0).getLatitude();
longitude = list.get(0).getLongitude();
Toast.makeText(context,String.valueOf(latitude) + " (....) " + String.valueOf(longitude),Toast.LENGTH_LONG).show();
int count = 0;
while (latitude==null||longitude==null){
latitude = list.get(count).getLatitude();
longitude = list.get(count).getLongitude();
count++;
Toast.makeText(context,String.valueOf(latitude) + " --- " + String.valueOf(longitude),Toast.LENGTH_LONG).show();
}
}
}else{
Toast.makeText(context,"No response!!",Toast.LENGTH_LONG).show();
}
}catch (IOException e){
e.printStackTrace();
}
}else{
Toast.makeText(context,"Server not responding",Toast.LENGTH_LONG).show();
}
}
This piece of code is working perfectly fine when the gps is enabled. If gps is disabled, it doesn't work.
Now, if we are setting the location to NETWORK_PROVIDER:
Location locationNetwork = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Why do we still require gps ?
Now if I change it to PASSIVE PROVIDER:
Location locationNetwork = locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
It works fine with the passive provider. Why is it that ?
Can someone explain what is the major difference here and what would be the right way to get the coordinates with network provider ?
I know this question is been asked several times and I did went through it. I just want to get cleared with this concept.
Thank's in advance.. :)
It doesn't require GPS to use the network provider, I've done it many times. However, getLastKnowLocation may not return a value if either it has never had an app request updates for that provider, or if the last time that happened was too long ago. You cannot count on that function always returning non-NULL. If you want to ensure that you get a location, use requestSingleUpdate instead. This will always get you a location (assuming the provider you use is enabled), but may take some time- a result may not be immediately available.
(There is one other time that function may never return- if you use the GPS provider and it can't get a lock on enough sattelites to find a location. Such as if you're in an underground parking garage).
This is the bit of code that I use to quickly get the current location, by checking all available network options.
private double[] getGPS(){
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}

Can't get any location - android

i got a problem:
I can't get any location, other than Location.getLastKnownLocation(), which is not satisfying, because i need current location. Precision doesn't have to be well, 300 meters would be ok. My code looks like this:
This is the part I run when a button is pressed:
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
This is my onLocationChanged() method:
#Override
public void onLocationChanged(Location location) {
lati = location.getLatitude();
longi = location.getLongitude();
locRes.setText("Lati: " + lati + " Longi: " + longi);
lm.removeUpdates(this);
}
I defined:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
In the manifest file. I've been looking everywhere on the internet and can't find solution to my problem. Location just doesn't get fixed. Before, when i was trying to use gps location i also couldn't get any. Gps wasn't even working, because there was no icon of gps working on the top bar of my phone. Have anyone got similar problem to mine? Any suggestions?

How to get the coarse location using Wifi or GSM or GPS, whichever is available?

My app only needs a coarse location service when started up.
In detail, I need the app's rough location so as to provide the users with the shop info nearby.
The location does NOT need to be updated constantly. In addition, coarse localization will be sufficient in this case.
I wish the app to choose GSM, or wifi, or GPS automatically, whichever is available.
The location service should also be one-time to save phone energy.
How may I do that?
I have tried using GPS separately.
My problem is I don't know how to stop the constantly-refreshing-location feature of GPS. I don't know how to make the phone select one out of the three methods, either.
Some sample codes or ideas are greatly appreciated.
Here's a certain point of view:
private void _getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
try {
lat = location.getLatitude();
lon = location.getLongitude();
} catch (NullPointerException e) {
lat = -1.0;
lon = -1.0;
}
}
This might however request a FINE_LOCATION access. So:
Another way is to use this which uses the LocationManager.
The quickest possible way is to use the Last Known location with this, I used it and it's quite fast:
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
you can use LocationClient it provides a unified/simplified (Fused) location API, check out this Video for introductory material and background or this developer guide
the main drawback is that you make you app dependent on the existence of Google Play Services on the device.
In order to get info from specific provider you should use: LocationManager.getLastKnownLocation(String provider), if you want your app to choose automatically between providers then you can add choosing of the provider with getBestProvider method. As for stopping refreshing of GPS location, I didn't quite catch. Do you need to get the location info only once or do you need to monitor location changes periodically?
EDIT: Oh by the way, if you want your location info to be up-to-date you should use requestSingleUpdate method of location manager, with specified Criteria. In this case provider should also be chosen automatically

Unable to get Latitude/Longitude to change on Android app

Seem to be having some issues getting the longitude and latitude to change on my gps app I am making. I am new to Android, and am not 100% sure I did it right. I also want to note that I do NOT want to use listners for only when the location changes. I specifically want it to update every 5 seconds as I have it in the thread. I also don't seem to be retrieving any satellite data from the stuff I used below either. I just keep writing an empty list to my log file. Am I missing a permission or is the code just wrong? All of the needed information is provided below. Thanks.
<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.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize various GPS things
location = new Location("now");
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
gps = lm.getGpsStatus(null);
// Create Entry List for later use
entries = new ArrayList<Entry>();
rows = new ArrayList<TableRow>();
// Turn off Stop by default
Button button = (Button)findViewById(R.id.stopButton);
button.setEnabled(false);
// GPS enabled?
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
criteria = new Criteria();
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAltitudeRequired(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
backgroundThread.start();
}
Thread thread = new Thread()
{
#Override
public void run() {
while (keepGoing) {
// timestamp
long ts = System.currentTimeMillis() / 1000L;
// set location
location = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
// location stuff
float ac = location.getAccuracy();
double al = location.getAltitude();
double lat = location.getLatitude();
double lon = location.getLongitude();
DataEntry d = new DataEntry(ac, al, lat, lon);
// satellite stuff
List<GPSSatelliteEntry> GPSentries = new ArrayList<GPSSatelliteEntry>();
Iterable<GpsSatellite> sats = gps.getSatellites();
Iterator<GpsSatellite> satI = sats.iterator();
while (satI.hasNext()) {
GpsSatellite item = (GpsSatellite) satI.next();
float az = item.getAzimuth();
float el = item.getElevation();
int p = item.getPrn();
float s = item.getSnr();
GPSSatelliteEntry temp = new GPSSatelliteEntry(az, el, p, s);
GPSentries.add(temp);
}
Entry en = new Entry(ts, d, GPSentries);
entries.add(en);
try {
Thread.sleep(5000); // 5000 ms
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
writeData();
}
};
getLastKnownLocation only works if something on the device is generating the location updates. If nothing on your phone has called requestLocationUpdates, then thye will never update in getLastKnownLocation. The fix is to call requestLocationUpdates and either move your logic into those functions, or even ignore the calls to those functions but have them going off anyway. Remember to unregister when you no longer want updates, like in onStop.

android - GPS not accurate

i am trying to show on the map where i am.
I am in Israel and it shows me i am in Egypt.
I tried implementing all of the suggestions of different posts and non helped me solve my problem.
I turned on GPS.
I am connected to the Internet.
When i launch MAPS default android App it shows my real location!
MANIFEST PERMISSIONS:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
I also use the uses-library android:name="com.google.android.maps".
This is the relevant code from the MapActivity which extends LocationListener:
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = lm
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
if(came_from.matches("FollowLocation"))
{
Location location;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String providerName = lm.getBestProvider(criteria, true);
// If no suitable provider is found, null is returned.
if (providerName == null)
{
// reflecting changes if distance travel by
// user is greater than 20m from current location
// and every 1 minute
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
else
{
// reflecting changes if distance travel by
// user is greater than 20m from current location
// and every 1 minute
lm.requestLocationUpdates(providerName, 1*60*1000, 20, this);
location=lm.getLastKnownLocation(providerName);
}
gMapView = (MapView) findViewById(R.id.myGMap);
gMapView.setStreetView(true);
mc = gMapView.getController();
if (location != null)
{
lat = location.getLatitude();
lng = location.getLongitude();
mc.setZoom(14);
}
else //in case we didn't get the location yet
{
lat = 32.08;
lng = 35.84;
mc.setZoom(9);
}
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
// Adding zoom controls to Map
gMapView.setBuiltInZoomControls(true);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
}
}
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
return false;
}
These coordinates: lat = 32.08 lng = 35.84 are some spot in israel...
So..... What am i doing wrong ? Or what am i doing not right ? :P
Thanks!
I'd say that getLastKnownLocation is giving you that erroneous position for some reason. Your default of 32.08, 35.84 won't ever get reached because location will never be null. I'd suggest using the debugger to see which of your calls to getLastKnownLocation is getting hit, and what the value returned is.
I took my code and compared it to a working code from an example in the internet.
Not sure what solved the problem, but i definetly know that you should requestLocationUpdates after you finish to configure everything else.
Other people who encounter this problem - try taking a working code from the internet and comparing it step by step until it works on your app.

Categories

Resources