android获取当前位置名称

Raj*_*ran 5 gps android google-maps geocoding latitude-longitude

嘿伙计们,我想要获得当前位置的名称.我做了编码获取当前位置(lat,lang)我如何显示相对地名

(即)13.006389 - 80.2575:Adyar,Chennai,India

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // called when the location provider status changes. Possible status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or AVAILABLE.
    }
    public void onProviderEnabled(String provider) {
        // called when the location provider is enabled by the user
    }
    public void onProviderDisabled(String provider) {
        // called when the location provider is disabled by the user. If it is already disabled, it's called immediately after requestLocationUpdates
    }

    public void onLocationChanged(Location location) {
        double latitute = location.getLatitude();
        double longitude = location.getLongitude();
        // do whatever you want with the coordinates
    }
});
Run Code Online (Sandbox Code Playgroud)

Sco*_*ion 7

这会将lat&lng转换为String Address,并在示例的文本字段中设置它.这是通过使用反向地理编码的概念完成的,并且Geocoder在Android中有一个类.

    //  Write the location name.
    //

    try {

        Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
        List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
        if (addresses.isEmpty()) {
            yourtextboxname.setText("Waiting for Location");
        }
        else {
            if (addresses.size() > 0) {
                yourtextboxname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Ale*_*cas 0

您要查找的短语是“反向地理编码”。 StackOverflow 上的另一个问题讨论了相同的主题 - 您可以使用该人选择的答案:)