获取标记的精确地址 - Google 地图 V3

发布于 2024-11-09 21:15:50 字数 1253 浏览 5 评论 0原文

我正在使用 Google 地图 V3 API。每当用户在街道上(或者距离街道几米/码远)放置图钉时,它应该获取我用来从放置的标记中检索的地址组件。

令我困扰的是,当我在街道上放置图钉时,有时它会返回门牌号/名称,而它应该始终返回列表:

  • 街道名称;
  • 城市;
  • 状态;
  • 县;
  • 然后国家/地区

我通过定制的地址组件返回从 Google Maps API 生成的整个 JSON 响应:

getAddress(pinLocation, function(addressobj)
{
    for(i = 0; i < addressobj[0].address_components.length; i++)
    {
        var addressComp = addressobj[0].address_components[i].long_name;
        $('input[type=text][address=comp]:eq(' + i + ')').val(addressComp);
    }
});

因此,当返回数据时,它会返回结果,每个地址组件(来自上面的列表)都会进入一个输入字段。这是返回的预期结果:

  • San Antonio Valley Rd (街道名称)
  • Livermore (城市)
  • Diablo Range (州)
  • Santa Clara < code>(县)
  • 加利福尼亚州 (国家/地区)

这是完美的响应,但从某些位置落在街道上(主要是拥挤的街道)时,我得到的结果是:

  • 2920 (应该是达拉纳Ct)
  • Dalarna Ct (应该是 Turlock)
  • Turlock (这可以,但被省略)
  • Turlock (应该是 Stanislaus)
  • Stanislaus (应该是加利福尼亚州)

我不知道如何制作一个不显示门牌号的万无一失的地址组件,并且应该始终返回有关列表的信息(第一个)因为数据当我需要它产生与列表相同的结果时,在街道上放置标记时总是会发生变化。

I'm using Google Maps V3 API. Whenever a user drops a pin on a street (or perhaps a few metres/yards away from a street) it should get the address components which I use to retrieve from the dropped marker.

What is bothering me, is that when I drop a pin on a street, sometimes it returns the house number/name when it should always return the list as:

  • Street name;
  • City;
  • State;
  • County;
  • then Country

I go through the address components via a custom made that returns the entire JSON response generated from the Google Maps API:

getAddress(pinLocation, function(addressobj)
{
    for(i = 0; i < addressobj[0].address_components.length; i++)
    {
        var addressComp = addressobj[0].address_components[i].long_name;
        $('input[type=text][address=comp]:eq(' + i + ')').val(addressComp);
    }
});

So when the data is returned it returns results and each address component (from the list above) each goes into a input field. This is what kind of expected results returns:

  • San Antonio Valley Rd (street name)
  • Livermore (city)
  • Diablo Range (state)
  • Santa Clara (county)
  • California (country)

This is the perfect response but from some locations when dropping on a street (mostly crowded streets) I get like:

  • 2920 (should be Dalarna Ct)
  • Dalarna Ct (should be Turlock)
  • Turlock (this is okay, but is omitted)
  • Turlock (should be Stanislaus)
  • Stanislaus (should be California)

I have no idea how I can make a foolproof address component that does not display the house number, and should always return the information regarding the list (first one) because the data always varies when dropping markers on a street when I need it to produce the same results as the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2024-11-16 21:15:50

将此用于您的 getAddress 函数:

    geocode2FormFields = {'route':0,
                          'locality':1,
                          'sublocality':1,
                          'administrative_area_level_1':2,
                          'administrative_area_level_2':3,
                          'country':4};

    for(i = 0; i < addressobj[0].address_components.length; i++) { 
        for(j = 0; j < addressobj[0].address_components[i].types.length; j++) {
            formFieldIndex = geocode2FormFields[addressobj[0].address_components[i].types[j]];
            if (typeof formFieldIndex !== 'undefined') {
                var addressComp = addressobj[0].address_components[i].long_name;
                $('input[type=text][address=comp]:eq(' + formFieldIndex + ')').val(addressComp);
            }
        }
    }

正如 Google 的文档所述,“反向地理编码不是一门精确的科学。”然而,我相信这应该为美国大多数地方提供合理的结果。您的字段名称(例如“州”)似乎假设美国位置,因此我猜测这将满足您的需求,或者至少比您现在所拥有的更接近理想。

如果您发现想要或需要调整 geocode2FormFields 内容,各种类型都记录在 http://code.google.com/apis/maps/documentation/javascript/services.html

Use this for your getAddress function:

    geocode2FormFields = {'route':0,
                          'locality':1,
                          'sublocality':1,
                          'administrative_area_level_1':2,
                          'administrative_area_level_2':3,
                          'country':4};

    for(i = 0; i < addressobj[0].address_components.length; i++) { 
        for(j = 0; j < addressobj[0].address_components[i].types.length; j++) {
            formFieldIndex = geocode2FormFields[addressobj[0].address_components[i].types[j]];
            if (typeof formFieldIndex !== 'undefined') {
                var addressComp = addressobj[0].address_components[i].long_name;
                $('input[type=text][address=comp]:eq(' + formFieldIndex + ')').val(addressComp);
            }
        }
    }

As Google's documentation says, "Reverse geocoding is not an exact science." However, I believe this should provide reasonable results for most places in the United States. Your field names (e.g., "state") seem to assume a United States location, so I'm guessing that will meet for your needs, or at least be closer to ideal than what you have now.

If you ever find you want or need to tweak the geocode2FormFields stuff, the various types are documented under "Address Component Types" at http://code.google.com/apis/maps/documentation/javascript/services.html.

囚你心 2024-11-16 21:15:50

这些地址组件中的每一个,除了具有“long_name”属性之外,还有一个“types”属性,它是一个类型数组。以下是一些类型:
街道编号
路线(街道名称)
邮政编码
还有更多。

查看 http://code.google.com/apis/maps/文档/地点/#PlaceDetails

Each of those address_components, in addition to having "long_name" property, has a "types" property which is an array of, well, types. Here are some of the types:
street_number
route (street name)
postal_code
and many more.

Take a look at http://code.google.com/apis/maps/documentation/places/#PlaceDetails

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文