<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
<script>
var city = document.getElementById("userdata_citystate");
console.log(city);
var autocomplete = new google.maps.places.Autocomplete(city);
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
var address = place.formatted_address;
var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();
var latlng = new google.maps.LatLng(latitude, longitude);
var geocoder = geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].formatted_address;
var state = results[0].address_components[results[0].address_components.length - 3].long_name;
var city = results[0].address_components[results[0].address_components.length - 4].long_name;
$("#userdata_citystate").val(city+", "+state);
}
}
});
console.log("You selected: '" + place.formatted_address + "'");
});
</script>
No comments:
Post a Comment