Getting location from coordinates

I gave the coordinates for latitude and longitude, and I want to create a location object with them. There is no constructor that accepts doubling to make a location, so I tried like this:

Location l = null; l.setLatitude(lat); l.setLongitude(lon); 

but it will work. Any other idea?

+7
source share
4 answers

Just create a new location with a 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); 
+14
source

This causes a crash because you cannot call methods on a nonexistent object.

I assume you are talking about android.location.Location?

This is usually returned by various Android positioning services. What do you want to do with this?

Do you want to change the geocode? How to find the address for this geo-coordinate?

Or do you want to use it as a "fake" position and submit it to other applications?

There are two BTW constructors. One takes the name of the positioning service, and the other takes the copy constructor and takes the existing location.

So you can create a location like this:

 Location l = new Location("network"); 

But I do not think that this will lead to something that you want to have.

Here is the link to the documentation:

https://developer.android.com/reference/android/location/Location.html#Location%28java.lang.String%29

+4
source
 Location l = new Location("any string"); l.setLatitude(lat); l.setLongitude(lon); 
+1
source

Try the following:

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

All Articles