Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false&amp;language=en" type="text/javascript"></script>

<h1>Google maps API v.3</h1>

<h2>Geocoding</h2>
<p><em>Reference: https://developers.google.com/maps/documentation/javascript/geocoding</em></p>
<p>
    <label for="addr"><strong>Address</strong></label><br>
    <input id="addr" type="text" value="Castel Sant'Angelo, Rome, Italy" size="80">
    <button id="creaMappa" type="button">Build map</button>
</p>
<p>Drag the cursor to change coordinates</p>
<p><strong>Coords</strong>: <span id="lat">--</span>, <span id="lng">--</span></p>
<div class="map" id="map1"></div>

<h2>Reverse Geocoding</h2>
<p>Retrieve coordinates first, then trigger reverse geocoding:</p>
<p><button id="reverseGeocoding" type="button">Reverse Geocoding</button></p>
<p>Reverse Geocoding result formatted_address: <span id="reverseGeocodingResult_formatted_address">--</span></p>
<p>Reverse Geocoding result address components:</p>
<blockquote>
    <span class=""><span id="reverseGeocodingResult_address_components">--</span></span>
</blockquote>
<p>NB: change the <code>language</code> url parameter to reverse geocode in other languages</p>

<p class="p">Demo by Massimo Cassandro. <a href="http://www.sitepoint.com/google-maps-recipes">See article</a>.</p>
              
            
!

CSS

              
                body {
  text-align: center;
}

iframe {
  display: block;
  margin: auto;
}

.map {
  border: 1px solid #ccc;
  background-color: #efefef;
  height: 300px;
}
              
            
!

JS

              
                $(document).ready(function() {
	var geocoder;
	var map;
	
	$('#creaMappa').click(function () {
		var address=$('#addr').val();
		
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				
				console.log('geocoder results:');
				console.dir(results);
				
				var mapOptions = {
					zoom: 16,
					mapTypeControl: true,
					mapTypeControlOptions: {
					  style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
					},
					zoomControl: true,
					zoomControlOptions: {
					  style: google.maps.ZoomControlStyle.SMALL
					},
					//streetViewControl: false,
					center: results[0].geometry.location
				}
				
				map = new google.maps.Map(document.getElementById('map1'), mapOptions);
				
				$('#lat').text(results[0].geometry.location.lat());
				$('#lng').text(results[0].geometry.location.lng());
				
				//map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location,
					draggable: true,
					animation: google.maps.Animation.DROP
				});
				
				google.maps.event.addListener(marker, 'dragend', function() {
					
					marker.setAnimation(google.maps.Animation.DROP);
					
					var marker_pos=marker.getPosition();
					
					console.log('Marker getPosition():');
					console.dir(marker_pos);
					
					$('#lat').text(marker_pos.lat());
					$('#lng').text(marker_pos.lng());
				});
				
			} else {
				alert('Si \u00E8 verificato un problema nel generare la mappa (' + status + ')');
			}
		});
	   
	})
  .trigger('click');
	
	$('#reverseGeocoding').click(function () {
		
		var lat = parseFloat($('#lat').text());
		var lng = parseFloat($('#lng').text());
		var latlng = new google.maps.LatLng(lat, lng);
	   
		geocoder.geocode({'latLng': latlng}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				
				console.log('Reverse Geocoding:');
				console.dir(results);
				
				$('#reverseGeocodingResult_formatted_address').text(results[0].formatted_address);
				
				// address components: recupero di tutti gli elementi 
				console.log('Address components:');
				console.dir(results[0].address_components);
			   
				var temp=[];
				for(var i=0; i<results[0].address_components.length; i++) {
					
					console.log(results[0].address_components[i]);
					
					if($.inArray(results[0].address_components[i].long_name, temp) === -1) { // evita potenziali duplicati
						temp.push(results[0].address_components[i].long_name);
					}
					if($.inArray(results[0].address_components[i].short_name, temp) === -1) { // evita potenziali duplicati
						temp.push(results[0].address_components[i].short_name);
					}
				}
				
				console.log('Address components (elaborati):');
				console.dir(temp);
				$('#reverseGeocodingResult_address_components').html(temp.join('<br>'));
				
			} else {
				alert("Geocoder failed due to: " + status);
			}
		});
	});
	
});

              
            
!
999px

Console