不能使用的位置,距离谷歌地图获得的地方 [英] cannot get place from google map using location

查看:132
本文介绍了不能使用的位置,距离谷歌地图获得的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让地方(例如:地方,国家,邮政code)使用形式谷歌地图
纬度和经度。以下是我的code,进行了空数据:

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:

编辑:

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

推荐答案

我认为这个问题是关于方法的参数。我猜的位置的GeoPoint 在这种情况下,而不是

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);

试试这个:

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

因为在GeoPoint的坐标重新以微psented $ P $

because the coordinates in a geopoint are represented in microdegrees

修改
我复制你的code和尝试。假设你的位置对象是一个GeoPoint对象时,code,它的工作原理如下:

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();
        }

    }

不过,如果你是在模拟器上测试它,要确保你有互联网连接。否则,该方法getFromLocation无法找到该地址并不会显示任何内容。如果你没有任何错误的logcat的,问题只是显示了什么,这就是问题所在:没有网络

这篇关于不能使用的位置,距离谷歌地图获得的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆