我正在尝试使用htc desire c来反转地理编码,在运行应用程序时,它的抛出服务不可用异常。但是GeoCoder.isPresent()正在返回'true'。请帮助我找到问题。
这是我的代码:
public class MainActivity extends Activity {
LocationManager lManager;
String provider;
Location location ;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = lManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
location = lManager.getLastKnownLocation(provider);
}
boolean geoCoder = false;
Geocoder geo = new Geocoder(this, Locale.getDefault());
geoCoder = Geocoder.isPresent();
System.out.println("GEO CODER : "+geoCoder);
try {
List<Address> address = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
System.out.println("Size------------- "+address.size());
/*Address addr = address.get(0);
System.out.println("City "+addr.getLocality());*/
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("in here--------------");
e.printStackTrace();
}
}
日志:
03-16 10:33:58.609: I/System.out(30398): GEO CODER : true
03-16 10:33:58.609: I/System.out(30398): in here--------------
03-16 10:33:58.609: W/System.err(30398): java.io.IOException: Service not Available
03-16 10:33:58.619: W/System.err(30398): at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-16 10:33:58.619: W/System.err(30398): at com.example.geocoder.MainActivity.onCreate(MainActivity.java:46)
03-16 10:33:58.619: W/System.err(30398): at android.app.Activity.performCreate(Activity.java:4538)
03-16 10:33:58.629: W/System.err(30398): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.access$600(ActivityThread.java:139)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
03-16 10:33:58.629: W/System.err(30398): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 10:33:58.629: W/System.err(30398): at android.os.Looper.loop(Looper.java:156)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-16 10:33:58.639: W/System.err(30398): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 10:33:58.639: W/System.err(30398): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 10:33:58.639: W/System.err(30398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-16 10:33:58.639: W/System.err(30398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-16 10:33:58.649: W/System.err(30398): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
@smk为您提供了有用的链接。为了避免大量研究,我会尽量回答你的问题。地理编码器并不总是返回值。您可以尝试在for循环中发送3次请求。我应该能够至少返回一次。如果没有,那么它们可能是连接问题,或者可能是其他问题,例如服务器没有回复您的请求。
我也有一个while循环,但我曾经尝试过最多10次。有时,即使它连接到互联网,也从未返回任何内容。然后,我使用this更可靠的方式每次获取地址:
我曾经获得纬度和经度,然后请求谷歌服务器,回复一个包含有关位置坐标的各种信息的JSOn对象。这是功能:
public JSONObject getLocationInfo() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
我打电话给它如下:
JSONObject ret = getLocationInfo();
JSONObject location;
String location_string;
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
希望这会有所帮助。我也厌倦了依靠Geocoder。这对我有用。虽然它可能比地理编码器慢。您只需使用您拥有的纬度和经度坐标替换URL即可。尝试在Web浏览器中查看返回的JSON对象。您将看到如何提取地址字符串。尝试并阅读这些主题:
Geocoder doesn't always return a value和geocoder.getFromLocationName returns only null