【问题标题】:Cannot get Latitude and Longitude to be stored in a variable无法将纬度和经度存储在变量中
【发布时间】:2017-01-31 15:13:42
【问题描述】:

我想再次使用 lat、lon 变量,但是当我在 onConnected() 中设置它时,它并没有在 MainActivity 中设置。 这是我的代码,

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private static final int PERMISSION_ACCESS_FINE_LOCATION = 1;
private GoogleApiClient googleApiClient;
protected Context context;
double lat, lon;
TextView txtLat;
double latitude = 0, longitude = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSION_ACCESS_FINE_LOCATION);
    }

    googleApiClient = new GoogleApiClient.Builder(this, this, this).addApi(LocationServices.API).build();


}

这是另一部分,包括 get lat long。

@Override
public void onConnected(Bundle bundle) {
    Log.i(MainActivity.class.getSimpleName(), "Connected to Google Play Services!");

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

        lat = lastLocation.getLatitude();
        lon = lastLocation.getLongitude();
        txtLat = (TextView) findViewById(R.id.textView1);
        txtLat.setText(lat + " " + lon);
    }

}

【问题讨论】:

  • 什么double latitude = 0, longitude = 0;这是你想再次重复使用的吗?
  • @ntaloventi 没有。纬度,经度

标签: android google-maps location


【解决方案1】:

允许位置权限和启用位置不是一回事(即:用户可以允许位置权限但位置未启用,因此 LastLocation 可能为空) 例如,您可以:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;

gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (gps_enabled || network_enabled) {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1001);
    mLocationRequest.setNumUpdates(1);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationServices.FusedLocationApi.requestLocationUpdates(GoogleClient, mLocationRequest, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
                   LocationServices.FusedLocationApi.removeLocationUpdates(GoogleClient, this);   
                   lat = location.getLatitude();
                   lon = location.getLongitude();   
                }
            });
        }

【讨论】:

    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2014-01-15
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多