By Skizit

2010-06-06 18:16:36 8 Comments

I'm looking to test some code I've written and to do so I need to construct a variable of type Location and to give it a long / lat value but I'm unsure how I would do so. Any ideas?


@Saqib Omer 2018-08-10 12:10:38

You can use LatLng

LatLng myLoc = new LatLng(18.015365, -77.499382);

@Kuvalya 2016-10-16 21:01:11

You can write a method:

Location createNewLocation(double longitude, double latitude) {
    Location location = new Location("dummyprovider");
    return location;

And then call it:

Location myLoc = createNewLocation(dLong, dLati);

Or you can use string with Double.parse():

Location myLoc = createNewLocation(Double.parse("s.Long"), Double.parse("s.Lati"));

@Atta Ullah 2015-12-03 09:52:21

Location object = new Location("service Provider");

it will create an object of Type Location that contains the initial Latitude and Longitude at location '0' to get the initial values use

double lat = object.getLatitude();
double lng = object.getLongitude();

@Matti Virkkunen 2010-06-06 18:25:54

The API documentation is quite clear on this. First create a new Location instance:

Location loc = new Location("dummyprovider");

And then use the setter methods to set the location parameters you need, e.g.:


@Adam Johns 2015-06-17 15:00:21

what is a provider?

@Gennady Kozlov 2016-11-30 20:53:24

Location loc = new Location("");

@Matti Virkkunen 2016-12-21 11:57:09

@AdamJohns: The provider is supposed to specify what exactly acquired the specific coordinate fix. In the Location object it's just a string field and can be anything. When dealing with actual location fixes it'll often be equal to one of the constants in LocationManager such as GPS_PROVIDER which equals "gps", which would mean that the specific location was acquired via GPS.

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Deleting shared preferences

10 Answered Questions

[SOLVED] View's getWidth() and getHeight() returns 0

25 Answered Questions

21 Answered Questions

[SOLVED] How do I get the current GPS location programmatically in Android?

35 Answered Questions

[SOLVED] How to make an Android Spinner with initial text "Select One"

13 Answered Questions

[SOLVED] Dilemma: when to use Fragments vs Activities:

18 Answered Questions

[SOLVED] Fling gesture detection on grid layout

31 Answered Questions

[SOLVED] How to emulate GPS location in the Android Emulator?

17 Answered Questions

[SOLVED] How to declare global variables in Android?

Sponsored Content