0
我想要再次使用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();
}
和这里的,其中包括了获得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);
}
}
什么'双纬度= 0,经度= 0;'这是你想再次使用这个? –
@ntaloventi no。 lat,lon –