Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                <nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item active" aria-current="page">Búsqueda de dirección en mapa</li>
  </ol>
</nav>


<div class="container">
	
	<div class="row">
		<div class="col">
			<div class="input-group mb-3">
				<input type="text" id="txt_direccion" 
					 class="form-control" 
					 placeholder="Ingrese dirección (calle, número, ciudad, comuna.)"
					 aria-describedby="direccionHelp">
				<div class="input-group-append">
					<button class="btn btn-outline-primary" type="button" id="btn_buscar">Buscar</button>
				</div>
			</div>
		</div>
	</div>

	<div class="row">
		<div class="col">
			<div id="map"></div>
			<p>&nbsp;</p>
		</div>
	</div>
	
	<div class="row">
		<div class="col">
			<div class="list-group" id="lst_resultado">
				<div class="list-group-item list-group-item-action">Busca una dirección</div>
			</div>
		</div>
		
	</div>
	
	
</div>
              
            
!

CSS

              
                #content {
	display: block;
	width: 90%;
	margin: 0px auto;
}
.bloque {
	margin-top: 30px;
}
#txt_api_01 {
	font-size: 0.8em;
}
#map { 
	height: 500px; 
}
              
            
!

JS

              
                var map = L.map('map', {
    center: [51.4774009, 0],
    zoom: 15
});

var marcadores = [];
var salida_direcciones = [];

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);


$("#btn_buscar").click(function(){
	if ($("#txt_direccion").val().trim() != '') {
		
		$("#lst_resultado").html('<div class="list-group-item list-group-item-action">Buscando...</div>');
		salida_direcciones = [];

		$.get("https://nominatim.openstreetmap.org/search?q=" + $("#txt_direccion").val() + "&format=json&polygon=0&addressdetails=1", 
			function(retorno) {
				salida_direcciones = retorno;

				$("#lst_resultado").html('');
				if (salida_direcciones.length == 0) {
					$("#lst_resultado").html('<div class="list-group-item list-group-item-action">No hay resultados</div>');
				} else {
					
					$.each(salida_direcciones, function(index, item){
						$("#lst_resultado").append('<a href="#" data="'+index+'" class="list-group-item list-group-item-action lnk_mapa">'+item.display_name+'</a>')
					});
					
					$(".lnk_mapa").click(function(){
						carga_direccion_mapa($(this).attr('data'));
					});
				} 
			}
		);
		
	} else {
		alert("Debe ingresar una dirección");
	}

});

$("#txt_direccion").keypress(function( event ){
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		$("#btn_buscar").click();
	}
});



function carga_direccion_mapa ( idx_elemento ) {

	if (salida_direcciones.length > 0 && 
		idx_elemento < salida_direcciones.length) {
	
		var lat = salida_direcciones[idx_elemento].lat;
		var lon = salida_direcciones[idx_elemento].lon;

		// Mueve el mapa a la posicion de la dirección
		map.panTo(L.latLng(lat, lon));
		
	
		
		// Limpia los marcadores
		$.each(marcadores, function(index, item){
			item.remove();
		});
		
		var direccion = salida_direcciones[idx_elemento];

		// Ingresa los marcadores
		marcadores.push(L.marker([lat, lon]).addTo(map)
		.bindPopup(direccion)
		.openPopup());
		
	}
}


              
            
!
999px

Console