android谷歌反地理,android – 谷歌地理编码服务是不可用的(协调地址)

这个问题的常见答案是您必须重新启动设备.

当然你不能告诉你的用户重启设备以使你的应用程序运行,所以我的解决方案是使用HTTP回退,这是我在我的代码中使用的AsyncTask.

你必须根据你的情况修改它,因为我从位置查找地址而不是相反.

private class GetAddressPositionTask extends

AsyncTask {

@Override

protected void onPreExecute() {

super.onPreExecute();

}

@Override

protected LatLng doInBackground(String... plookupString) {

String lookupString = plookupString[0];

final String lookupStringUriencoded = Uri.encode(lookupString);

LatLng position = null;

// best effort zoom

try {

if (geocoder != null) {

List

addresses = geocoder.getFromLocationName(

lookupString, 1);

if (addresses != null && !addresses.isEmpty()) {

Address first_address = addresses.get(0);

position = new LatLng(first_address.getLatitude(),

first_address.getLongitude());

}

} else {

Log.e(TAG, "geocoder was null, is the module loaded? "

+ isLoaded);

}

} catch (IOException e) {

Log.e(TAG, "geocoder failed, moving on to HTTP");

}

// try HTTP lookup to the maps API

if (position == null) {

HttpGet httpGet = new HttpGet(

"http://maps.google.com/maps/api/geocode/json?address="

+ lookupStringUriencoded + "&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 {

// Log.d("MAPSAPI", stringBuilder.toString());

jsonObject = new JSONObject(stringBuilder.toString());

if (jsonObject.getString("status").equals("OK")) {

jsonObject = jsonObject.getJSONArray("results")

.getJSONObject(0);

jsonObject = jsonObject.getJSONObject("geometry");

jsonObject = jsonObject.getJSONObject("location");

String lat = jsonObject.getString("lat");

String lng = jsonObject.getString("lng");

// Log.d("MAPSAPI", "latlng " + lat + ", "

// + lng);

position = new LatLng(Double.valueOf(lat),

Double.valueOf(lng));

}

} catch (JSONException e) {

Log.e(TAG, e.getMessage(), e);

}

}

return position;

}

@Override

protected void onPostExecute(LatLng result) {

Log.i("GEOCODE", result.toString());

super.onPostExecute(result);

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值