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?


@Shylendra Madda 2019-02-11 11:01:24

In Kotlin using LocationManager class you can pass the required location provider like:

val location = Location(LocationManager.NETWORK_PROVIDER) // OR GPS_PROVIDER based on the requirement
location.latitude = 42.125
location.longitude = 55.123

@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

21 Answered Questions

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

13 Answered Questions

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

23 Answered Questions

[SOLVED] Deleting shared preferences

36 Answered Questions

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

26 Answered Questions

14 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