GPSを使用して現在地を取得しようとしています。だから最初に私はGPSが有効かどうかをチェックしている。その時点でGPSが無効になっている場合は、アラートを表示し、設定からGPSを有効にするようにユーザーをリダイレクトします。 GPSが有効になっているとき、onResumeアクティビティでその時点で設定を自分のアプリケーションにリダイレクトすると、現在の位置が0.00になります。 GPSを有効にすると、現在の現在地をどのように取得できますか?Android:onResumeアクティビティの現在の場所を取得
ありがとうございます。
@Override
protected void onResume() {
super.onResume();
getCurrenLocation();
}
public void getCurrenLocation() {
gps = new GpsTracker(GetLocation.this);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// boolean gps_enabled = false;
// boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (gps_enabled && network_enabled) {
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
// Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
if (latitude != 0.0 && longitude != 0.0) {
txtLocation.setVisibility(View.VISIBLE);
txtLong.setVisibility(View.VISIBLE);
txtLat.setVisibility(View.VISIBLE);
txtLocation.setText("Your Location is - \nLat: " + latitude + "\nLong: " + longitude);
txtLong.setText(Double.toString(longitude));
txtLat.setText(Double.toString(latitude));
}
Log.d("latitude onResume:: :: :: ::", String.valueOf(latitude));
Log.d("longitude onResume:: :: :: ::", String.valueOf(longitude));
}
} else if (!gps_enabled && !network_enabled) {
alertDialog();
}
}
このデモを試してみてくださいサービスをGoogle PlayからFusedLocationProviderを使用する必要があります。それはGPS技術の仕組みではありません。 – Budius
瞬時ではないし、LocationListenerが必要な場合や、アップデートを取得しない場合があります。 http://developer.android.com/intl/es/reference/android/location/LocationListener.html – Enpi