<!-- Replace <YOUR_API_ACCESS_TOKEN> with your LocationIQ token before using this code. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="#" />
<title>Search box only</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<!-- Include the geocoder control -->
<script src="https://tiles.locationiq.com/v3/libs/gl-geocoder/5.0.0/locationiq-gl-geocoder.min.js?v=0.1.9"></script>
<link rel="stylesheet" href="https://tiles.locationiq.com/v3/libs/gl-geocoder/5.0.0/locationiq-gl-geocoder-mapless.css?v=0.1.9" type="text/css" />
<style>
body {
margin: 0;
}
#search-box {
padding-left: 20px;
padding-top: 20px;
width: 800px;
height: 100px;
}
#result {
padding-left: 20px;
padding-top: 20px;
width: 800px;
height: 100px;
}
/* Optional: customize the standalone geocoder typography. */
.locationiq-ctrl-geocoder {
font-size: 14px;
line-height: 20px;
}
.locationiq-ctrl-geocoder .suggestions {
font-size: 13px;
}
.locationiq-ctrl-geocoder--suggestion-address {
font-size: 12px;
line-height: 16px;
}
</style>
</head>
<body>
<div class="container">
<!-- For the search box -->
<div id="search-box"></div>
<!-- To display the result -->
<div id="result"></div>
</div>
<script>
//Add your LocationIQ Access Token here - https://my.locationiq.com/
var locationiqKey = '<YOUR_API_ACCESS_TOKEN>';
//Add Geocoder control to the page
var geocoder = new LocationIQGeocoder({
accessToken: locationiqKey,
// This search box is not attached to a map, so disable map-only behavior explicitly.
marker: false,
flyTo: false,
trackProximity: false,
// These options are passed through to the LocationIQ Autocomplete API.
limit: 5,
dedupe: 1,
normalizecity: 1
});
geocoder.addTo('#search-box');
// Listen for the selected result.
geocoder.on('result', function (event) {
var result = event.result;
displayLatLon(result.display_name, result.center[1], result.center[0]);
});
//Displays the geocoding response in the "result" div
function displayLatLon(display_name, lat, lng) {
var resultString = "You have selected " + display_name + "<br/>Lat: " + lat + "<br/>Lon: " + lng;
document.getElementById("result").innerHTML = resultString;
}
</script>
</body>
</html>