关于 Android中googleMaps的Geocoder, “the service is not available”解决办法

1 篇文章 0 订阅
1 篇文章 0 订阅

                                             Android中用Json解析googleMaps经纬度

         大家在用googleMaps中的Geocoder进行地理位置反转经纬度时, 会出现这个错误Geocoder, “the service is not available” && Geocoder返回List为null,得不到街道信息。其实googleMaps中的Getcoder反转经纬度一直存在BUG,在前一段时间google终止了这项服务,在google官网http://code.google.com/intl/zh-CN/apis/maps/documentation/geocoding/已经说明了,google让我们只能通过Json解析地址才能获取地理位置的经纬度了,现在总结了一下内容:

         Json解析包到android3.1才集成到android里面,现在还不能直接使用。我们现在用Json解析数据,首先下载Json包导入到你的项目中,Json解析包网址是http://code.google.com/p/google-gson/downloads/detail?name=google-gson-1.7.1-release.zip。然后你在浏览器中输入http://maps.google.com/maps/api/geocode/json?address=平顶山学院ka&sensor=false,可以看到一个Json数据,可以看到我们平顶山学院的一些数据,其中可以看到我们学校的经纬度"location" : { "lat" : 33.766190, "lng" : 113.1928140 },,这样我们不需要Getcoder类也可以解析出来地理位置的经纬度了,接下来我们看一下实现代码:

public static JSONObject getLocationInfo(String address) 
{//address是我所反转平顶山学院名字
HttpGet httpGet=new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" + address    
                + "ka&sensor=false");
HttpClient client=new DefaultHttpClient();
HttpResponse httpResponse;
StringBuffer stringBuffer=new StringBuffer();
try {
httpResponse=client.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
InputStream inputStream=httpEntity.getContent();
int b;
while ((b=inputStream.read())!=-1)
{
stringBuffer.append((char)b);
}

catch (ClientProtocolException e)
{
}
catch (Exception e)
{
}
JSONObject jsonObject=new JSONObject();
try {
jsonObject=new JSONObject(stringBuffer.toString());
Log.d(tab,"stringbuffer的值"+stringBuffer.toString());

catch (Exception e) {
}
return jsonObject;
}
public  static GeoPoint getGeoPoint(JSONObject jsonObject)
{
Double lon=new Double(0);//地点的纬度
Double lat=new Double(0);//地点的经度
try {
 lon=((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
 lat=((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");//上面这些参数你可以在http://maps.google.com/maps/api/geocode/json?address=平顶山学院ka&sensor=false中看到


catch (JSONException  e)
{
Log.d(tab,"geoPoint方法捕获的异常"+e.getMessage());
}
catch (Exception e) 
{
}
return new GeoPoint((int) (lat* 1E6), (int) (lon* 1E6));
}

我们在响应事件里写上这句话 controller.animateTo(getGeoPoint(getLocationInfo("平顶山学院"))); googleMaps就会显示我的学校,哈哈哈,用Json解析地理位置经纬度就这样实现了,再也不用Geocoder类了,岂不是很好。当然,这些代码不是我想出来的,我是在google的英文网站上看到一个外国人是这样写的,我自己试试测试一下,没想到成功了,建议大家好好看看这个网站http://code.google.com/intl/zh-CN/apis/maps/documentation/geocoding/,相信大家会有所收获的,搞了一天到现在终于搞出来了,累死了,我睡觉 了,谢谢大家

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值