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
Related
Here I am using this code in order to get the latitude and longitude of the location but all that I am getting is latitude 0.0 and longitude 0.0 the code to find the latitude and logitude that I am using is given as below , Also I am testing this code in Genymotion emulator and all that is printed in the log is 0.0 0.0 because the if(address.size()>0) is not getting satisfyied can anyone tell where am I going wrong or is it because of emulator.
Geocoder geo = new Geocoder(getActivity());
try {
List<Address> address = geo.getFromLocationName(str_ELocation,1);
if (address.size() > 0) {
Log.d("List Size : ", "" + address.size());
latitude = address.get(0).getLatitude();
longitude = address.get(0).getLongitude();
} else {
Log.d("In Else", "In Else");
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
Log.d("LONGITUDE AND LATITUDE", "" + longitude + "" + latitude);
Also is there any other simpler solution to just find latitude and longitude. I am using this in Fragment and so I am using getActivity() here instead of myClassName.this.
I'm guessing you don't have a backend service you are using to obtain this information.
Note The Docs say
The Geocoder class requires a backend service that is not included in
the core android framework. The Geocoder query methods will return an
empty list if there no backend service in the platform. Use the
isPresent() method to determine whether a Geocoder implementation
exists.
This should also be done on a background Thread if not already being done.
Alternative
You may be better off using LocationManager
Be sure to read those docs on it before beginning to have an understanding of how it works and should be implemented (That portion is short).
Here is an example that can get you started
Anything like this should be done on a background Thread and not on the UI Thread. And there are listeners which can return the values you need.
full disclosure: I haven't used GPS but I read through the docs ;)
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
I test my apps on two phones (1) an LG Optima running 2.3.7 and (2) a galaxy S3 running 4.1.2. The following code always works on the LG and always fails with a nullPointerException on the Galaxy..
try { //sometimes getting the last known location gets a nullpointerexception
String locationProvider = LocationManager.GPS_PROVIDER;
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location myLoc = locMgr.getLastKnownLocation(locationProvider);
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(myLoc.getLatitude(), myLoc.getLongitude()));
map.animateCamera(center);
DebugLog.debugLog("Instantiated new Location listener. Moved camera to: " + myLoc.getLatitude() + myLoc.getLongitude() , false);
} catch (Exception e) {
DebugLog.debugLog("Exception getting lastknownlocation - doing without" + e, false);
}
Is there something wrong with this code?
Thanks,
Gary
Is there something wrong with this code?
You are not checking to see if myLoc is null. getLastKnownLocation() does not have to return a non-null value.
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?
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.