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();
}
Related
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
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.
I have this View Adapter method for a ListView which is populated from a JSON file.
One of the components I wanna display on a TextView of each list item is the distance between the user's location and each location which is displayed on the ListView.
But the app crashed everytime I run it, I know there's something wrong with my code starting from //calculate the distance.
Can anybody help me to analyze and fix on the lines where I went wrong probably?
Here's the sample of the parsed longitude and latitude from the JSON:
"latitude": 52.5074779785632,
"longitude": 13.3903813322449,
and here's the method I'm using:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LocationsListActivity.this.getLayoutInflater().inflate(R.layout.listitems, null, true);
}
VideoLocation vidLocation = videoLocations[position];
ImageView v = (ImageView)convertView.findViewById(R.id.image);
String url = vidLocation.documentary_thumbnail_url;
v.setTag(url);
loader.DisplayImage(url, ctx, v);
TextView titleView = (TextView)convertView.findViewById(R.id.txt_title);
String title = vidLocation.name;
titleView.setText(title.toUpperCase());
TextView descView = (TextView)convertView.findViewById(R.id.txt_list_desc);
String desc = vidLocation.text;
descView.setText(desc);
//calculate the distance
Location newLocation = new Location("User");
GeoPoint geopoint = new GeoPoint(
(int) (newLocation.getLatitude() * 1E6), (int) (newLocation
.getLongitude() * 1E6));
GeoPoint myposition = geopoint;
Location locationA = new Location("point A");
Location locationB = new Location("point B");
locationA.setLatitude(geopoint.getLatitudeE6() / 1E6);
locationA.setLongitude(geopoint.getLongitudeE6() / 1E6);
locationB.setLatitude(vidLocation.latitude/ 1E6);
locationB.setLongitude(vidLocation.longitude / 1E6);
double distance = locationA.distanceTo(locationB);
String distances = String.valueOf(distance);
TextView distanceView = (TextView)convertView.findViewById(R.id.txt_distance);
System.out.println(distances);
distanceView.setText(distances+" m");
return convertView;
}
When your application crashes and you ask about it on StackOverflow, you should always include the logcat output of the crash details. The answer is almost always right there and very easy to pick out.
What you're doing here is using the Location object incorrectly. If you look at the docs for the constructor you're using, you'll see that this just returns a default location object and doesn't contain the devices actual location. Also, the string that you pass into this is not arbitrary, it's the name of the location provider to be associated with this Location object.
To get the user's last known location, get an instance of the LocationManager and call getLastKnownLocation. This also takes a string which corresponds to a location provider that should be used.
Read the docs on obtaining user location and look into the Criteria object and the LocationManager.getBestProvider method. These are the safest ways to get the best location and not crash in the process. For example, if you request location passing the GPS provider string and the device doesn't have a GPS or the user has their GPS turned off, your code will crash (I believe you get a null object from getLastKnownLocation in this case). Also make sure to add the appropriate permissions to the manifest.
http://developer.android.com/guide/topics/location/obtaining-user-location.html
http://developer.android.com/reference/android/location/Criteria.html
http://developer.android.com/reference/android/location/LocationManager.html
Location newLocation = new Location("User");
"User" is not a valid LocationProvider. It should at least be one of
LocationManager.getAllProviders();
(Usually "gps" or "network")
Also, in your code, newLocation is not really initialized. Its values are most likely empty. You should obtain the user location with something like, for instance:
LocationManager.getLastKnownLocation(null);
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());
}
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;
}