web-dev-qa-db-fra.com

Saisie semi-automatique de Google Maps SANS données d'atmosphère

J'utilise actuellement l'API Javascript de saisie automatique Google Maps Places pour permettre aux utilisateurs de sélectionner un emplacement en recherchant l'adresse ou le nom de l'établissement. Exemple de code:

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

Cela obtient les données suivantes.

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

   ]
}

Il y a beaucoup de données inutiles, je sais. Mais ces données supplémentaires me coûtent très cher, car les "Détails des lieux", les "Données d'atmosphère" et les "Données de contact" me coûtent quelques centimes PAR DEMANDE.

Dans ma console de facturation se trouve également une entrée nommée "Saisie semi-automatique sans détails sur les lieux - Par session". Cela signifie que je peux faire une demande sans toutes ces informations inutiles. Comment puis-je faire une telle demande au lieu d'une avec tous les détails de l'établissement?

8
Kento Nishi

Vous pouvez utiliser AutocompleteOptions objet afin de définir les champs que vous souhaitez voir dans les réponses détaillées. AutocompleteOptions a une propriété fields

champs - Champs à inclure pour la place dans la réponse des détails lorsque les détails sont récupérés avec succès. Pour une liste des champs, voir PlaceResult. Les champs imbriqués peuvent être spécifiés avec des chemins de points (par exemple, "geometry.location").

Vous pouvez donc modifier l'initialisation de la saisie semi-automatique au format suivant

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

J'espère que ça aide!

23
xomena

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

Une demande de détails sur les lieux génère des SKU de données (de base, contact et/ou atmosphère) - en fonction des champs spécifiés dans la demande. Si vous ne spécifiez pas de champs dans la demande Détails de lieu, toutes les références de données sont déclenchées (de base, contact et atmosphère). Pour éviter de payer pour des données dont vous n'avez pas besoin, appelez Autocomplete.setFields () pour spécifier uniquement les données de lieu que vous utiliserez.

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

Vous pouvez vérifier tous les champs ici

5
sto

Appelez simplement setField() après avoir instancié l'objet de saisie semi-automatique, par exemple:

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

Bien essayé Google. Après le changement de prix important, au lieu de déprécier les API et de rendre évident que les demandes coûteront une fortune, ils ont silencieusement permis au code actuel de tout demander, profitant ainsi de l'augmentation soudaine des frais.

Belle décision, mais elle se retourne déjà: supprimera tous les services Google de mes systèmes. OpenStreetMap et ArcGIS et Mapbox sont beaucoup plus abordables

1