Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <title>Geolocation test</title>
- <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
- <script>
- function initialize() {
- var address = (document.getElementById('my-address'));
- var autocomplete = new google.maps.places.Autocomplete(address);
- autocomplete.setTypes(['geocode']);
- google.maps.event.addListener(autocomplete, 'place_changed', function () {
- var place = autocomplete.getPlace();
- if (!place.geometry) {
- return;
- }
- var address = '';
- if (place.address_components) {
- address = [
- (place.address_components[0] && place.address_components[0].short_name || ''),
- (place.address_components[1] && place.address_components[1].short_name || ''),
- (place.address_components[2] && place.address_components[2].short_name || '')
- ].join(' ');
- }
- });
- }
- function codeAddress() {
- geocoder = new google.maps.Geocoder();
- var address = document.getElementById("my-address").value;
- geocoder.geocode({ 'address': address }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- alert("Latitude: " + results[0].geometry.location.lat());
- alert("Longitude: " + results[0].geometry.location.lng());
- }
- else {
- alert("Geocode was not successful for the following reason: " + status);
- }
- });
- }
- google.maps.event.addDomListener(window, 'load', initialize);
- </script>
- </head>
- <body>
- <input type="text" id="my-address">
- <button id="getCords" onClick="codeAddress();">getLat&Long</button>
- <script>
- function initialize() {
- var address = (document.getElementById('my-address'));
- var autocomplete = new google.maps.places.Autocomplete(address);
- autocomplete.setTypes(['geocode']);
- google.maps.event.addListener(autocomplete, 'place_changed', function () {
- var place = autocomplete.getPlace();
- if (!place.geometry) {
- return;
- }
- var address = '';
- if (place.address_components) {
- address = [
- (place.address_components[0] && place.address_components[0].short_name || ''),
- (place.address_components[1] && place.address_components[1].short_name || ''),
- (place.address_components[2] && place.address_components[2].short_name || '')
- ].join(' ');
- }
- });
- }
- function codeAddress() {
- geocoder = new google.maps.Geocoder();
- var address = document.getElementById("my-address").value;
- geocoder.geocode({ 'address': address }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- alert("Latitude: " + results[0].geometry.location.lat());
- alert("Longitude: " + results[0].geometry.location.lng());
- }
- else {
- alert("Geocode was not successful for the following reason: " + status);
- }
- });
- }
- google.maps.event.addDomListener(window, 'load', initialize);
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment