如何在内部函数中使用 class 变量?
How to use class variable inside inner function?
我想在内部函数中使用 class 成员
我尝试将变量设置为全局变量,但出现错误 'class or interface expected'
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in current location,
// and move the map's camera to the same location.
//Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);
}else {
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
wayLongitude = location.getLongitude();
wayLatitude = location.getLatitude();
/*LatLng sydney = new LatLng(wayLatitude, wayLongitude);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
}
});
}
我希望能够在 "OnSuccess" 函数中执行 "googleMap.addMarker"。
我该怎么做?
您需要将 googleMap 参数声明为最终参数:
public void onMapReady(final GoogleMap googleMap) {
//...
}
我想在内部函数中使用 class 成员
我尝试将变量设置为全局变量,但出现错误 'class or interface expected'
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in current location,
// and move the map's camera to the same location.
//Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);
}else {
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
wayLongitude = location.getLongitude();
wayLatitude = location.getLatitude();
/*LatLng sydney = new LatLng(wayLatitude, wayLongitude);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
}
});
}
我希望能够在 "OnSuccess" 函数中执行 "googleMap.addMarker"。 我该怎么做?
您需要将 googleMap 参数声明为最终参数:
public void onMapReady(final GoogleMap googleMap) {
//...
}