Can't get any location - android - 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?

Related

Does LocationManager:NETWORK_PROVIDER work without GPS enabled?

I'm wondering why my app still finds location although the GPS is disabled. So I asked myselft why this is possible and I have too less knowledge about this. Maybe the NETWORK_PROVIDER needs no GPS?
I promise, GPS is really disabled.
Can anyone tell me how this is possible?
I have this in my App:
in oncreate():
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Method:
public void getGpsLocation(){
locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, myLocationListener, this.getMainLooper());
}
Listener:
LocationListener myLocationListener = new LocationListener() {
// When location has changed
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
locationAll = location;
// positionOnceFound = true --> location was already found and no further update necessary
//if (location != null && positionOnceFound == false)
if (location != null)
{
// location is found, no more update necessary --> true
positionOnceFound = true;
// get Lat/Lon of my current position
myPosLat = location.getLatitude();
myPosLon = location.getLongitude();
// For calculating the point B(right top corner) and point C(left bottom corner
// Lat/Lon of B and C needed for getting the prices from this area around my position
double dy = 5.0 / 110.54; // 5.0 -> 5km to vertical
double dx = 5.0 / (111.320 * Math.cos(myPosLat / 180 * Math.PI)); // 5km to horizontal
// Get point B
rightTopCornerLon = myPosLon + dx;
rightTopCornerLat = myPosLat + dy;
// Get point C
leftBottomCornerLon = myPosLon - dx;
leftBottomCornerLat = myPosLat - dy;
System.out.println("Alat: " + myPosLat + " Alon: " + myPosLon + " Blat: " + rightTopCornerLat + " Blon: " + rightTopCornerLon +
" Clat: " + leftBottomCornerLat + " Clon: " + leftBottomCornerLon);
getCityName(isItStartOrStop);
}
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
The network provider requires the permission of ACCESS_COARSE_LOCATION, so it will work and provide you location updates using the WIFI networks, or Mobile networks, this may not be so accurate but is faster than getting a location update using the GPS.
Yes, NETWORK_PROVIDER needs no GPS, but NETWORK_PROVIDER fetches location with low precision by wireless network site.
Yes, NETWORK_PROVIDER doesn't requires GPS.
Network provider access you location through Wireless network if GPS is not enabled but it is not accurate location. Enabling GPS will provide you with more accurate location results.
The access coarse location and access fine location permissions in you meanifeast file differentiate it. With access coarse location you will get location without GPS but fine location can access GPS location too
NETWORK_PROVIDER works with both permissions

Can't get current device location

I am trying to get current location of my android phone and display the longitude and latitude in a toast. Here is a function I wrote. While debugging the code I see that the control never goes inside onLocationChanged function.
From the following android documentation it looks like, when I call "locationMgr.requestLocationUpdates", it should call the callback function onLocationChanged. But that does not seem to happen in my code.
http://developer.android.com/training/basics/location/currentlocation.html
I checked my phone has GPS turned on. I can not figure out what is wrong in the following code. Please help.
public void getCurrentLocation(){
LocationManager locationMgr;
locationMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
// A new location update is received. Do something useful with it
String latitude = "latitude: " + location.getLatitude();
String longitude = "longitude: " + location.getLongitude();
String toastString = "location is" + latitude + "," +longitude;
Toast.makeText( getApplicationContext(),toastString,Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
// No code here
}
#Override
public void onProviderEnabled(String provider) {
// No code here
}
#Override
public void onStatusChanged(String provider, int status,Bundle extras)
{
// No code here
}
};
locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, listener);
}
I also have following two lines in my Manifest file.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Thank you for your help.
I am using Eclipse and my phone(OS: Thuderbolt) has API level 15, target 4.0.4.
There are several reasons that you are not getting location.
1.) If you are trying to get location on emulator. Then you have to manually push the coordinates using DDMS.
2.) If You are checking it on device and still you are not getting location. Then as you said that you are expecting it from GPS. Then you should have Clear sky view to get that. As GPS receiver dont work under roofs or under some hinderances. They must have sky view.
3.) You can get location from using wi-fi or cell-Tower. Also you can opt for Last known location if location accuracy is not as much important.
What i think is that may be second point will resolve your problem.
The problem was google-play-services library.
I had to download the library code and compile it in eclipse. Then I added the path to the library in my project. This solved the problem.
Earlier I had included the google-play-services .jar file in my project, but it was not working. Not sure why.
Here is example i wrote that uses LocationManager to get the location data every two minutes. Its not perfect but should be sufficient to solve your issue: It can be found here
Focus on:
#Override
public void onLocationChanged(final Location location) {
this.location=location;
final Handler handler = new Handler();
Timer ourtimer = new Timer();
TimerTask timerTask = new TimerTask() {
int cnt=1;
public void run() {
handler.post(new Runnable() {
public void run() {
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
Double altitude = location.getAltitude();
Float accuracy = location.getAccuracy();
textView.setText("Latitude: " + latitude + "\n" + "Longitude: " + longitude+ "\n" + "Altitude: " + altitude + "\n" + "Accuracy: " + accuracy + "meters"+"\n" + "Location Counter: " + cnt);
try {
jsonData = new JSONObject();
jsonData.put("Latitude", latitude);
jsonData.put("Longitude", longitude);
jsonData.put("Altitude", altitude);
jsonData.put("Accuracy", accuracy);
System.out.println(jsonData.toString()); //not required, for testing only
if(url!=null) {
new HttpPostHandler().execute();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cnt++;
}
});
}};
ourtimer.schedule(timerTask, 0, 120000);

GPS Coordinates on Test Developer Phone

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

problem in showing latitude longitude location in an android app

Following is the code which i am using to find the latitude longitude and location of a place in my app, but it always show no location found
I have added the permissions in manifest file
{
LocationManager locManager;
setContentView(R.layout.main);
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
String param = (String)locManager.getProviders(true).get(0);
Location loc = locManager.getLastKnownLocation(param);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
private void updateWithNewLocation(Location location)
{
TextView myLocationText = (TextView)findViewById(R.id.widget52);
String latLongString = " ";
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}
else
{
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
private final LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider)
{
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
}
pls help me...
I've put your code in an Android project and ran it on the emulator and it seems to be working fine.
I would change the code to first check for a lastknown location, and after that check for location updates.
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
If that location is null, or too stale (timestamp) for your needs, you can start requesting location updates. (currently, you're first requesting location updates from the GPS, and then decide to retrieve its lastknownlocation). This might cause the location manager to stop querying the GPS.
Also you need to ensure the following is in place :
For GPS Provider, make sure the following permission is put in the manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Ensure the GPS is turned on that you have sufficient GPS coverage.
Do this by checking for the GPS icon in your Notification bar
Test on a real device
Although testing GPS location listeners works partly through the emulator, the behavior of an actual device will always be different.
Debug on the emulator
Basic GPS testing can be done using the emulator. Put a breakpoint in your locationlistener, and use the DDMS perspective to send some GPS coordinates to your AVD image.
#Siva, your problem is with the UI thread, as your updates are from different thread.
To verify if this is the UI problem, put a toast or Log cat the message when you receive an update.
Once you know that UI problem, then try using Handler to postInvalidate() the UI.

Geo Fix command does not pass altitude

On Android 2.2 Emulator, the "geo fix" command seems not to be working properly. The emulator responds "OK", and onLocationChanged() is properly called in my program. However, the Location object seems not to be complete - it registers latitude and longitude just fine, but it does not contain an altitude reading: hasAltitude() returns false.
Any ideas why?
Example emulator commands:
geo fix -74 40.75 500
geo fix -77 39 400.0
Code snippet:
public void onLocationChanged(Location loc) {
System.out.println("onLocationChanged Called");
if (loc.hasAltitude()) {
double newalt = loc.getAltitude();
System.out.println("new altitude: " + newalt);
gps[ALTITUDE] = newalt;
} else {
System.out.println("No altitude fix");
}
gps[LONG] = loc.getLongitude();
System.out.println(gps[LONG]);
gps[LAT] = loc.getLatitude();
System.out.println(gps[LAT]);
}
Sample Output:
onLocationChanged Called
No altitude fix
-74.012333333333333333
40.756666666666666667
onLocationChanged Called
No altitude fix
-77.012833333333333335
39.006499999999999996
This is a bug in the emulator and has been reported at https://code.google.com/p/android/issues/detail?id=24809

Categories

Resources