NiVeR
NiVeR

Reputation: 9806

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?

Upvotes: 6

Views: 7938

Answers (4)

user1944690
user1944690

Reputation: 9

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();    
}

Upvotes: 0

Dirk Jäckel
Dirk Jäckel

Reputation: 3025

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

Upvotes: 4

Matthias Robbers
Matthias Robbers

Reputation: 15728

Location l = new Location("any string"); 
l.setLatitude(lat);
l.setLongitude(lon);

Upvotes: 1

Jonathan
Jonathan

Reputation: 178

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);

Upvotes: 13

Related Questions