简体   繁体   中英

Google maps API region parameter does not omit country name from formatted address parameter for text search query

I am trying to achieve google maps API text search query results for the US only. A user might enter ambiguous address queries that is the reason why I chose Text Search Requests API over other APIS provided by the google.

I am using the following URL https://maps.googleapis.com/maps/api/place/textsearch/json? sensor=false&query=thane&location=19.218331035236503,72.97808971256018&region=us&key=XXXXYYYSSSSSZZZZZ (key is wrong, for secrecy purpose)

I am aware that in spite of adding region parameter I may get results based on relevance from other countries. Lattitude and Longitude present in the query are also not from the US.

But as per the docs provided by the Google when you use region parameter:

When this parameter is used, the country name is omitted from the resulting formatted_address for results in the specified region

The response I get does not omit the country name,

part of the Response as follows :

"results" : [
  {
     "formatted_address" : "Maharashtra, India",
     "geometry" : {
        "location" : {
           "lat" : 19.2183307,
           "lng" : 72.9780897
        },
        "viewport" : {
           "northeast" : {
              "lat" : 19.296441,
              "lng" : 73.07666159999999
           },
           "southwest" : {
              "lat" : 19.1313429,
              "lng" : 72.926259
           }
        }
     }]

I also tried using components parameter in place of the region parameter but I guess it will work only with autocomplete API.

Thank you in advance.

  1. You pasted the sentence yourself

the country name is omitted for results in the specified region

In your example query you specify a location in India and a region "us" (which makes no sense) so in this case the country name is not omitted, which is implicitly the expected behavior.

If you do the same request, only specifying the region ( in for India) then the formatted address doesn't contain the country:

https://maps.googleapis.com/maps/api/place/textsearch/json?%20sensor=false&query=thane&region=in

{
  "html_attributions": [],
  "results": [{
    "formatted_address": "Maharashtra",
    "geometry": {
      "location": {
        "lat": 19.2183307,
        "lng": 72.9780897
      },
      "viewport": {
        "northeast": {
          "lat": 19.296441,
          "lng": 73.07666159999999
        },
        "southwest": {
          "lat": 19.1313429,
          "lng": 72.926259
        }
      }
    },
    "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
    "id": "5a575922f6db500ba22b52c62751717dd2548e0b",
    "name": "Thane",
    "photos": [{
      "height": 576,
      "html_attributions": [
        "\u003ca href=\"https://maps.google.com/maps/contrib/105961144843321217972/photos\"\u003eA Google User\u003c/a\u003e"
      ],
      "photo_reference": "CmRaAAAAXwGLh6JweZeWjWdKDFbKvdUP7rR5XWwNeZ4vImjPagRztcOVoTsQQdkjnstw3NkjWPBQja3rFVo6o14-KspWomJi-8wCP5ViiNAJgkCYllSc1LUF_UsoxzWM5pTQzGI_EhAjL3hmEjPCFQPkvmu4nTkOGhTaSXKV4u3aasoPi-QILWPfKI6LnQ",
      "width": 786
    }],
    "place_id": "ChIJWf12_vy45zsRgwLF94V9Ns8",
    "reference": "CmRbAAAAJCtYPdY98PuNeZt1quzUSGMYGZIJAUAtt17HlcZzOAU0tAsXttmjJjn2dUWjg57wm3d3N_6vU57uZMjXzokFf4HIIDB6DpWX9INXYc-GSfcneQQYoJottDmrzdCnGCaYEhA-R7L3cmzO0HQw6bHuzCUPGhQLfvZ-YgwMIpG-D3V2UYirW71iCw",
    "types": ["locality", "political"]
  }],
  "status": "OK"
}
  1. Documentation also mentions that

If you specify a location parameter, you must also specify a radius parameter.

Now if your goal is to biaise results based on a region then I don't think it makes sense to also use a location and a radius.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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