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/,相信大家会有所收获的,搞了一天到现在终于搞出来了,累死了,我睡觉 了,谢谢大家

更多相关文章

  1. Android(安卓)学习笔记 ——第二行代码 基于位置的服务
  2. Android根据经纬度获取地址
  3. Android(安卓)原生定位
  4. Android(安卓)如何获取经纬度
  5. Android(安卓)高手进阶教程(十四)之----Android(安卓)Location的
  6. Android(安卓)地址转换为经纬度
  7. android:如何从照片中获取拍摄地址信息
  8. 三十、Google Map导航应用
  9. Android获取当前位置(GPS和网络定位)

随机推荐

  1. android中sqlite操作
  2. Android向通讯录添加联系人的一般方法
  3. 关于使用adb命令安装apk到手机中的问题
  4. Android(安卓)Snake
  5. Android中日志信息的打印方式
  6. Android在TextView中设置图片显示问题
  7. Android—自动弹出软键盘
  8. android 线程通信Handler Bundle
  9. android tools 汇总
  10. 最全面的Android学习路线 思维导图附知识