【问题标题】:Parsing Google Maps JSON data for Geocoding in JQ (Not JQuery)在 JQ 中解析 Google Maps JSON 数据以进行地理编码(不是 JQuery)
【发布时间】:2014-11-24 11:07:00
【问题描述】:

我正在尝试使用 JQ 从 Lat 和 Long 值中获取国家和城市名称。

这是完整的 JSON 示例 https://maps.googleapis.com/maps/api/geocode/json?latlng=55.397563,10.39870099999996&sensor=false

我将返回的 JSON 粘贴到 jqplay

尝试选择国家和城市名称,但我得到的最接近的是

.results[0].address_components[].short_name

如何指定只带"types" : [ "country", "political" ] 所在的节点?

谢谢

【问题讨论】:

    标签: json google-maps jq


    【解决方案1】:

    我不清楚您到底在寻找什么。每个结果都有一组类型,每个地址组件也有一组类型。你想要哪一个?我们可以编写一个过滤器来匹配您尝试的内容,但考虑到数据,它对您来说完全没用。包含您列出的类型的唯一项目只是一个国家名称。

    无论如何,假设您想要获得具有"country""political" 类型的结果对象,请使用contains() 过滤器。

    .results | map(
        select(
            .types | contains(["country","political"])
        )
    )
    

    否则,您需要明确您想要从该数据集中得到什么。预期结果的示例...

    【讨论】:

    • 谢天谢地,有人已经提出并回答了这个问题,否则我会放弃使用 exif GPS json 数据和这些复杂规则来组织我的照片。
    • 这还能用吗?对我来说它只是返回 []
    【解决方案2】:

    我为此编写了一个函数。

    /**
    *   geocodeResponse is an object full of address data.  
    *   This function will "fish" for the right value
    *   
    *   example: type = 'postal_code' => 
    *   geocodeResponse.address_components[5].types[1] = 'postal_code'
    *   geocodeResponse.address_components[5].long_name = '1000'
    * 
    *   type = 'route' => 
    *   geocodeResponse.address_components[1].types[1] = 'route'
    *   geocodeResponse.address_components[1].long_name = 'Wetstraat'
    */
    function addresComponent(type, geocodeResponse, shortName) {
      for(var i=0; i < geocodeResponse.address_components.length; i++) {
        for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
          if (geocodeResponse.address_components[i].types[j] == type) {
            if (shortName) {
              return geocodeResponse.address_components[i].short_name;
            }
            else {
              return geocodeResponse.address_components[i].long_name;
            }
          }
        }
      }
      return '';
    }
    

    使用方法;一个例子:

    ...
    myGeocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK && results[1]) {
        var country     = addresComponent('country', results[1], true);
        var postal_code = addresComponent('postal_code', results[1], true);
        ...
      }
    });
    ...
    

    我在这里用过:saving marker data into db

    【讨论】:

      【解决方案3】:

      将 json 分配给 results 变量,var results = {your json}。 然后试试这个:

      for( var idx in results.results)
      {
          var address = results.results[idx].address_components;
          for(var elmIdx in address)
          {
              if(address[elmIdx].types.indexOf("country") > -1 &&
                 address[elmIdx].types.indexOf("political") > -1)
              {
                 address[elmIdx].short_name //this is the country name
                 address[elmIdx].long_name  //this is the country name
      
              }
          }
      }    
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多