0
I am making several attempts, but I cannot get the current location of a user using the Location API.
I am running the following example:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
Location localizacao;
GoogleApiClient mapGoogleApiClient;
EditText edtLat;
EditText edtLog;
Button btLocalizacao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtLat = (EditText) findViewById(R.id.edtLatitude);
edtLog = (EditText) findViewById(R.id.edtLongitude);
btLocalizacao = (Button) findViewById(R.id.btLocalizacao);
if (mapGoogleApiClient != null) {
mapGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
btLocalizacao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GPS();
}
});
}
@TargetApi(Build.VERSION_CODES.M)
public void GPS() {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
localizacao = LocationServices.FusedLocationApi.getLastLocation(mapGoogleApiClient);
if (localizacao != null) {
edtLat.setText(String.valueOf(localizacao.getLatitude()));
edtLog.setText(String.valueOf(localizacao.getLongitude()));
} else {
AlertDialog erroLocation = new AlertDialog.Builder(this).create();
erroLocation.setIcon(R.drawable.alert);
erroLocation.setTitle("Localização não encontrada");
erroLocation.setMessage("Sua Localização não foi encontrada!! Tente novamente!");
erroLocation.show();
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}}
However it returns the following error:
java.lang.NoSuchMethodError: com.estudosmobileoreia.mapsdemo.MainActivity.checkSelfPermission
Knowing that Androidmanifest.xml has the following permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
</activity>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I wonder if someone can assist me with this error or demonstrate another way to get the user’s location ?
..My XML is correct, so much so that in it there is only one Button to get the location and 4 Textview to return me the location in two of them..
@ramaral what I would need to remove from the method ? Can you comment on an example, please ?
– Gabriel Henrique