Android GPS position give just first number - android

Here is my app:
public class HlavnaAktivita extends MapActivity implements LocationListener {
...
public void onLocationChanged(Location location) {
Log.w("myApp", String.valueOf(location.getLatitude()));
}
When I give to emulator for example 48.590007 and 17.824579, it just write "17.0". I tried http://www.androidcompetencycenter.com/?s=GPS this tutorial (from StackOverflow) and when I run that app, it write good postition, but on map is just rounded position (17, 48) (position according to maps.google.com is good, but on map is cca 100km wrong).
What could be the problem? I tried a lot of tutorials but it's still not working.

To get the exact position on the map you need 2 parameters:
Latitude and Longitude of a Point.
GeoPoint accepts latitude and longitude in microdegrees so try to create your point like this to get better accuracy:
double lati = location.getLatitude();
double longi = location.getLongitude();
GeoPoint geo_point= new GeoPoint((int)(lati * 1e6),(int)(longi * 1e6));

It converts somewhere your number to int. To avoid it try someting like:
Log.w("myApp", String.valueOf((float) location.getLatitude()));

This was just my mistake - I forget to add (). Sorry.

Related

I need to sort the latitude and longitude. which is nearer to my current location

This is how i calculate the Distance between my list.
my code is below
public void calc(){
for(int i = 0; i < mSearchResultModelsToGetAppointments.size(); i++){
Location newLocation = new Location("");
newLocation.setLatitude(gpsTracker.getLatitude());
newLocation.setLongitude(gpsTracker.getLongitude());
if(String.valueOf(gpsTracker.getLatitude()).equalsIgnoreCase("0.0") || String.valueOf(gpsTracker.getLongitude()).equalsIgnoreCase("0.0")) {
} else{
Location oldLocation = new Location("");
oldLocation.setLatitude(mSearchResultModelUnderProcess.getLatitude());
oldLocation.setLongitude(mSearchResultModelUnderProcess.getLongitude());
float distanceInMeters = newLocation.distanceTo(oldLocation)/1000;
if(distanceInMeters < 2820){
getAdapter().notifyDataSetChanged();
getAdapter().removeItem(i);
getAdapter().notifyDataSetChanged();
}
}
}
}
This is how i display the location now.
If you do not want use any external library, I would suggest following way.
Create your own custom Location Class with attributes lat and lng, and additionally attribute called distance which measures from your current location till the point. here you can find how to find distance between 2 points
This class should implement Comparable interface and override its compare function. Inside this function, you can create your own sorting logic based on distance
here you can find how to use comparable interface, Let me know if you have still problem
Happy coding

Android: How can I get the traveled distance while I'm running?

I'm wrinting a application and I have to show the distance covered while I'm running.
I use the function "public void onLocationChanged" of the LocationListener. When the user tap a botton and start running I want to show the distance he covered updated to the point in which he is located.
I've written this code:
public void onLocationChanged(Location location) {
if(location != null) {
if(location.hasSpeed()){
if(latitude1 == 0 && longitude1 == 0){
latitude1 = (location.getLatitude()*Math.PI)/180;
longitude1 = (location.getLongitude()*Math.PI)/180;
} else{
latitude2 = (location.getLatitude()*Math.PI)/180;
longitude2 = (location.getLongitude()*Math.PI)/180;
distance = (6372.795477598)*Math.acos(Math.sin(latitude1)
*Math.sin(latitude2)+Math.cos(latitude1)
*Math.cos(latitude2)*Math.cos(longitude1-longitude2));
sumDistance += distance;
latitude1 = latitude2;
longitude1 = longitude2;
}
tv.setText("Distance covered=" + sumDistance + " m");
}
}
}
Is it accurated?
Just a Suggestion:
Store the Latitude and Longitude of the start location and the end location when the user clicks the appropriate button.
and then you could use distanceBetween or distanceTo to get the distance between those two geoPoints.
P.S: This may not work if the user will start and end his run at the same point ;)
Addition:
Check this tutorial:
Recently google has improved its location based API's. They have fused the sensors with the location based api's to share examples of how location can be made more accurate (about 4 times more effective) and consume much lesser power (10 times lesser).
Some interesting links for you to go through will be this and video google io.
SO link for how to use the API's here

Getting Location from coordinates

I have given coordinates for latitude and longitude, and I want make a location object with those.There isn't a constructor that takes to doubles to make a location, so I tried like this:
Location l = null;
l.setLatitude(lat);
l.setLongitude(lon);
but it crashes. Any different idea?
Just create a new Location with new Location("reverseGeocoded");
like this
GeoPoint newCurrent = new GeoPoint(59529200, 18071400);
Location current = new Location("reverseGeocoded");
current.setLatitude(newCurrent.getLatitudeE6() / 1e6);
current.setLongitude(newCurrent.getLongitudeE6() / 1e6);
current.setAccuracy(3333);
current.setBearing(333);
This crashes because you cannot call methods on an non existent object.
I presume you are talking about android.location.Location?
This is usually returned by the various positioning services of Android.
What do you want to do with it?
Do you want to reverse geocode it? As in find an address for that geo coordinate?
Or do you want to use it as a "fake" position and feed it to other applications?
There are BTW two constructors. One takes the name of a positioning service and the other is a copy constructor and takes an existing Location.
So you could create a Location like this:
Location l = new Location("network");
But I do not think that this will result in something you want to have.
Here is a link to the documentation:
https://developer.android.com/reference/android/location/Location.html#Location%28java.lang.String%29
Location l = new Location("any string");
l.setLatitude(lat);
l.setLongitude(lon);
Try this:
public double lat;
public double lon;
public void onLocationChanged(Location loc)
{
lat=loc.getLatitude();
lon=loc.getLongitude();
Toast.makeText(getBaseContext(),"Latitude:" + lat +Longitude"+lon,Toast.LENGTH_LONG).show();
}

Android Google Maps - getLastKnownLocation returns inaccurate latitude and longitude

I am developing an app where I need to calculate the distance from the current position and some other locations. I am using the GPS to access the users current location and the other locations coordinates are stored in a database. The problem occurs in the following snippet:
#Override
public void onLocationChanged(Location arg0) {
Log.v("LOCATION LAT", String.valueOf(arg0.getLatitude()));
currentLocation = arg0; //currentLocation is a global class variable
}
The problem is when I feed the DDMS with coordinates such as:
Latitude: 62.639579
Longitude: 17.909689 and log these values I get Latitude: 62.0 and Longitude 17.0 .
If I create a location object and set the lat and lng values myself it works. Like this:
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current.setLatitude(62.639579);
current.setLongitude(17.909689);
Log.v("Current LAT", "" + current.getLatitude());
}
EDIT SOLVED:
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently this should be delimited with a comma sign, not a dot...
Have you used the permissions specified in this post? Else it kicks back to using cell tower triangulation.
Other question
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently the coordinates should be delimited with a comma sign, not a dot...
you can do something like as below. Create a location variable in that you have to assign location change var
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current=arg0;
Log.v("Current LAT", "" + current.getLatitude());
}

Geo Point vs Location

This is maybe a noob question but im not 100% sure about it.
How can i make a Location Object using Geo points? I want to use it to get the distance between two points.
I already found a thread where it says
Location loc1 = new Location("Location");
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);
Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
and then i would use the distanceTo() to get the distance between the two points.
My Questions
What is the Providername for? ...new Location("What is this here???")
So do i have to define a Provider before or something?
I want to use this code in a for() to calaculate between more GeoPoints.
And btw - i have to convert the E6 Values back to normal?
Not exactly
loc.setLatitude() takes a double latitude. So the correct code is:
loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);
Location() constructor take the name of the GPS provider used. It can be LocationManager.GPS_PROVIDER or NETWORK_PROVIDER among other values
To get the distance between two point you can use the Location class and more precisely the distanceBetween static method.
The doc is quite clear on what it does but here a quick code sample:
float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
To convert from minute/second to degree you can use the convert method of the Location class.
Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
then
public Location GeoToLocation(GeoPoint gp) {
Location location = new Location("dummyProvider");
location.setLatitude(gp.getLatitudeE6()/1E6);
location.setLongitude(gp.getLongitudeE6()/1E6);
return location;
}

Categories

Resources