While implementing AutoComplete places using google maps api, when i try to enter any places a bunch of errors are generated at the chrome console window. One of them is Uncaught TypeError: b.get is not a function at Ew._.k.get (js?key=[KEY]&libraries=places&callback=initMap:106).
Code:
<!-- Google Maps API integration-->
<div class="gmaps">
<div id="map"></div>
<script>
function initMap() {
<!-- getting the user location -->
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
alert('geolocation not supported');
}
<!-- if successfully found the location -->
function success(position) {
var uluru = {lat:position.coords.latitude, lng: position.coords.longitude};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
var acOptions = {
types: ['establishment']
};
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'),acOptions);
autocomplete.bindTo('bounds',map);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
infoWindow.close();
var place = autocomplete.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
marker.setPosition(place.geometry.location);
infoWindow.setContent('<div><strong>' + place.name + '</strong><br>');
infoWindow.open(map, marker);
google.maps.event.addListener(marker,'click',function(e){
infoWindow.open(map, marker);
});
});
}
</script>
</div>
The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.
This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.
The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function.
In short, the "is not a function" error means that the value you're trying to invoke is not a function. The most common reasons this error occurs are: defining a variable with the same name that shadows the original object. calling a method on a value of a different type, e.g. calling the Array.
The problem is this line:
autocomplete.bindTo('bounds', map);
When that code first runs, the map
hasn't finished initializing and its bounds aren't valid. Put that code inside a bounds_changed
event listener:
google.maps.event.addListener(map,'bounds_changed', function() {
autocomplete.bindTo(map, 'bounds');
});
proof of concept fiddle
code snippet:
var infoWindow;
var map;
function initMap() {
<!-- getting the user location -->
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, errorFunc);
} else {
alert('geolocation not supported');
}
function errorFunc(posErr) {
console.log("Position Error code:" + posErr.code + " msg:" + posErr.message);
}
<!-- if successfully found the location -->
function success(position) {
infoWindow = new google.maps.InfoWindow();
var uluru = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
var acOptions = {
types: ['establishment']
};
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), acOptions);
google.maps.event.addListener(map, 'bounds_changed', function() {
autocomplete.bindTo(map, 'bounds');
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
infoWindow.close();
var place = autocomplete.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
marker.setPosition(place.geometry.location);
infoWindow.setContent('<div><strong>' + place.name + '</strong><br>');
infoWindow.open(map, marker);
google.maps.event.addListener(marker, 'click', function(e) {
infoWindow.open(map, marker);
});
});
}
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map,
.gmaps {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<input type="text" id="autocomplete" />
<!-- Google Maps API integration-->
<div class="gmaps">
<div id="map"></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With