无法获取我的应用的上一个已知位置的当前位置

时间:2019-08-09 14:34:53

标签: java

我有一个无法接收任何位置的应用程序。我必须等待一天,才能获得当前或最近的已知位置。请帮助

此应用就像旋转木马买卖应用一样,需要日志和经纬才能获取位置

下面是我的代码。我不擅长编程。还在学习。谢谢

// MARK: - GET CURRENT LOCATION ------------------------------------------------------
protected void getCurrentLocation() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_LOW);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    assert locationManager != null;
    String provider = locationManager.getBestProvider(criteria, true);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    currentLocation = locationManager.getLastKnownLocation(provider);

    if (currentLocation != null) {
        getCityCountryNames();

        // Try to find your current Location one more time
    } else {
        locationManager.requestLocationUpdates(provider, 1000, 0, this);
    }
}


@Override
public void onLocationChanged(Location location) {
    if (PermissionsUtils.hasPermissions(this, locationPermissions)) {
        return;
    }

    locationManager.removeUpdates(this);
    currentLocation = location;

    if (currentLocation != null) {
        getCityCountryNames();
        // NO GPS location found!
    } else {
        Configs.simpleAlert(getString(R.string.get_location_failure), AdsListActivity.this);

        // Set New York City as default currentLocation
        currentLocation = new Location("provider");
        currentLocation.setLatitude(Configs.DEFAULT_LOCATION.latitude);
        currentLocation.setLongitude(Configs.DEFAULT_LOCATION.longitude);

        // Set distance and city labels
        String distFormatted = String.format("%.0f", distanceInMiles);
        distanceTxt.setText(getString(R.string.ads_list_distance_formatted, distFormatted));
        cityCountryTV.setText(getString(R.string.not_available_text_placeholder));

        // Call query
        queryAds();
    }
}

0 个答案:

没有答案
相关问题