Single input field
For geocoding the parts of an adress can be passed in a single string. The sequence of the parts of an adress is arbitrary.
The example is complete; you can save it locally in a file and call it in the browser. You only have to replace the value for the apiKey
.
SmartMaps map
Code example: Single input field
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Geocoding with simple input field.</title>
</head>
<body>
<div id="map-wrapper">
<div class="geocoder-form" style="z-index:1000">
<form id="single-slot-form">
<fieldset>
<input type="text" name="singleSlot" value="Wattkopfstr, 76133 Karlsruhe" placeholder="Zip/City/Street/HouseNo">
<button type="submit">send</button>
</fieldset>
</form>
</div>
<div id="map" style="height: 400px; width: 500px;"></div>
</div>
<!-- SmartMaps-API -->
<script src="https://www.yellowmap.de/api_rst/api/loader?libraries=free-3&apiKey={API_KEY}"></script>
<script>
ym.ready(function(modules) {
var singleSlotForm = document.getElementById('single-slot-form');
// Define Layer Group.
var layerGroup = ym.layerGroup();
// Draw in map.
var map = ym.map("map", {
center: ym.latLng(49.021273, 8.439316),
zoom: 14
});
// Drawing a Layer Group.
layerGroup.addTo(map);
singleSlotForm.onsubmit = function(e) {
e.preventDefault();
var input = document.querySelector('#single-slot-form input');
// Geocoding address data.
modules.geocodeString(input.value);
};
ym.services.geoCoder.on('success', function(req, res) {
// Delete old data from the card.
layerGroup.clearLayers();
var geoJson = ym.geoJson(res.body, {
// Draw markers for each entry.
onEachFeature: function(feature, layer) {
layer.setIcon(ym.provider.Icon.Default());
var popUpContent = "";
if (feature.properties.street) {
popUpContent += feature.properties.street + ", "
}
if (feature.properties.zip) {
popUpContent += feature.properties.zip + " "
}
if (feature.properties.city) {
popUpContent += feature.properties.city + " "
}
if (feature.properties.cityPart) {
popUpContent += feature.properties.cityPart
}
// Show address data in popup.
layer.bindPopup(popUpContent);
}
});
// Optimize map section for result set.
map.fitBounds(geoJson.getBounds());
layerGroup.addLayer(geoJson);
});
ym.services.geoCoder.on('error', function(req, res, errorType) {
console.log(arguments);
});
});
</script>
</body>
</html>