Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
- <script>
- function initialize() {
- var defaultBounds = new google.maps.LatLngBounds(
- new google.maps.LatLng(-33.8902, 151.1759),
- new google.maps.LatLng(-33.8474, 151.2631));
- var input = document.getElementById('location');
- var options = {
- bounds: defaultBounds,
- types: ['(cities)', '(regions)']
- };
- autocomplete = new google.maps.places.Autocomplete(input, options);
- google.maps.event.addListener(autocomplete, 'place_changed', function() {
- var place = autocomplete.getPlace();
- var city;
- var state;
- var country;
- var postcode;
- var latitude;
- var longitude;
- input.className = '';
- if (place && place.geometry){
- address_components = place.address_components;
- if (address_components){
- for (index in address_components){
- component = address_components[index];
- if (component.types.indexOf('locality') > -1){
- city = component.long_name;
- }
- if (component.types.indexOf('administrative_area_level_1') > -1){
- state = component.long_name;
- if (!state){
- if (component.types.indexOf('administrative_area_level_2') > -1){
- state = component.long_name;
- if (!state){
- if (component.types.indexOf('administrative_area_level_3') > -1){
- state = component.long_name;
- }
- }
- }
- }
- }
- if (component.types.indexOf('country') > -1){
- country = component.long_name;
- }
- if (component.types.indexOf('postal_code') > -1){
- postcode = component.long_name;
- }
- }
- }
- if (place.geometry.location){
- latitude = place.geometry.location.lat();
- longitude = place.geometry.location.lng();
- }
- }else{
- // Inform the user that the place was not found and return.
- input.className = 'notfound';
- return;
- }
- });
- }
- google.maps.event.addDomListener(window, 'load', initialize);
- </script>
- </head>
- <body>
- <input type="text" id="location">
- </body>
- </html>
Add Comment
Please, Sign In to add comment