如何找到当前位置'纬度经度

时间:2011-06-22 09:28:10

标签: android gps

我想开发一个应用程序,当我启动应用程序时,它将首先给出我当前位置的纬度和经度。这是我的代码:

   LocationListener locLis=new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}      
@Override
public void onProviderEnabled(String provider) {}           
@Override
public void onProviderDisabled(String provider) {}  
@Override
public void onLocationChanged(Location location) 
{
// TODO Auto-generated method stub
   Double lat=location.getLatitude();
   Double lon=location.getLongitude();
   Log.i("Latitude=="+lat,"=="+lon);

}
};
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0,0, locLis);

我在清单文件中使用ACCESS_FINE_LOCATION。 但是,当我启动应用程序时,它找不到经度和纬度。为什么?如果我从命令提示符更改位置的纬度经度,则它将显示更新的纬度和经度。请任何人帮助我

3 个答案:

答案 0 :(得分:1)

获取此代码并享受    

public void find_Location(Context con)     {         Log.d(“查找位置”,“在find_location中”);         this.con = CON;         String location_context = Context.LOCATION_SERVICE;         locationManager =(LocationManager)con.getSystemService(location_context);

    List<String> providers = locationManager.getProviders(true);
   for (String provider : providers) 
   {
       locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener()
       {
           public void onLocationChanged(Location location) {}
           public void onProviderDisabled(String provider){}
           public void onProviderEnabled(String provider){}
           public void onStatusChanged(String provider, int status,Bundle  extras){}
       });
       Location location = locationManager.getLastKnownLocation(provider);
       if (location != null)
       {
           latitude = location.getLatitude();
           longitude = location.getLongitude();
           addr=ConvertPointToLocation(latitude,longitude);
                   String temp_c=SendToUrl(addr);

       } 
   }
}

public String ConvertPointToLocation(double pointlat,double pointlog) {

    String address = "";
    Geocoder geoCoder = new Geocoder(con,
            Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocation(pointlat,pointlog, 1);
        if (addresses.size() > 0) {
            for (int index = 0; index < addresses.get(0)
                    .getMaxAddressLineIndex(); index++)
                address += addresses.get(0).getAddressLine(index) + " ";
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return address;
}  

答案 1 :(得分:0)

您的代码设置为仅在更改时显示位置。 我认为为了在应用程序启动时显示它,您应该尝试将其放在onProviderEnabled()或其他一些初始化例程中。

答案 2 :(得分:0)

你应该这样做

            public class MyLocationListener implements LocationListener
{

    @Override
    public void onLocationChanged(Location loc)

    {}

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

    }
       }
     mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();

     mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);