cannot get place from google map using location

喜夏-厌秋 提交于 2019-12-11 08:34:20

问题


I try to get place(ex: locality, country, postal code) form google map using latitude and longitude. The following is my code that got null data:

Edit:

public void onLocationChanged(Location location) {

    geocoder = new Geocoder(this, Locale.getDefault());

    if (location != null) {

        try {
            addresses = geocoder.getFromLocation(location.getLatitude()/1E6,
                    location.getLongitude()/1E6, 1);
            if (addresses.size() > 0) {
                resultAddress = addresses.get(0);

                locality = resultAddress.getLocality();
                sublocality = resultAddress.getSubLocality();
                postalcode = resultAddress.getPostalCode();
                country = resultAddress.getCountryName();
                adminarea = resultAddress.getSubAdminArea();

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

I tried to assign latitude and longitude manually, but got nothing.

Any one can correct my mistakes ? Thank you


回答1:


I think the problem is on the parameters of the method. I guess that location is a GeoPoint In that case, instead of

addresses = geocoder.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);

try this:

addresses = geocoder.getFromLocation(location.getLatitude()/1E6,
                    location.getLongitude()/1E6, 1);

because the coordinates in a geopoint are represented in microdegrees

Edit I copied your code and tried it. Assuming your "location" object is a GeoPoint, the code that works is as follows:

GeoPoint location = new GeoPoint(lat, lon);
    if (location != null) {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(location.getLatitudeE6()/1E6,
                    location.getLongitudeE6()/1E6, 1);
            if (addresses.size() > 0) {
                Address resultAddress = addresses.get(0);

                String locality = resultAddress.getLocality();
                String sublocality = resultAddress.getSubLocality();
                String postalcode = resultAddress.getPostalCode();
                String country = resultAddress.getCountryName();
                String adminarea = resultAddress.getSubAdminArea();

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

however, if you are testing it on the emulator, be sure you have internet connection. Otherwise, the method "getFromLocation" cannot find out the address and won't display anything. If you don't have any error in the logcat and the problem is only that nothing is displayed, that is the problem: no network.




回答2:


Do you have the <uses-permission android:name="android.permission.INTERNET" /> in manifest.xml?



来源:https://stackoverflow.com/questions/6914474/cannot-get-place-from-google-map-using-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!