如何从android获取新的经纬度坐标

时间:2015-01-14 10:55:21

标签: android gps

我如何获取位置详细信息:

AppLocationService appLocationService = new AppLocationService(getApplicationContext());

Location nwLocation= appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
nwLocation.getLatitude();
nwLocation.getLongitude();

发生了什么事,因为我的代码已经知道位置....它给了我最后更新的位置说现在它的晚上。它给了我早上更新的位置

我想要什么::如何制作新的网络请求以获取当前位置


AppLocationService.java

public class AppLocationService extends Service implements LocationListener {

    protected LocationManager locationManager;
    Location location;

    private static final long MIN_DISTANCE_FOR_UPDATE = 0;
    private static final long MIN_TIME_FOR_UPDATE = 0;

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

    public Location getLocation(String provider) {
        if (locationManager.isProviderEnabled(provider)) {
            locationManager.requestLocationUpdates(provider,
                    MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

4 个答案:

答案 0 :(得分:1)

您的代码正在注册以接收位置更新,并返回上一个已知位置。预期会发生什么,因为您没有等待接收更新的更新。

一旦系统调用了“onLocationChanged”方法,你将获得新方法。然后,您可以从服务发送广播。

您正试图以同步模式获取当前位置,这是不可能的,因为查找更新的位置需要一些时间。

答案 1 :(得分:0)

您无法快速获取更新位置信息的主要原因是您依赖于NETWORK_PROVIDER。

您应该使用此

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

答案 2 :(得分:0)

使用onLocationChanged方法从参数位置获取经度和纬度

代码:

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = new Location(location);
    latitude=mCurrentLocation .getLatitude();
    longitude=mCurrentLocation .getLongitude();
}

答案 3 :(得分:0)

如何显式调用onLocationChanged

public void locationlist()
     {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,
                    50, 0, this);
            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);

            if (location != null) {
              System.out.println("Provider " + provider + " has been selected.");
              onLocationChanged(location);

            } else {}
         if(location == null)
         {

         }
         else{
             lat = (double) (location.getLatitude());
             lng = (double) (location.getLongitude());
         }

     }

现在在计时器中调用您想要的方法locationlist。