Android Can';我无法从地理编码器那里得到地址
我创建了简单的代码,只用于从字符串位置获取地址。代码在星线处刹车。为什么?如果我打印异常:服务不可用Android Can';我无法从地理编码器那里得到地址,android,google-geocoder,Android,Google Geocoder,我创建了简单的代码,只用于从字符串位置获取地址。代码在星线处刹车。为什么?如果我打印异常:服务不可用 List<Address> address; String myAddress = "Vilnius"; Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault()); System.out.println("coder : :"
List<Address> address;
String myAddress = "Vilnius";
Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
System.out.println("coder : :" + coder);
**address = coder.getFromLocationName(myAddress, 1);**
ObjLoc1adr = coder.getFromLocationName(ObjLoc1String, 1);
ObjLoc2adr = coder.getFromLocationName(ObjLoc2String, 1);
列表地址;
字符串myAddress=“Vilnius”;
Geocoder coder=新的Geocoder(getApplicationContext(),Locale.getDefault());
System.out.println(“编码器::”+编码器);
**address=coder.getFromLocationName(myAddress,1)**
ObjLoc1adr=coder.getFromLocationName(ObjLoc1String,1);
ObjLoc2adr=coder.getFromLocationName(ObjLoc2String,1);
因此,您应该始终将代码包装在try块中以捕获IOException。无法保证地理编码器总能找到位置或服务是可访问的。这不一定是您的代码或设备的故障。一个简单的连接问题或超时就足以阻止你的应用程序
要保存,请执行以下操作:
if(GeoCoder.isPresent()) {
Geocoder geocoder = new Geocoder(this);
String myAddress = "Vilnius";
List<Address> addresses = null;
try {
address = geocoder.getFromLocationName(myAddress, 1);
if (!addresses.isEmpty()) {
Address address = list.get(0);
// do something with your address
} else {
// No results for your location
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(GeoCoder.isPresent()){
Geocoder Geocoder=新的Geocoder(本);
字符串myAddress=“Vilnius”;
列表地址=空;
试一试{
地址=geocoder.getFromLocationName(myAddress,1);
如果(!addresses.isEmpty()){
地址=list.get(0);
//把你的地址改一下
}否则{
//您所在的位置没有结果
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
检查Geocoder.isPresent()
是否返回true。顺便说一句,我在几年前就写了他的代码,并且它正常工作。没有什么变化。现在它不工作了。一些模拟器不做反向地理编码,这取决于API版本号。从内存来看,API级别7还可以。我使用的是19 API,在我的nexus 5上,mounth之前它还可以工作,这很奇怪