çalışmıyor aynı yöntemi kullanır benim kodudur ? Uygulama yüklendiğinde kullanıcının geçerli konumunu almak istiyorum, aynı şeyi almak için kullanıcıyı bir düğmeye basmaya zorlamıyorum.LocationServices.FusedLocationApi.getLastLocation onCreate() İşte yöntemde
Uygulama getCurrentLocationViaPhoneLocation()
Yöntemi - (çoğunlukla Google API docs alınan)
protected String getCurrentLocationViaPhoneLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// 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 ActivityCompat#requestPermissions for more details.
return "Error - location services not available!";
}
startLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (startLocation != null) {
Log.e("We are at ", String.valueOf(startLocation.getLatitude()));
Log.e("We are at ", String.valueOf(startLocation.getLongitude()));
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addressList = geocoder.getFromLocation(startLocation.getLatitude(), startLocation.getLongitude(), 1);
if (addressList != null && addressList.size() > 0) {
currentCity = addressList.get(0).getLocality();
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
return (sb.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ("Error - current location unavailable!");
}
Düzenleme: alıyorum: startLocation==null
yöntem onCreate()
sırasında çağrıldığında anlamına gelir Error - current location unavailable!
onCreate()
sırasında.
Awesome, sorun buydu, onConnected (Bundle) yöntemi içinde getCurrentLocationViaPhoneLocation çağrısı yapmak sorunu çözdü. Çok teşekkür ederim! – zooter
Rica ederim. Çalıştığına sevindim! – Phoca