get latitude and longitude (position) google maps

2

I would like to be able to determine my current location in an android project, in order to get the latitude and longitude of my position to use them in a specific method, I have reviewed the resolved questions, but I do not understand the code they have given in response , if you could give me a hand, I would appreciate it

    
asked by zhet 14.12.2016 в 01:24
source

2 answers

0

As in one of the answers given to you in other questions, LocationManager

is used
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

This will bring you the current position, if there is no current position it will return null obviously but there is a method that does this work asynchronously

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

How to get latitude and longitude when starting the application:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
    
answered by 14.12.2016 / 01:27
source
-2

Did you see the documentation of Apache Cordova? There comes out how to do it, I achieve it but in ionic. I leave the link, greetings.

link

    
answered by 14.12.2016 в 03:27