7

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?

NiVeR
  • 9,644
  • 4
  • 30
  • 35

4 Answers4

14

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);
Jonathan
  • 188
  • 6
4

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

Dirk Jäckel
  • 2,979
  • 3
  • 29
  • 47
  • No.What I am doing is, for given location a use the Google Places API to receive nearby places and I need to calculate the distance between the places and the current location.I saw the distanceto() method but it accepts Location object.So I need to create Location from lon and lat returned in the JSON object from google. – NiVeR May 20 '12 at 21:11
1
Location l = new Location("any string"); 
l.setLatitude(lat);
l.setLongitude(lon);
Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
0

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