Google Maps Autocomplete WITHOUT Atmosphere Data
Asked Answered
D

4

16

I am currently using the Google Maps Places Autocomplete Javascript API to let users select a location by searching for the address or establishment name. Example code:

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

This gets the following data.

{  
   "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":[  

   ]
}

There is a lot of unnecessary data, I know. But this extra data is costing me a lot, as "Places Details", "Atmosphere Data" and "Contact Data" cost me a few cents PER REQUEST.

In my billing console is also an entry named "Autocomplete without Places Details - Per Session". This means that I can make a request without all of this unnecessary info. How can I make such a request instead of one with all of the establishment details?

Dees answered 15/8, 2018 at 19:42 Comment(0)
A
36

You can use AutocompleteOptions object in order to define fields that you want to see in details responses. AutocompleteOptions has a fields property

fields - Fields to be included for the Place in the details response when the details are successfully retrieved. For a list of fields see PlaceResult. Nested fields can be specified with dot-paths (for example, "geometry.location").

So, you can change autocomplete initialization to the following form

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

I hope this helps!

Aurelia answered 15/8, 2018 at 23:41 Comment(6)
Thank you so much, I wasn't aware of the fields parameter! – Dees
Thanks for thisβ€”very helpful in helping to avoid the ridiculous fee's Gmaps are now charging. – Wimmer
The prices are up by 1400% – Dees
It's absurd that you have to opt OUT of these charges for data you don't use. – Behka
I still can't believe it, but from a business standpoint every person's mistake is an extra dollar made... sad. – Dees
Google are thieves increasing their prices so much and simultaneously reducing the free tier from 25000 per day to 28000 per month. Outrageous. Will be eliminating all Google Cloud services from our company. – Peccable
W
7

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

A Places Details request generates Data SKUs (Basic, Contact, and/or Atmosphere) – depending on the fields that are specified in the request. If you do not specify fields in the Place Details request, all Data SKUs are triggered (Basic, Contact, and Atmosphere). To avoid paying for data that you don't need, call Autocomplete.setFields() to specify only the place data that you will use.

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

You can check all the fields here

Whimsicality answered 19/1, 2019 at 4:45 Comment(0)
N
3

Just call setField() after instantiating the autocomplete object, for example:

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

Nice try Google. After the hefty price change, instead of deprecating the APIs and make it obvious the requests will cost a fortune, they silently allowed the current code to request everything, thus profiting from the sudden charge increase.

Nice move but it's already backfiring: Will remove all the Google services from my systems. OpenStreetMap and ArcGIS and Mapbox are much more affordable

Nones answered 2/3, 2019 at 13:37 Comment(1)
I'm thinking about doing the same, now that I'm rewriting it with ReactJS and Azure anyways! – Dees
N
0

If you're calling the API with HTTP requests (instead of using the Javascript package), you can add &fields= to the URL params. If you're using both the Autocomplete and Place Details APIs, make sure to specify &fields= on both of them. I.e.

Old:

autocomplete - https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${input}&types=geocode&components=country:us&key=${key}

place details - https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&key=${key}

New:

autocomplete - https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${input}&types=geocode&components=country:us&key=${key}&fields=formatted_address

place details - https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&key=${key}&fields=address_components

Nancinancie answered 8/5, 2023 at 16:37 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.