Google API地址组件

时间:2014-11-29 15:42:45

标签: jquery google-maps

我正在尝试使用Google Maps API获取地址组件,但无法正确解析结果。我的代码如下:

    // Ajax Call
$.ajax({
    url: 'queryPage.php?',
    data: 'varObtainGoogleAddress=y&' +
          'varAPILink=' + encodeURIComponent(varAPILink),
    dataType: 'json',
    success: function(data) {   
        // Formatted Address
        varFormattedAddress = data.result['formatted_address'];  // Works!
    }
});

我想要的是城市,州和邮政编码项目。在这方面的任何帮助表示赞赏。我是一名自学成才的业余网站开发人员。 :)

3 个答案:

答案 0 :(得分:1)

我写了一个函数。

/**
*   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);
  }
});
...

答案 1 :(得分:0)

我假设您正在做的是reverse geocode

返回的对象看起来像这样(来自示例):

{
  "address_components": [
    {
      "long_name": "Grand St/Bedford Av",
      "short_name": "Grand St/Bedford Av",
      "types": [
        "bus_station",
        "transit_station",
        "establishment"
      ]
    },
    {
      "long_name": "Williamsburg",
      "short_name": "Williamsburg",
      "types": [
        "neighborhood",
        "political"
      ]
    },
    {
      "long_name": "Brooklyn",
      "short_name": "Brooklyn",
      "types": [
        "sublocality_level_1",
        "sublocality",
        "political"
      ]
    },
...

没有直接的方式来访问结果的街道,门牌号等,因为地址的每个部分可以同时是几种类型。您必须迭代address_components并根据需要进行过滤。

这是另一个涉及此主题的Stackoverflow主题:get city from geocoder results?

答案 2 :(得分:0)

有点晚,但这是我有

calle ===街道

nro ===街道编号

.directive('myCustomToggle', function () {
  return {
    restrict: 'E',
    replace: true,
    require: 'ngModel',
    transclude: true,
    scope: {
      ngModel: '=',
      ngModelOptions: '<?',
      ngTrueValue: '<?',
      ngFalseValue: '<?',
    },
    link: {
      pre: function preLink(scope, element, attrs, ctrl) {
        // defaults for optional attributes
        scope.ngTrueValue = attrs.ngTrueValue !== undefined
          ? scope.ngTrueValue
          : true;
        scope.ngFalseValue = attrs.ngFalseValue !== undefined
          ? scope.ngFalseValue
          : false;
        scope.ngModelOptions = attrs.ngModelOptions !== undefined
          ? scope.ngModelOptions
          : {};
      },
      post: function postLink(scope, element, attrs, ctrl) {
        ...
        function updateModel(disable) {
          // flip model value
          var newValue = disable
            ? scope.ngFalseValue
            : scope.ngTrueValue;
          // assign it to the view
          ctrl.$setViewValue(newValue);
          ctrl.$render();
        }
        ...
    },
    template: ...
  }
});