Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Autocomplete WITHOUT Atmosphere Data

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?

like image 435
Kento Nishi Avatar asked Aug 15 '18 19:08

Kento Nishi


People also ask

Can I use Google Places API without billing account?

Reminder: To use the Places API, you must enable billing on each of your projects and include an API key with all API or SDK requests. The Places API uses a pay-as-you-go pricing model.

Can Google Maps work without API key?

Google Maps API is Now Required. As of June 11, 2018, an API key is now required for Google Maps.

How do I hide the features on Google Maps?

To turn on Incognito mode in Google Maps on a smartphone or tablet, tap your profile picture on the top right and select Turn on Incognito mode.


2 Answers

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!

like image 95
xomena Avatar answered Oct 14 '22 07:10

xomena


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

like image 34
sto Avatar answered Oct 14 '22 09:10

sto