в эти дни для моего единственного финального проекта я хочу реализовать в одном упражнении (нижняя строка меню) Google Maps, который показывает близлежащие больницы (или что-то еще). Я следую учебному пособию, и у меня есть этот код:
public class Harta extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
double currentLatitude, currentLongitude;
Location myLocation;
private final static int REQUEST_CHECK_SETTINGS_GPS = 0x1;
private final static int REQUEST_ID_MULTIPLE_PERMISSIONS = 0x2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_harta);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.harta);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
overridePendingTransition(0, 0);
return true;
case R.id.video:
startActivity(new Intent(getApplicationContext(), About.class));
overridePendingTransition(0, 0);
return true;
case R.id.masina:
startActivity(new Intent(getApplicationContext(), DashBoard.class));
overridePendingTransition(0, 0);
return true;
case R.id.harta:
return true;
}
return false;
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
setUPGClient( );
}
private void setUPGClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
checkPermission();
}
private void checkPermission() {
int permissionLocation = ContextCompat.checkSelfPermission(Harta.this,
Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermission = new ArrayList<>();
if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
listPermission.add(Manifest.permission.ACCESS_FINE_LOCATION);
if(!listPermission.isEmpty()){
ActivityCompat.requestPermissions(this,listPermission.toArray(new String[listPermission.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
}
} else {
getMyLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
int permissionLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
getMyLocation();
} else {
checkPermission();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
private void getNearbyHospitals() {
StringBuilder stringBuilder =
new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
stringBuilder.append("location="+String.valueOf(currentLatitude)+ ","+String.valueOf(currentLongitude));
stringBuilder.append("&radius=1000");
stringBuilder.append("&type=hospital");
stringBuilder.append("&key="+getResources().getString(R.string.google_maps_key));
String url = stringBuilder.toString();
Object dataTransfer[] = new Object[2];
dataTransfer[0] = mMap;
dataTransfer[1] = url;
GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
getNearbyPlacesData.execute(dataTransfer);
}
private void getMyLocation(){
if(mGoogleApiClient!=null) {
if (mGoogleApiClient.isConnected()) {
int permissionLocation = ContextCompat.checkSelfPermission(Harta.this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(3000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, locationRequest, (com.google.android.gms.location.LocationListener) this);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi
.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied.
// You can initialize location requests here.
int permissionLocation = ContextCompat
.checkSelfPermission(Harta.this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
myLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied.
// But could be fixed by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
// Ask to turn on GPS automatically
status.startResolutionForResult(Harta.this,
REQUEST_CHECK_SETTINGS_GPS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied.
// However, we have no way
// to fix the
// settings so we won't show the dialog.
// finish();
break;
}
}
});
}
}
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Проблема заключается в следующем: я получаю ошибку в функции checkPermission в этом if (
:
if(!listPermission.isEmpty()){
ActivityCompat.requestPermissions(this,listPermission.toArray(new String[listPermission.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
} )
Ошибка: нет подходящего метода найдено для метода toArray (String [], int). Collection использует или отменяет устаревший API.