简体   繁体   English

如何限制 Google Maps API 结果以防止返回对不存在的地址的估计

[英]How do I limit Google Maps API results to prevent returning estimate on addresses that do not exist

I am creating a button on a web page to return the latitude and longitude of an address entered into a form using google maps.我正在网页上创建一个按钮,以返回使用谷歌地图输入到表单中的地址的纬度和经度。 This works great when the address is valid but also returns results for invalid addresses.当地址有效但也会返回无效地址的结果时,这很有效。 I have tried using the geometry.location_type to limit to rooftop location types but this is not good enough since google returns "rooftop" for such queries as "1 b fl" or "1 atlanta ga" - clearly invalid addresses.我曾尝试使用 geometry.location_type 来限制屋顶位置类型,但这还不够好,因为谷歌为诸如“1 b fl”或“1 atlanta ga”之类的查询返回“屋顶”——显然是无效的地址。 In the case of "1 atlanta ga" it returns a formatted_address of "401 Moreland Ave NE, Atlanta, GA 30307, USA".在“1 atlanta ga”的情况下,它返回“401 Moreland Ave NE, Atlanta, GA 30307, USA”的格式化地址。 How do I make sure that it only returns accurate - or at least sort of accurate - results?我如何确保它只返回准确的——或者至少是准确的——结果? I am not getting error messages to show up in these cases.在这些情况下,我没有收到错误消息。

I tried using the location_type key but that seems to return "ROOFTOP" even in the above examples.我尝试使用 location_type 键,但即使在上面的示例中,它似乎也返回“ROOFTOP”。 I don't see any other keys that specify accuracy.我没有看到任何其他指定准确性的键。

function geocodeAddress(address) {
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == 'OK') {
            var locationType = results[0].geometry.location_type;
            if (results.length > 1) {
                $('#errorMessage').text('Too many results')
            } else {
                if (locationType != "ROOFTOP") {
                    $('#errorMessage').text('May not be accurate')
                }
             $('#Location_Latitude').val(results[0].geometry.location.lat());
             $('#Location_Longitude').val(results[0].geometry.location.lng());
            }
        } else {
            console.log('Unable to get coordinates at this time: ' + status);
        }
    });
}

I would like to see the error message, "May not be accurate" show up any time a close to exact match cannot be found.我希望看到错误消息“可能不准确”出现在任何无法找到接近完全匹配的情况下。 I say close to exact because I don't want the results to throw an error if the user doesn't type it in exactly as it is in the database and because I am only asking for address, city, and state so the country and zip code could be null.我说接近精确是因为如果用户没有完全按照数据库中的方式输入结果,我不希望结果抛出错误,并且因为我只要求地址、城市和州,所以国家和地区邮政编码可能为空。

This is the result I get back using the Georgia example above:这是我使用上面的 Georgia 示例返回的结果:

0:
address_components: (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
formatted_address: "401 Moreland Ave NE, Atlanta, GA 30307, USA"
geometry:
location: _.R {lat: ƒ, lng: ƒ}
location_type: "ROOFTOP"
viewport: _.Cd {na: Bd, ga: xd}
__proto__: Object
place_id: "ChIJJ72T1agG9YgR7f1FJRUZsEc"
plus_code: {compound_code: "QM82+3F Atlanta, Georgia, United States", global_code: "865QQM82+3F"}
types: (5) ["bar", "establishment", "food", "point_of_interest", "restaurant"]
__proto__: Object
length: 1
__proto__: Array(0)

Check the "partial match" value in the response:检查响应中的“部分匹配”值:

https://developers.google.com/maps/documentation/geocoding/intro#geocoding https://developers.google.com/maps/documentation/geocoding/intro#geocoding

  • partial_match indicates that the geocoder did not return an exact match for the original request, though it was able to match part of the requested address. partial_match 表示地理编码器没有返回与原始请求完全匹配的结果,尽管它能够匹配请求地址的一部分。 You may wish to examine the original request for misspellings and/or an incomplete address.您可能希望检查拼写错误和/或地址不完整的原始请求。

  • Partial matches most often occur for street addresses that do not exist within the locality you pass in the request.部分匹配最常发生在您在请求中传递的地区内不存在的街道地址。 Partial matches may also be returned when a request matches two or more locations in the same locality.当请求匹配同一地点的两个或多个位置时,也可能返回部分匹配。 For example, "21 Henr St, Bristol, UK" will return a partial match for both Henry Street and Henrietta Street.例如,“21 Henr St, Bristol, UK”将返回 Henry Street 和 Henrietta Street 的部分匹配项。 Note that if a request includes a misspelled address component, the geocoding service may suggest an alternative address.请注意,如果请求包含拼写错误的地址组件,地理编码服务可能会建议替代地址。 Suggestions triggered in this way will also be marked as a partial match.以这种方式触发的建议也将被标记为部分匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM