Monday, 18 July 2016

Place search using Google API

Live Example Here
<!--header-->
<html>
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', function () {
var places = new google.maps.places.Autocomplete(document.getElementById('txtPlaces'));
google.maps.event.addListener(places, 'place_changed', function () {
var place = places.getPlace();
var address = place.formatted_address;
var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();
//var city = place.geometry.location.city();
//alert(address);
//for testing purpose alert
var mesg = "Address: " + address;
mesg += "\nLatitude: " + latitude;
mesg += "\nLongitude: " + longitude;
// alert(mesg);
//$("#address").val(address);
//$("#latitude").val(latitude);
//$("#longitude").val(longitude);
});
});
</script>
</head>

<body>
<textarea id="txtPlaces" name="txtPlaces" placeholder="Enter place to search" class="f-location" required></textarea>
</body>
</html> 

No comments:

Post a Comment