2012-10-21 60 views
1

我有简单的流程:当我点击按钮时,我应该只在当时获得当前的纬度/经度, 我正在使用LocationListner,但问题是onLocationChange()方法只在我改变位置时调用,否则给出0.00/0.00元。我做什么,请帮助我。如何一次获得当前的GPS位置?

+0

http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-位置编程功能于安卓 – marcinj

回答

1
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener(); 
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
     mlocListener); 

public class MyLocationListener implements LocationListener { 

@Override 
public void onLocationChanged(Location loc) { 
    loc.getLatitude(); 
    loc.getLongitude(); 

    Geocoder gcd = new Geocoder(getApplicationContext(), 
      Locale.getDefault()); 
    try { 
     mAddresses = gcd.getFromLocation(loc.getLatitude(), 
       loc.getLongitude(), 1); 

    } catch (IOException e) { 

    } 

    String cityName = (mAddresses != null) ? mAddresses.get(0) 
      .getLocality() : TimeZone.getDefault().getID(); 
    String countryName = (mAddresses != null) ? mAddresses.get(0) 
      .getCountryName() : Locale.getDefault().getDisplayCountry() 
      .toString(); 


    mCurrentSpeed.setText("Longitude"+loc.getLongitude()+" Latitude"+loc.getLatitude()); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Gps Disabled", 
      Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Gps Enabled", 
      Toast.LENGTH_SHORT).show(); 
} 

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

您可以实现您的位置侦听器,记住传递的位置,可能以某种方式对它们进行平均。