利用jquery解析Google Map的geocode地址数据

在调用Google地图的geocode服务时,会得到一组json数据或者xml数据,但是并不适合直接显示在网页上,所以需要对返回的结果做进一步的解析。这里提供利用jquery解析json的方法。

返回的数据格式如下:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Kishim",
               "short_name" : "Kishim",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "巴达赫尚",
               "short_name" : "巴达赫尚",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "阿富汗",
               "short_name" : "AF",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Kishim, 阿富汗",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.0190016,
                  "lng" : 70.63659679999999
               },
               "southwest" : {
                  "lat" : 36.265314,
                  "lng" : 70.10925279999999
               }
            },
            "location" : {
               "lat" : 36.5712949,
               "lng" : 70.3360691
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.0190016,
                  "lng" : 70.63659679999999
               },
               "southwest" : {
                  "lat" : 36.265314,
                  "lng" : 70.10925279999999
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "巴达赫尚",
               "short_name" : "巴达赫尚",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "阿富汗",
               "short_name" : "AF",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "阿富汗巴达赫尚",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.490611,
                  "lng" : 74.89019499999999
               },
               "southwest" : {
                  "lat" : 35.44512100000001,
                  "lng" : 69.986097
               }
            },
            "location" : {
               "lat" : 36.7347725,
               "lng" : 70.81199529999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.490611,
                  "lng" : 74.89019499999999
               },
               "southwest" : {
                  "lat" : 35.44512100000001,
                  "lng" : 69.986097
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "阿富汗",
               "short_name" : "AF",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "阿富汗",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.49087670000001,
                  "lng" : 74.88986199999999
               },
               "southwest" : {
                  "lat" : 29.3772,
                  "lng" : 60.5170005
               }
            },
            "location" : {
               "lat" : 33.93911,
               "lng" : 67.709953
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.49087670000001,
                  "lng" : 74.88986199999999
               },
               "southwest" : {
                  "lat" : 29.3772,
                  "lng" : 60.5170005
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

可以看出,这个数据是一个多重嵌套的json。我们需要提取results下的formatted_address。解析方法如下:

$.ajax({
        url:'http://maps.google.com/maps/api/geocode/json?latlng=36.910093,70.403945&language=zh-CN&sensor=false,
        type:'get',
        dataType:'json',
        success:function(resp){
            if(resp['status']==='OK')
            {
                alert(resp.results[2].formatted_address);
            }
        }   
    });

PS:在使用这段代码之前,需要引入jquery库。

<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.2/jquery.min.js"></script>
IDL关于axis函数中coord_transform设置 google地图添加悬浮提示框(InfoWindow)js版本

作者:,GIS爱好者。
分享本文,请您带上本文链接
分享到:

发表评论