Reputation: 431
I am using google map java script autocomplete on my site
Here is my java script code for rendering google auto autocomplete
<script src="https://maps.googleapis.com/maps/api/js?key={{GOOGLE_MAP_API_KEY}}&libraries=places" type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
var options = {
types: ['(cities)'],
componentRestrictions: {country: "us"}
};
var input = document.getElementById('city');
var autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.setFields(['address_components', 'geometry', 'icon', 'name']);
}
initialize();
</script>
and html code is
<input type="text" class="form-control" id="city" name="city" required="" value="" placeholder="City" autocomplete="off">
In billing section , I am getting charged for
1.Places API Autocomplete without Places Details
2.Places API Places Details
I never used place details request anywhere in my site .How this request is fired ?.
Is it calling automatically after autocomplete request? How can i prevent it?
My site uses geolocation api to convert address into lat and long coordinates. Will it use place details api ?
How can I call google map autocomplete without firing place details request
Upvotes: 3
Views: 3678
Reputation: 11
Same problem. I try
autocomplete = new google.maps.places.Autocomplete(
document.getElementById('Details_Location'), { fields: ["place_id"] });
For getting geometry.location.lat() and geometry.location.lng(), you can use separate google.maps.Geocoder(), with string from autocomplete. google.maps.Geocoder is cheaper than Places Details.
Code something like:
autocomplete.addListener("place_changed", setMapCenter);
geocoder = new google.maps.Geocoder();
function setMapCenter() {
data = {
address: autocomplete_input_value
};
geocoder.geocode( data, function(results, status) {
if (status == 'OK') {
// var map = new google.maps.Map();
map.setCenter(results[0].geometry.location);
} else {
...
}
});
}
Upvotes: 1
Reputation: 81
placeIdOnly is deprecated as of January 15, 2019, and fields can be set in the Autocomplete constructor, this code worked for me and stopped the autocomplete from calling the places details request:
autocomplete = new google.maps.places.Autocomplete(
document.getElementById('Details_Location'), { fields: ["place_id"] });
Upvotes: 6
Reputation: 431
Fixed the issue. A PlaceService.GetPlaceDetails is called automatically every time you select or click an Autocomplete Suggestion even if you have no autocomplete.getPlace() function in your code. Additionally, using the Autocomplete Widget, when the user picks a prediction, the widget will call Place Details for you, so that a full Place result will be available when you call autocomplete.getPlace() upon the place_changed event. If you don't want Place Details to be called, use {placeIdOnly: true} when initializing the widget.
Upvotes: 6