Android反向地理编码

Android反向地理编码,android,google-maps,geocoding,Android,Google Maps,Geocoding,我想从某个地方找到一个地址。我在Fragement中的asynctask中这样做。所有其他代码都可以正常工作(检查API并在此基础上设置一些UI内容),但这一节中的地理编码就不行了。我遵循了StackOverflow上其他地方的一个例子(记不清确切的主题) TextView locTxt=(TextView)findViewById(R.id.locationText); 地理编码器; 列出地址; 双x=55.971627; 双y=-3.602585; 尝试 { geocoder=新的geoco

我想从某个地方找到一个地址。我在Fragement中的asynctask中这样做。所有其他代码都可以正常工作(检查API并在此基础上设置一些UI内容),但这一节中的地理编码就不行了。我遵循了StackOverflow上其他地方的一个例子(记不清确切的主题)

TextView locTxt=(TextView)findViewById(R.id.locationText);
地理编码器;
列出地址;
双x=55.971627;
双y=-3.602585;
尝试
{
geocoder=新的geocoder(getActivity(),Locale.ENGLISH);
地址=地理编码器。getFromLocation(x,y,1);
StringBuilder str=新的StringBuilder();
if(geocoder.isPresent())
{
Toast.makeText(getApplicationContext(),
“geocoder present”,Toast.LENGTH_SHORT.show();
地址返回地址=地址。获取(0);
String localityString=returnAddress.getLocality();
字符串city=returnAddress.getCountryName();
字符串region_code=returnAddress.getCountryCode();
字符串zipcode=returnAddress.getPostalCode();
str.append(localityString+“”);
str.append(城市+地区+代码+);
str.append(zipcode+“”);
locTxt.setText(str);
}
其他的
{
Toast.makeText(getApplicationContext(),“地理编码器不存在”,Toast.LENGTH\u SHORT.show();
}
}捕获(IOE){}

当它到达geocoder.isPresent()的if语句时,if失败,并继续执行程序的其余部分

问题在于Android Emulator。即使通过命令行设置坐标,仿真器也不会执行地理编码代码,因此无法检查地理编码程序是否存在。在物理设备上工作正常

您是否已添加internet权限<代码>@Andy抱歉,我以为我已经发回了这个帖子。我把它解决了。这是仿真器的问题。即使为模拟器设置GPS坐标,它也不会运行地理编码代码。但在物理设备上工作良好。
 TextView locTxt = (TextView) findViewById(R.id.locationText);
                Geocoder geocoder;
                List<Address> addresses;

                Double x = 55.971627;
                Double y = -3.602585;


                try
                {
                    geocoder = new Geocoder(getActivity(), Locale.ENGLISH);
                    addresses = geocoder.getFromLocation(x, y, 1);
                    StringBuilder str = new StringBuilder();
                    if (geocoder.isPresent())
                    {
                        Toast.makeText(getApplicationContext(),
                                "geocoder present", Toast.LENGTH_SHORT).show();
                        Address returnAddress = addresses.get(0);

                        String localityString = returnAddress.getLocality();
                        String city = returnAddress.getCountryName();
                        String region_code = returnAddress.getCountryCode();
                        String zipcode = returnAddress.getPostalCode();

                        str.append(localityString + "");
                        str.append(city + "" + region_code + "");
                        str.append(zipcode + "");

                        locTxt.setText(str);



                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "geocoder not present", Toast.LENGTH_SHORT).show();
                    }
                } catch (IOException e) {}