首页 > 解决方案 > 没有大气数据的谷歌地图自动完成

问题描述

我目前正在使用 Google Maps Places Autocomplete Javascript API 让用户通过搜索地址或机构名称来选择位置。示例代码:

autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
    console.log(autocomplete.getPlace());
});

这将获得以下数据。

{  
   "address_components":[  
      {  
         "long_name":"1600",
         "short_name":"1600",
         "types":[  
            "street_number"
         ]
      },
      {  
         "long_name":"Amphitheatre Parkway",
         "short_name":"Amphitheatre Pkwy",
         "types":[  
            "route"
         ]
      },
      {  
         "long_name":"Mountain View",
         "short_name":"Mountain View",
         "types":[  
            "locality",
            "political"
         ]
      },
      {  
         "long_name":"Santa Clara County",
         "short_name":"Santa Clara County",
         "types":[  
            "administrative_area_level_2",
            "political"
         ]
      },
      {  
         "long_name":"California",
         "short_name":"CA",
         "types":[  
            "administrative_area_level_1",
            "political"
         ]
      },
      {  
         "long_name":"United States",
         "short_name":"US",
         "types":[  
            "country",
            "political"
         ]
      },
      {  
         "long_name":"94043",
         "short_name":"94043",
         "types":[  
            "postal_code"
         ]
      }
   ],
   "adr_address":"<span class=\"street-address\">1600 Amphitheatre Pkwy</span>, <span class=\"locality\">Mountain View</span>, <span class=\"region\">CA</span> <span class=\"postal-code\">94043</span>, <span class=\"country-name\">USA</span>",
   "formatted_address":"1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
   "formatted_phone_number":"(650) 253-0000",
   "geometry":{  
      "location":{  
         "lat":37.4219999,
         "lng":-122.08405749999997
      },
      "viewport":{  
         "south":37.4206993697085,
         "west":-122.0847981802915,
         "north":37.4233973302915,
         "east":-122.08210021970848
      }
   },
   "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
   "id":"3a936e96ddcb18b4fa8a2974ebc8876c3108fef2",
   "international_phone_number":"+1 650-253-0000",
   "name":"Googleplex",
   "photos":[  
      {  
         "height":2992,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/103819965142360016765/photos\">Donald Polak</a>"
         ],
         "width":4000
      },
      {  
         "height":1080,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/113986172004889046503/photos\">Gil Carlos</a>"
         ],
         "width":1620
      },
      {  
         "height":810,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/112693353017475869362/photos\">spoorthi Shres</a>"
         ],
         "width":1080
      },
      {  
         "height":2448,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/112322727489597014623/photos\">Inmook Lee</a>"
         ],
         "width":3264
      },
      {  
         "height":480,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/100130762972482716067/photos\">JL N</a>"
         ],
         "width":640
      },
      {  
         "height":2988,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/112927044171481932986/photos\">Chamnan Muon</a>"
         ],
         "width":5312
      },
      {  
         "height":2448,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/112947157334846208308/photos\">Aslam Mohammed</a>"
         ],
         "width":3264
      },
      {  
         "height":3024,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/111560452835833490678/photos\">Emprendedor ypunto</a>"
         ],
         "width":4032
      },
      {  
         "height":2268,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/112821956543329496349/photos\">Remco Buddelmeijer</a>"
         ],
         "width":4032
      },
      {  
         "height":3024,
         "html_attributions":[  
            "<a href=\"https://maps.google.com/maps/contrib/112778094015197614835/photos\">Thais Ferreira Curi</a>"
         ],
         "width":4032
      }
   ],
   "place_id":"ChIJj61dQgK6j4AR4GeTYWZsKWw",
   "plus_code":{  
      "compound_code":"CWC8+Q9 Mountain View, California, United States",
      "global_code":"849VCWC8+Q9"
   },
   "rating":4.4,
   "reference":"CmRRAAAACSrJEHCvJp1k1sNsnP1YvwaVcHxCPCdgt9vF-dLxsyUO-VoVoJ44QUHBeay_xRA29z7r9M_Pl-GCRFke9AbTjR7uOQg6ujPCc2gI3yaVlTVIBOAfxlamFaYbwzInWZDYEhA6V8crd3XJ8w-LHYNjzm2RGhSjYXAaGOf9ewF1emkXjxFJu-tO7g",
   "reviews":[  
      {  
         "author_name":"Grand Palais Beachside",
         "author_url":"https://www.google.com/maps/contrib/117946213840380407904/reviews",
         "language":"en",
         "profile_photo_url":"https://lh3.googleusercontent.com/-f_xOeqEuQlY/AAAAAAAAAAI/AAAAAAAAAAA/AAnnY7oSEuVFn-pDYQWJEJTa0Ol6FC6oLw/s128-c0x00000000-cc-rp-mo/photo.jpg",
         "rating":5,
         "relative_time_description":"in the last week",
         "text":"How would you rate your experience with The Marble Man?  \nThe experience was excellent, totally professional, provided great advice on work timings and even when running a little late had the professionalism to advise us and that was very much appreciated.\nDo you have any suggestions on how we can improve in any areas?\nNot be so busy that we had to wait a month (approx.) but no in all honesty great service very happy but in running a resort sometimes these in the future need to be acted on asap as they are unable to be let depending on the repair required.\nHow was the completed job?\nExcellent\nHow did you find our job and inspection technicians and office team?\nExcellent\nWe would 100% recommend the team at Marble Man to all.",
         "time":1534208819
      },
      {  
         "author_name":"BRYAN HALLIGAN",
         "author_url":"https://www.google.com/maps/contrib/105850575620900701772/reviews",
         "language":"en",
         "profile_photo_url":"https://lh5.googleusercontent.com/-qlpyjtK9t8E/AAAAAAAAAAI/AAAAAAAAAAc/euGPm7V8gRA/s128-c0x00000000-cc-rp-mo/photo.jpg",
         "rating":1,
         "relative_time_description":"in the last week",
         "text":"My maps have not been giving me credit. It's not advancing my level. Receive notifications on high number of reviews. Level achievement.  The number on maps will be 95 percent lower. The badge's as well.  Than it increases a little. Out of nowhere resets.  Can you help. I'm not getting paid for this. At least give me respect.  \n\nThanks for being understanding and helpful with my account issue.\nPlease help me.",
         "time":1533763726
      },
      {  
         "author_name":"TAYYAB M",
         "author_url":"https://www.google.com/maps/contrib/118284785502689721341/reviews",
         "language":"en",
         "profile_photo_url":"https://lh4.googleusercontent.com/-Dx68fj0vjx8/AAAAAAAAAAI/AAAAAAAACHM/3a9bPJis8kc/s128-c0x00000000-cc-rp-mo-ba4/photo.jpg",
         "rating":5,
         "relative_time_description":"in the last week",
         "text":"The greatest revolution in the field of science and information technology to deliver the best IT services to the mankind across the globe is the Google! I congratulate and convey my best wishes to Google and Team Google for the outstanding contribution with the innovations, renovations, help and support in the age of digital media and E-communication to the people of the world! The Google bring the different Nations, races and communities on the one Goblal or international platform with harmony so that they keep in touch with each other for their welfare or human welfare as well as for the welfare of all living beings on the earth so let's go with the Google because ... the Google for the globe...!",
         "time":1534327927
      },
      {  
         "author_name":"Duane Cope",
         "author_url":"https://www.google.com/maps/contrib/102398970160192267535/reviews",
         "language":"en",
         "profile_photo_url":"https://lh4.googleusercontent.com/-bQ8vav4bzQ0/AAAAAAAAAAI/AAAAAAAAAZs/PiW9NlnXgtM/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
         "rating":5,
         "relative_time_description":"a week ago",
         "text":"Only here can you find priceless experiences on a daily basis. Just when you think everyday life is dull lack luster or monotonous. Get to know Google, and think again. IMHO. A formidable partner or adversary regardless of the end result. My REAL lasting hopes are to give back more then just a hard time and follow their example of patient goodness toward human kind.",
         "time":1533536068
      },
      {  
         "author_name":"DJ H",
         "author_url":"https://www.google.com/maps/contrib/111411591110116941001/reviews",
         "language":"en",
         "profile_photo_url":"https://lh6.googleusercontent.com/-WMc4UVUfOW4/AAAAAAAAAAI/AAAAAAAAJGE/GOwV8JZOBZc/s128-c0x00000000-cc-rp-mo-ba6/photo.jpg",
         "rating":5,
         "relative_time_description":"2 weeks ago",
         "text":"Google is one of my favorite companies hands down. No other company has given so much control and free information at their fingertips. On a morning walk around my neighborhood, I saw a Google Street View vehicle. Therefore I decided to take a picture of it before it had the chance to take a picture of me. Maybe I will check Google maps to see if we took a picture of each other. That would be interesting.",
         "time":1533077557
      }
   ],
   "scope":"GOOGLE",
   "types":[  
      "point_of_interest",
      "establishment"
   ],
   "url":"https://maps.google.com/?cid=7793879817120868320",
   "utc_offset":-420,
   "vicinity":"1600 Amphitheatre Parkway, Mountain View",
   "website":"http://www.google.com/",
   "html_attributions":[  

   ]
}

我知道有很多不必要的数据。但是这些额外的数据让我付出了很多,因为“地点详细信息”、“大气数据”和“联系数据”每个请求都花费了我几美分。

在我的计费控制台中,还有一个名为“没有地点详细信息的自动完成 - 每个会话”的条目。这意味着我可以在没有所有这些不必要信息的情况下提出请求。我怎样才能提出这样的请求,而不是一个包含所有机构详细信息的请求?

标签: javascriptjsongoogle-mapsgoogle-maps-api-3google-places-api

解决方案


您可以使用AutocompleteOptions对象来定义您希望在详细响应中看到的字段。AutocompleteOptions 有一个fields属性

fields - 成功检索详细信息后,详细信息响应中要包含的 Place 字段。有关字段列表,请参阅 PlaceResult。可以使用点路径指定嵌套字段(例如,“geometry.location”)。

因此,您可以将自动完成初始化更改为以下形式

autocomplete = new google.maps.places.Autocomplete(input, {
    fields: ["name", "geometry.location", "place_id", "formatted_address"]
});

我希望这有帮助!


推荐阅读