2016-04-08 15 views
2

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(); 
     } 
    } 
+1

このデモを試してみてくださいサービスをGoogle PlayからFusedLocationProviderを使用する必要があります。それはGPS技術の仕組みではありません。 – Budius

+1

瞬時ではないし、LocationListenerが必要な場合や、アップデートを取得しない場合があります。 http://developer.android.com/intl/es/reference/android/location/LocationListener.html – Enpi

答えて

0

緯度と経度を取得するには、このクラスを試してみてください

import android.app.AlertDialog.Builder; 
import android.app.Service; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 

public class GPSTracker extends Service implements LocationListener { 


private Context context; 

boolean isGPSEnabled=false; 
boolean isNetworkEnabled=false; 
boolean canGetLocation=false; 

Location location; 

double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10; 
private static final long MIN_TIME_BW_UPDATES=1000*60*1; 

protected LocationManager locationManager; 

public GPSTracker(Context context) 
    { 
this.context=context; 
getLocation(); 
    } 

    public Location getLocation() 
    { 
    try{ 
    locationManager=(LocationManager)  context.getSystemService(LOCATION_SERVICE); 
    isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 




if(!isGPSEnabled && !isNetworkEnabled) 
{ 
    showSettingsAlert(); 
} 
else{ 
    this.canGetLocation=true; 
    if(isNetworkEnabled) 
    { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 


    if(locationManager !=null) 
    { 
     location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

    if(location !=null) 
    { 
     latitude=location.getLatitude(); 
     longitude=location.getLongitude(); 
    } 
    } 
    } 

    if(isGPSEnabled){ 
     if(location==null) 
     { 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

      if(locationManager !=null) 
      { 
       location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

       if(location !=null) 
       { 
        latitude=location.getLatitude(); 
        longitude=location.getLongitude(); 
       } 

      } 

     } 
    } 


} 


} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
return location; 
} 

public void stopUsingGPS() 
    { 
    if(locationManager !=null) 
    { 
    locationManager.removeUpdates(GPSTracker.this); 
    } 
    } 

    public double getLatitude() 
{ 
    if(location!=null) 
    { 
    latitude=location.getLatitude(); 
    } 
    return latitude; 

} 


public double getLongitude() 
{ 
    if(location!=null) 
    { 
    longitude=location.getLongitude(); 
    } 
    return longitude; 

} 

public boolean canGetLocation(){ 

return this.canGetLocation; 

    } 

    public void showSettingsAlert(){ 
    Builder alertDialog=new Builder(context); 
alertDialog.setTitle("GPS Settings"); 
alertDialog.setMessage("GPS is not enabled.Do you want to go to the settings menu?"); 
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     context.startActivity(intent); 
    } 
}); 

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 

    } 
}); 
alertDialog.show(); 
} 


@Override 
public void onLocationChanged(Location location) { 
// TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
// TODO Auto-generated method stub 

} 

@Override 
public void onProviderDisabled(String provider) { 
// TODO Auto-generated method stub 

} 

@Override 
public IBinder onBind(Intent intent) { 
// TODO Auto-generated method stub 
return null; 
} 


} 

そして

GPSTracker gps; 

public void getCurrenLocation() { 
    gps = new GPSTracker(classname.this); 

    if (gps.canGetLocation) { 
     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 
       } 
} 

希望これは

+0

ありがとうございますが、私はgetCurrenLocation()を "onResume"で呼び出すときにまだ0.0を取得しています –

+0

これは私のためにうまく動作します.. ..すべての権限をマニフェストに含めますか? – Benedict

+0

ええ、私はマニフェストのすべてのアクセス許可を含めました –

1

はあなたのマニフェストに権限を追加したのに役立ちますか?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

GPSを有効にした直後に位置を取得することはできないと思います。 GPSはあいまいさを修正し、あなたの位置を推定する必要があります。 すぐに場所が必要な場合は、粗い場所にすることができます。しかし、この場所はGPSよりもはるかに不正確です。

+0

はい、既に追加済みです

2

@komalプロバイダと場所に応じて、場所を取得するには常に時間がかかります。 GPSは最低3人の衛星からの信号を受け取り、三辺測量を使用するので、信号の強度と位置に応じて時間がかかります。 Google Mapアプリでも10秒以内にLat/Lngを取得できます。あなたは

あなたがすることはできません「GPSを有効にするとき、どのように私はインスタント現在位置を取得することができます」https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates

+0

右です! LatLongを取得するには時間がかかります –

+0

瞬間的な場所では、最後の既知の場所のみを取得できます。または、サービスを実行し、10mと100msの位置更新を要求します。 –

関連する問題