Rana Talha Tariq Rana Talha Tariq - 1 month ago 13
Android Question

Basic Google Maps Android, 2 small errors with PERMISSIONS

Respected community, I'm learning Google Maps integration in ANDROID and have understood a lot of things, with permissions, as well as using the latest GoogleApiAvailability but still facing 2 errors which I am not able to resolve. I'm getting

Cannot Resolve Method isUserRecoverableError(int)
AND
non-static method getErrorDialog cannot be referenced from a static context in the end of code
at
public boolean ServicesOK()
method.
Following is the Java Code: (If anything else is needed please let me know so that I can be GUIDED properly with Android Google Maps concepts :) )

package com.example.testmap;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private static final int ERROR_DIALOG_REQUEST = 9901;

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

if(ServicesOK()) {
Toast.makeText(this, "Can't connect to mapping services", Toast.LENGTH_SHORT).show();}

// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMap mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

public boolean ServicesOK(){
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int isAvailable = googleApiAvailability.isGooglePlayServicesAvailable(this);

if(isAvailable== ConnectionResult.SUCCESS){
return true;
}
else if(GoogleApiAvailability.isUserRecoverableError(isAvailable)){
Dialog dialog;
dialog= GoogleApiAvailability.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
}
else{
Toast.makeText(this, "Can't connect to mapping services", Toast.LENGTH_SHORT).show();
}
return false;
}
}

Answer

isUserRecoverableError method come from GooglePlayServicesUtil not from GoogleApiAvailability. But as you can see in the doc, this method is deprecated and you should now use GoogleApiAvailability.getInstance().isUserResolvableError() instead

and for your second error you're trying to access to getErrorDialog but it's not a static method. you should do GoogleApiAvailability.getInstance().getErrorDialog instead.