Getting null from 'getLastKnownLocation' on SDK

前端 未结 6 1359
星月不相逢
星月不相逢 2020-12-01 19:52

I have a problem related to the Location API.

I tried the following code:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SER         


        
6条回答
  •  日久生厌
    2020-12-01 20:47

    Along with the permissions in your AndroidManifest.xml file, have you registered a location listener?

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
    lm.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener); 
    

    Then have a method, in this case locationListener, to complete your task

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

提交回复
热议问题