cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

            
              <form>
	<h2>Google Maps API - AutoComplete Address</h2>
	<div id="locationField" class="question">
		<label for="autocomplete">Enter your address</label>
		<input id="autocomplete" placeholder="Start typing your address" type="text"></input>
	</div>

	<div class="question address-block">
		<input class="field" id="street_number"></input>
		<input class="field" id="premise"></input>
		<input class="field" id="route"></input>
		<input class="field" id="locality"></input>
		<input class="field" id="postal_town"></input>
		<input class="field" id="postal_code"></input>
		<input class="field" id="country">
	</div>
</form>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Open+Sans:400,700');


$font:	Open Sans, Arial, Helvetica, sans-serif;

$size-body: 17px;
$size-smaller: 15px;

// Primary Colours
$color-primary: 	#BBB;
$color-active: 	#333;
$color-text:		#666;
$color-grey:		#BBB;
$color-valid:		#B4D411;
// Error Colours
$error-main: #AE3535;
$error-second: #FFF0F5;

// Spacing
$gutter: 20px;
$gutter-minus: -$gutter;
$gutter-quarter: $gutter / 4;
$gutter-half: $gutter / 2;
$gutter-half-minus: -$gutter-half;
$gutter-double: $gutter * 2;
$gutter-double-minus: -$gutter-double;
$gutter-triple: $gutter * 3;
$gutter-triple-minus: -$gutter-triple;
$radius: 5px;

// Form Widths / Buttons
$questionWidth: 180px;
$inputWidth: 460px;
$inputHeight: 50px;

$dateWidth: calc((100% - 200px) / 3);
$buttonWidth: calc(33.33% - (20px / 3));

* {
	box-sizing: border-box;
}

html,
body {
	font-family: $font;
	background: #e0e0e0;
	position: relative;
	margin: 0;
	padding: 0;
	height: 100%;
}
body {
	padding: $gutter-double;
	@media only screen and (max-width: 420px){
		padding: $gutter;
	}
}

form {
	width: 560px;
	max-width: 100%;
	padding: 30px;
	background: #FFF;
	margin: $gutter-double auto;
	border-radius: 6px;
	@media only screen and (max-width: 420px){
		padding: $gutter;
	}
}

h1 {
	margin-top: 0;
	margin-bottom: 30px;
}


/* QUESTION STYLES */
.question {
	padding: 0 0 10px;
	font-size: 0;
	position: relative;
	display: block;
}


/* LABEL STYLES */
label {
	font-family: $font;
	font-size: $size-body;
	font-weight: 400;
	margin-bottom: $gutter-half;
	width: 100%;
	display: inline-block;
	vertical-align: top;
	padding-right: $gutter;
	color: $color-text;
}


/* INPUT STYLES */
input {
	border: 2px solid $color-grey;
	padding: 0 $gutter;
	color: $color-text;
	display: inline-block;
	max-width: $inputWidth;
	width: 100%;
	height: $inputHeight;
	font-size: $size-body;
	font-family: $font;
	.good & {
		border: 2px solid $color-valid;
		color: #B4D411;
	}
	.error & {
		border: 2px solid $error-main;
	}
}




.error-message {
	font-size: 12px;
	color: #AE3535;
	font-weight: bold;
	border: 2px solid #AE3535;
	padding: 5px 10px;
	display: none;
	background: #ffe0e0;
	margin-left: 180px;
	.error & {
		display: block;
	}
}



button {
	border: 0 none;
	width: $inputWidth;
	height: $inputHeight;
	margin-left: $questionWidth;
	border-radius: 5px;
	background: $color-active;
	color: #FFF;
	font-size: $size-body;
	font-family: $font;
	font-weight: 600;
}

fieldset.question {
	border: 0;
	margin-left: $questionWidth;
	padding: 0 0 $gutter-half;
	font-size: 0;
	position: relative;
	display: block;
	max-width: 100%;
	// legend styles
	legend {
		font-size: $size-body;
		margin-bottom: $gutter-half;
		width: $questionWidth;
		display: inline-block;
		vertical-align: top;
		padding-right: $gutter;
		color: $color-text;
		line-height: $inputHeight;
		letter-spacing: -0.04em;
		position: absolute;
		top:0;
		left: -$questionWidth;
	}
	// label used as button
	label {
		display: inline-block;
		width: $buttonWidth;
		height: $inputHeight;
		margin-right: $gutter-half;
		margin-bottom: $gutter-half;
		padding: 9px $radius;
		line-height: 1.4;
		background: $color-primary;
		text-align: center;
		font-size: $size-smaller;
		color: #FFF;
		text-align: center;
		cursor: pointer;
		border-radius: 5px;
		&:nth-of-type(3n) {
			margin-right: 0;
		}
		&.single-line {
			padding: 0 $radius;
			line-height: $inputHeight;
		}
	}
	
	// fieldset has error
	&.error {
		label {
			background: $error-main;
		}
	}

	// style hovered button
	input:hover + label {
		background: darken($color-primary, 25%);
		color: #fff;
	}
	// style checked button
	input:focus + label,
	input:checked + label {
		background: $color-active;
		color: #fff;
	}
	
	// accessible hide inputs
	input {
		border: 0;
		clip: rect(0 0 0 0);
		height: 1px;
		margin: -1px;
		overflow: hidden;
		padding: 0;
		position: absolute;
		width: 1px;
	}
}


.address-block {
	background: #FFF;
	border-radius: $gutter-quarter;
	padding: $gutter;
	display: none;
	&.addresses-found {
		display: block;
		border: 2px solid limegreen;
	}
	input {
		height: unset;
		width: 100%;
		border: 0;
		padding: $gutter-quarter 0;
	}
}


h2 {
	margin-top: 0;
	font-size: 20px;
}


a {
	color: $color-active;
	font-weight: bold;
}
            
          
!
            
              // This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

// form fields we'll populate and the string type we want from place object
var componentForm = {
	street_number: "short_name",
	premise: "long_name",
	route: "long_name",
	locality: "long_name",
	postal_town: "long_name",
	country: "long_name",
	postal_code: "short_name"
};

// set api options to restrict search area to UK.
var options = {
	types: ["geocode"],
	componentRestrictions: { country: "gb" }
};

function initAutocomplete() {
	// Create the autocomplete object, restricting the search to geographical location types.
	autocomplete = new google.maps.places.Autocomplete(
		(document.getElementById("autocomplete")), options
	);
	// When the user selects an address from the dropdown, populate the address fields in the form.
	autocomplete.addListener("place_changed", fillInAddress);
}

function fillInAddress() {
	// Get the place details from the autocomplete object.
	var place = autocomplete.getPlace();
	// log the place details for debugging
	console.log(place);
	
	// clear value and hide each input in address block
	for (var component in componentForm) {
		var componentElm = document.getElementById(component)
		componentElm.value = "";
		componentElm.style.display = "none"
	}

	// Get each component of the address from the place details
	// and fill the corresponding field on the form.
	for (var i = 0; i < place.address_components.length; i++) {
		var addressType;
		
		// get address type 
		// e.g. street_number, route, postal_code
		addressType = place.address_components[i].types[0];
		
		// componentForm has matching address type
		// get address type value 
		// and set input value
		// and display the input elm on address block
		if (componentForm[addressType]) {
			var val = place.address_components[i][componentForm[addressType]];
			var addressTypeElm = document.getElementById(addressType);
			
			addressTypeElm.value = val;
			addressTypeElm.style.display = "block";
		}
	}
	
	// once address autocompleted
	// display the address block
	showAddressBlock()
}

// add 'addresses-found' class to address block
// this class will display the address block
function showAddressBlock(){
	var addressBlock = document.getElementsByClassName('address-block')[0]
	addressBlock.classList.add('addresses-found')
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function(position) {
			var geolocation = {
				lat: position.coords.latitude,
				lng: position.coords.longitude
			};
			var circle = new google.maps.Circle({
				center: geolocation,
				radius: position.coords.accuracy
			});
			autocomplete.setBounds(circle.getBounds());
		});
	}
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console