【问题标题】:getting city name from longitude and latitude, android从经度和纬度获取城市名称,android
【发布时间】:2013-12-09 09:16:48
【问题描述】:

我正在编写此代码,以使用我的经度和纬度获取您当前位置的城市名称。

这是我正在编写的代码:

protected String currentLatitude, currentLongitude;
double lat, lon;
protected LocationManager locationManager;
private LocationListener ll;
locationManager = (LocationManager) getSystemService(
            Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            Toast.makeText(this,
                    "GPS enabled. Finding fine location. ",
                    Toast.LENGTH_SHORT).show();
        ll = new GpsListener();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, ll);

    } else {
        if (locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

                Toast.makeText(this,
                        "GPS disabled. Finding coarse location.",
                        Toast.LENGTH_SHORT).show();
            ll = new GpsListener();
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, ll);

        } else {
            Toast.makeText(this,
                    "Enable location settings to get current location.",
                    Toast.LENGTH_LONG).show();

        }
    }

private class GpsListener implements LocationListener {

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null) {
            try {
                Toast.makeText(getApplicationContext(), "Location Found",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {

            }

            lat = location.getLatitude();
            lon = location.getLongitude();

            currentLatitude = Double.toString(lat);
            currentLongitude = Double.toString(lon);
            try {
                if (ll != null)
                    locationManager.removeUpdates(ll);
            } catch (Exception e) {

            }

            locationManager = null;

        } else {
            Toast.makeText(getApplicationContext(), "No Location Found",
                    Toast.LENGTH_LONG).show();

        }

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}// end of Class

我不知道如何从经度、纬度中获取城市名称。

我尝试这样做,但地址为 NULL。

【问题讨论】:

    标签: android location


    【解决方案1】:

    您尝试执行的操作称为反向地理编码。 您可以使用 Google 的解决方案,它可能无法在任何设备上运行(也许这是您的问题)。

    或者您可以使用真正免费的开源解决方案,例如:

    OpenStreetMap 项目: http://nominatim.openstreetmap.org/reverse?format=xml&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1

    你也可以使用http://wikimapia.org/ API

    【讨论】:

    【解决方案2】:

    这可能对您有用,因为它将返回您提供给它的纬度和经度地址..

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new
    Geocoder(this, 
          Locale.getDefault());
     addresses =
     geocoder.getFromLocation
    (latitude,longitude, 1);
    String address =addresses.get
    (0).getAddressLine(0);
     String city =addresses.
      get(0). getAddressLine(1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2017-10-07
      • 2014-04-14
      相关资源
      最近更新 更多