Henry
Henry

Reputation: 1042

Getting user location and storing it in a variable Android

is it possible to get user location and store it in a variable so it can be use for other purposes, such as giving directions to user from they location to a XYZ, or change the marker style for the user location.

Currently, I have this;

map.setMyLocationEnabled(true); 

I have been searching for hours but I can't seem to find anything of use that explains how I can go about storing user current location. I am working with fragment and not activity class.

Sorry if this question sounds stupid.

Upvotes: 2

Views: 4529

Answers (2)

Vasilov Artur
Vasilov Artur

Reputation: 1487

If you use GoogleMap, location changes can be easily handled via OnMyLocationChangeListener in GoogleMap. For example:

map.setOnMyLocationChangeListener(this);

//this - is some class which implements OnMyLocationChangeListener interface.

//method for handling this event.    
@Override
public void onMyLocationChange(Location location) {
    Logger.log(TAG, "My location changed!");
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    //TODO
}

So, now you can handle all changes in location of the user.

We also have a good class for storing single points - LatLng. If you need to store the set of points, just create your own class, for example:

public class MyLocations {
    private final List<LatLng> locations = new ArrayList<LatLng>();

    public void addLocation(LatLng latLng) {
        locations.add(latLng);
    }
    //other methods
}

Create instance of this class and rewrite onMyLocationChange method:

MyLocations locations = new MyLocations();
//...
@Override
public void onMyLocationChange(Location location) {
    Logger.log(TAG, "My location changed!");
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    LatLng latLng = new LatLng(lat, lng);
    locations.add(latLng);
    //other code, if needed
}

This listener of location changes is calling rather often, it may has a bad effect for battery, so, if you want to disable it, just set it to null in map:

map.setOnMyLocationChangeListener(null);

Upvotes: 1

bwegs
bwegs

Reputation: 3767

One simple way to do what you're asking is to retrieve the user's LastKnownLocation (as suggested in the comment above) and then create a Location class of your own so you can then create a variable where you store all the information you deem relevant to a user's location (i.e. - latitude, longitude, address, etc.).

Example Location class might look something like this...

public class UserLocation {
    private String name; // name of the location
    private double latitude; // latitude (-90 to +90)
    private double longitude; // longitude (-180 to +180)
    private String address; // address of the location

    ... // constructor(s), getters, setters, other methods
}

Storing the user's current location in a Location variable might look like this...

...
// get last known location
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));

// create variable to store the user's location
UserLocation currentLocation = new UserLocation();

// set values of our location variable
currentLocation.setLatitude(location.getLatitude());
currentLocation.setLongitude(location.getLongitude());
...

Upvotes: 1

Related Questions