web-dev-qa-db-ja.com

大気データなしのGoogleマップのオートコンプリート

現在、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":[  

   ]
}

不要なデータがたくさんあります。ただし、「場所の詳細」、「大気データ」、および「連絡先データ」には、リクエストごとに数セントかかるため、この余分なデータにはかなりの費用がかかります。

私の課金コンソールには、「場所の詳細なしのオートコンプリート-セッションごと」という名前のエントリもあります。これは、この不要な情報をすべて使わずにリクエストを行うことができることを意味します。施設の詳細がすべて記載されたリクエストではなく、どのようにしてリクエストを行うことができますか?

8
Kento Nishi

AutocompleteOptions オブジェクトを使用して、詳細応答で表示するフィールドを定義できます。 AutocompleteOptionsにはfieldsプロパティがあります

fields-詳細が正常に取得されたときに、詳細応答のプレイスに含まれるフィールド。フィールドのリストについては、PlaceResultを参照してください。ネストされたフィールドは、ドットパスで指定できます(たとえば、「geometry.location」)。

そのため、オートコンプリートの初期化を次の形式に変更できます

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

これがお役に立てば幸いです!

23
xomena

https://developers.google.com/places/web-service/usage-and-billing

プレイス詳細リクエストは、リクエストで指定されたフィールドに応じて、データSKU(Basic、Contact、Atmosphere)を生成します。プレイス詳細リクエストでフィールドを指定しない場合、すべてのデータSKUがトリガーされます(Basic、Contact、およびAtmosphere)。不要なデータの支払いを回避するには、Autocomplete.setFields()を呼び出して、使用する場所データのみを指定します。

var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.setFields(['address_components', 'formatted_address', 'geometry', 'icon', 'name']);

すべてのフィールドを確認できます here

5
sto

オートコンプリートオブジェクトをインスタンス化した後、setField()を呼び出すだけです。次に例を示します。

autocomplete.setFields(['address_components', 'formatted_address', 'geometry', 'icon', 'name']);

Googleをお試しください。大幅な価格変更が行われた後、APIを非推奨にし、要求に莫大な費用がかかることを明らかにする代わりに、現在のコードがすべてを要求することを黙って許可したため、急激な課金の増加から利益を得ました。

良い動きですが、すでに逆火です:私のシステムからすべてのGoogleサービスを削除します。 OpenStreetMapとArcGISおよびMapboxははるかに手頃な価格です

1