i am getting data from gps provider using mylocation class. code is this:
MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
#Override
public void gotLocation(Location location) {
//Got the location!
// for phone
//currentLocation = new GeoPoint((int) (location.getLatitude() * 1000000),
// (int) (location.getLongitude() * 1000000));
// for emulator
currentLocation = new GeoPoint((int) (location.getLatitude()),
(int) (location.getLongitude()));
doSomething();
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
when i use the app in emulator(2.3.3) it shows the correct location without multiplying anything.
but when i use it in a device(4.0) lat and lon need to multiplied with 1000000. i couldn't find why. i don't think its because of the version of android. anyone have any idea?
Because the MapView uses microdegress for its units so you need to multiply by 1e6. Otherwise you show up off the coast of Africa - basically lat long of approximately 0,0
From the documentation on GeoPoint:
An immutable class representing a pair of latitude and longitude, stored as integer numbers of microdegrees.
Don't know why the emulator is working - it shouldn't.
Related
look at this:
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
GeoPoint myGeoPoint = myLocationOverlay.getMyLocation();
That works fine. But i need to save the coordinates in a variable. So i tried this:
myLocationLon = (double) myGeoPoint.getLongitudeE6();
When i run the App, this last line makes it collapse. Can you please tell me why this doesn't work ? Thank you
GeoPoint.getLongitudeE6() and GeoPoint.getLatitudeE6() both return microdegrees (basically degrees * 1E6).
so you need to convert microdegrees to degrees simply write function:
public double microDegreesToDegrees(int microDegrees) {
return microDegrees / 1E6;
}
and then
myLocationLon = microDegreesToDegrees(myGeoPoint.getLongitudeE6());
Is there a way to get the accuracy of the fix from MyLocationOverlay? It obviously knows how accurate it is since it draws the circle of the area of how accurate it is. Is there away to get this value?
Thanks
Code from my location listener in one of my apps...
public void onLocationChanged(Location location) {
currentLatitudeE6 = (int) (location.getLatitude() * 1E6);
currentLongitudeE6 = (int) (location.getLongitude() * 1E6);
currentAccuracy = location.getAccuracy();
hasCurrentPosition = true;
gpsMessage = "GPS Tracking";
updateMap();
}
Implement Location Listener by locationManager.requestLocationUpdates method, check following link:
http://hejp.co.uk/android/android-gps-example/
Im using this code for getting the location for my app:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this);
But when i tried the app in my real android phone it show this location about 80 kilometers away from the location im actualy at.. How would i make this code more accurate.. I want the result to be way more accurate for what im making..
Im using the onLocationChanged to display it at the map.. Here it is:
public void onLocationChanged(Location location) {
if (location != null) {
//Gets users longitude and latitude
lat = location.getLatitude();
lng = location.getLongitude();
//sets the GeoPoint usersLocation equal lat and lng
userLocation = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
OverlayItem usersLocationIcon = new OverlayItem(userLocation, null, null);
LocationPin myLocationPin = new LocationPin(userIcon, MainActivity.this);
//Removes the previous location
if(previousLocation != null)
mainMap.getOverlays().remove(previousLocation);
myLocationPin.getLocation(usersLocationIcon);
overlayList.add(myLocationPin);
//refresh the map
mainMap.invalidate();
//Making myLocationPin into the previousLocation just to be able to remove it later
previousLocation = myLocationPin;
}
The call requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this); is asking to be updated no more than once every 1000ms when the location from GPS changes by more than 200.0 meters from the last update. If you want finer precision, try lowering these numbers.
However, you shouldn't be off by 80km. Are you testing this outside with a clear view of the sky?
I think the issue is with rounding. You are using new GeoPoint((int) lat * 1000000, (int) lng * 1000000);, but instead do this:
new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6));
The difference is, the double values were converted to integers before the multiplication. This way the multiplication happens afterwards, and so the digits after the decimal point are maintained.
There are 2 possible answers...
You can either ask for fine permission, this uses nearby wi-fi networks along with GPS in order to get a better track on where you are:
http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION
or you might just be getting bad GPS data. Have you tried to restart the phone? Are you getting the correct location in Google Maps?
Hope this helps.
I am facing a problem with the google map search. I have mad a app through which i can see a location in google map on my emulator which is set in the code as:
int lat = (int)(22.3666667*1000000);
int lng = (int)(91.8000000*1000000);
GeoPoint pt = new GeoPoint(lat,lng);
This works fine.
Now i want to search a location dynamically i.e. i have a editbox(Location_For_Search) and a button(Find_Location_Button). So when i write some location in editbox and press the button then it will show the location in google map with a marker on location. How can i do this?
Please any one help me.
With best wishes
Md. Fazla Rabbi
Use the geocoder:
Geocoder geo = new Geocoder(this);
List<Address> addr;
try
{
addr = coder.getFromLocationName(yourEditTextaddr, 10);
Address loc = addr.get(0);
loc.getLatitude();
loc.getLongitude();
point = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
return point;
}
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableCompass();
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
if (myLocOverlay == null)
{
assignFirstLoc();
}
if (myLocOverlay != null)
{
Log.e("error","1");
p = new GeoPoint((int) (myLocOverlay.getMyLocation().getLatitudeE6()), (int) (myLocOverlay.getMyLocation().getLongitudeE6()));
mc = myMap.getController();
mc.animateTo(p);
mc.setCenter(p);
}
I saw "error 1" message in the logcat but after that I took an null pointer exception I dont know why, I believe I check myLocOverlay is null or not with "if" block.. Any suggesstions?
Edit:
This is my assignedGFirstLoc method that assigned the firtst values:
private void assignFirstLoc()
{
final String coordinates[] = {"41.00527", "28.97696"};
double lat2 = Double.parseDouble(coordinates[0]);
double lng2 = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));
myMap.getController().setZoom(16);
myMap.getController().animateTo(p);
myMap.getController().setCenter(p);
}
MyLocationOverlay can only be used to show a location coming from sensors.
You cannot set it.
You got a NullPointer because MyLocationOverlay.getMyLocation() returns null. Sensors didn't have the time to catch the geolocation yet.
If you want to show the location when the sensor got the data you can do this:
mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
mapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
}});
If you want to display an item in an arbitray location (with latitude and longitude values) you need to implement your own version of ItemizedOverlay
You can find an exemple in the SDK documentation: Google Map View tutorial, Part2 AddingOverlayItem
If you just want to show an arbitrary location on the map, you don't need an overlay. just call your function and it will work:
private void initMyLocation() {
assignFirstLoc();
}
It looks like you are not passing any coordinates into GeoPoint. Unless it is somewhere else in your code. Something like this works
String coordinates[] = {"41.146064", "-80.642861"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));