2014-09-17 2 views
0

Button 프레스에서 Location 개체를 가져 오려고합니다. LocationListener 콜백을 알고 있지만 특정 시간이나 거리 후에 만 ​​실행됩니다. 나는 즉시 위치를 얻고 싶다. 그렇게 할 수있는 방법이 있습니까?현재 lat long android 가져 오기

+0

(이 [튜토리얼]을 살펴 http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) –

+0

androidhive는 잘못된 튜토리얼 소스입니다 ...이 튜토리얼의 fx GPSTracker 클래스는 서비스를 확장합니다 ... 무엇 때문에? 왜 새로운 GPSTracker (AndroidGPSTrackingActivity.this)를 사용하여 "시작"했는가? (드럼 ... 더 드럼!) http://d.android.com (교육 섹션) – Selvin

답변

2

getLastKnownLocation()LocationManager에 전화하면 요청한 위치 제공 업체의 마지막으로 알려진 위치를 검색 할 수 있습니다. 그러나 :

  • 위치 된 수 있다는 것을
  • 위치는 null

이 따라서, 여러분의 코드가 그 시나리오 모두에 대처할 수 있어야합니다 수 있음.

0

사용 LocationManager 클래스 :

locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); 
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
if (location != null) { 
    latitude=location.getLatitude(); 
    longitude=location.getLongitude(); 
    Log.d("old","lat : "+latitude); 
    Log.d("old","long : "+longitude); 
    this.onLocationChanged(location); 
} 
+0

죄송하지만, 내 관리자가 말하는 것은 Google지도에서 즉시 위치를 표시 할 수 있다면 가능해야합니다. – user3921255

+0

Google지도는이 코드를 내부에서 사용하며 "Google지도" –

5
public class GPSHelper { 

    private Context context; 
    // flag for GPS Status 
    private boolean isGPSEnabled = false; 
    // flag for network status 
    private boolean isNetworkEnabled = false; 
    private LocationManager locationManager; 
    private Location location; 
    private double latitude; 
    private double longitude; 

    public GPSHelper(Context context) { 
     this.context = context; 

     locationManager = (LocationManager) context 
       .getSystemService(Context.LOCATION_SERVICE); 

    } public void getMyLocation() { 
     List<String> providers = locationManager.getProviders(true); 

     Location l = null; 
     for (int i = 0; i < providers.size(); i++) { 
      l = locationManager.getLastKnownLocation(providers.get(i)); 
      if (l != null) 
       break; 
     } 
     if (l != null) { 
      latitude = l.getLatitude(); 
      longitude = l.getLongitude(); 
     } 
    } 

    public boolean isGPSenabled() { 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     return (isGPSEnabled || isNetworkEnabled); 
    } 

    /** 
    * Function to get latitude 
    */ 
    public double getLatitude() { 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    */ 
    public double getLongitude() { 
     return longitude; 
    } 
관련 문제