【问题标题】:Get current latitude and longitude in the string获取字符串中当前的经纬度
【发布时间】:2013-10-11 02:12:46
【问题描述】:

我试图获得latitudelongitude,但有时network is available 但我没有获得latitudelongitude 的价值。我正在使用 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) 
    {
    }
}

【问题讨论】:

  • 你到底想要什么?
  • 你在哪里调用 MyLocationListener 类?

标签: android gps android-location


【解决方案1】:

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

/** 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";
        }
    }
}

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

希望对你有帮助。

快乐编码:D

【讨论】:

  • 嗨,Pratik,有些设备没有获得当前的纬度和经度。我正在使用LocationManager.GPS_PROVIDERLocationManager.NETWORK_PROVIDER
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多