2014-12-14 2 views
0

활동에서지도를 사용하지 않고 현재 사용자 위치를 얻는 방법은 무엇입니까? 좌표의 좌표를 저장하고 싶습니까? 이 코드를 사용했지만 작동하지 않습니다!안드로이드에서지도를 사용하지 않고 현재 위치를 얻으십시오.

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

Criteria criteria = new Criteria(); 

String provider = locationManager.getBestProvider(criteria, true); 

Location location = locationManager.getLastKnownLocation(provider); 
latitude = location.getLatitude(); 
longitude = location.getLongitude(); 
+0

'이 코드를 사용했지만 작동하지 않습니다! '- 작동하지 않는 코드를 알려주십시오. –

+0

위치는 항상 null을 반환합니다! – Mohammed

+0

매니페스트에''을 선언 했습니까? –

답변

0

이 내가 잘 작동, 내 응용 프로그램에서 사용하고 무엇을 :

public class TheLocationClass implements LocationListener { 
    final long MIN_TIME_INTERVAL = 60 * 1000L; 
    private boolean isGPSEnabled = false; 
    private boolean isNetworkEnabled = false; 
    private Location location;   // location.getLatitude() & location.getLongitude() will give you what you need... 
    private LocationManager locationManager; 

    public TheLocationClass() { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    } 

    public void requestForUpdates(){ 

     if(locationManager == null) { 
      return; 
     } 

     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (isNetworkEnabled) { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,TheLocationClass.this); 
      if (locationManager != null) { 
       Location tempLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       if (tempLocation != null && isBetterLocation(tempLocation,location)) 
        location = tempLocation; 
      } 
     } 
     if (isGPSEnabled) { 
      locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, TheLocationClass.this, null); 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, TheLocationClass.this); 
      if (locationManager != null) { 
       Location tempLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
       if (tempLocation != null && isBetterLocation(tempLocation,location)) 
        location = tempLocation; 
      } 
     } 
    } 

    public void stopLocationUpdates(){ 
     if(locationManager!=null){ 
      locationManager.removeUpdates(TheLocationClass.this); 
     } 
    } 

    private boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      return true; 
     } 

     // Check whether the new location fix is newer or older 
     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     boolean isSignificantlyNewer = timeDelta > MIN_TIME_INTERVAL; 
     boolean isSignificantlyOlder = timeDelta < -MIN_TIME_INTERVAL; 
     boolean isNewer = timeDelta > 0; 

     // If it's been more than two minutes since the current location, use the new location because the user has likely moved 
     if (isSignificantlyNewer) { 
      return true; 
     } else if (isSignificantlyOlder) { 
      return false; 
     } 

     // Check whether the new location fix is more or less accurate 
     int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
     boolean isLessAccurate = accuracyDelta > 0; 
     boolean isMoreAccurate = accuracyDelta < 0; 
     boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

     // Check if the old and new location are from the same provider 
     boolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider()); 

     // Determine location quality using a combination of timeliness and accuracy 
     if (isMoreAccurate) { 
      return true; 
     } else if (isNewer && !isLessAccurate) { 
      return true; 
     } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
      return true; 
     } 
     return false; 
    } 

    private boolean isSameProvider(String provider1, String provider2) { 
     if (provider1 == null) { 
      return provider2 == null; 
     } 
     return provider1.equals(provider2); 
    } 

    @Override 
    public void onLocationChanged(Location loc) { 
     if (loc != null && isBetterLocation(loc, location)) { 
      location = loc; 
     } 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 

그냥 requestForUpdates를이 클래스의 인스턴스를 생성하고()를 호출.

가변 위치가 장치의 정확한 위치를 새로 업데이트했습니다.

관련 문제