2

我试图得到latitudeandlongitude但有时network is available我没有得到latitudeand的价值longitude。我正在使用MyLocationListener类并设置条件,但有一段时间value没有得到。

protected void showCurrentLocation() 
{
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) 
    {
        counter++;          
        latitude=location.getLatitude();
        longitude=location.getLongitude();
        altitude=location.getAltitude();
    }
 }   

private class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location location)
    {
         counter++;
         latitude=location.getLatitude();
         longitude=location.getLongitude();
         altitude=location.getAltitude();
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle b) 
    {
    }
    @Override
    public void onProviderDisabled(String s) 
    {   
    }
    @Override
    public void onProviderEnabled(String s) 
    {
    }
}
4

2 回答 2

5

这里获得经度纬度的最佳方法:

/** PROCESS for Get Longitude and Latitude **/
locationManager     = (LocationManager) getSystemService(LOCATION_SERVICE);

// Define a listener that responds to location updates

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.

        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d("msg", "changed Loc : "+longitude + ":"+latitude);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// check if GPS enabled     
if(isGPSEnabled){

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

    if(location != null)
    {
        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d("msg", "Loc : "+longitude + ":"+latitude);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }else
    {
        /*
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
         */

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(location != null)
        {
            longitude = String.valueOf(location.getLongitude());
            latitude = String.valueOf(location.getLatitude());

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }else
        {
            longitude   = "0.00";
            latitude    = "0.00";
        }
    }
}

如果设备无法使用 currentLocation 获取,GPS那么它将采用 fronNetworkProvider否则它将采用 0,0 作为我的要求,但您可以根据您的要求进行修改

愿它对你有所帮助。

快乐编码:D

于 2013-10-04T07:11:14.870 回答
0

看,这是一个非常普遍的问题。为了获得准确的纬度和经度,您必须使用 GPS 也必须在移动设备上激活,但 GPS 也有一些限制。GPS 在开阔的天空下工作效率最高。你不会在建筑物内有一个明确的范围。因此,在开阔的天空下尝试您的代码并检查响应。

于 2013-10-04T07:09:23.270 回答