Je ne parviens pas à obtenir l'emplacement actuel dès que j'ouvre mon activité Maps. Bien qu'il affiche l'emplacement actuel avec la marque de point bleu.Impossible d'obtenir l'emplacement actuel dès que j'ouvre mon activité Maps
Je vois un message d'exception: java.lang.NullPointerException: tentative d'invoquer la méthode virtuelle « double android.location.Location.getLatitude() » sur une référence d'objet nul
Il faut beaucoup de temps iront chercher coordonnées. Pour cela, je dois déplacer mon téléphone et attendre.
Voici mon code pour elle:
if (Build.VERSION.SDK_INT < 25) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// Ask for permission
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
lastKnownLocation =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
pDialog.hide();
locate = new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude());
mMap.clear();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locate, 13));
originMarkers.add(mMap.addMarker(new MarkerOptions()
.title("Your Location")
.position(locate)));
origin_lati = "" + lastKnownLocation.getLatitude();
origin_longi = "" + lastKnownLocation.getLongitude();
Log.e("Lati Longi", origin_lati+", "+origin_longi);
}catch (Exception e){
Log.e("Exception", e.toString());
}
}
}
pourquoi n'utilisez-vous pas le nouveau fusedLocationApi pour les mises à jour de localisation ?? – sumit
Je suis assez nouveau à ce concept. Comment cela marche-t-il? –
Dois-je changer le code complet? –