Tutorials / Learn How to Geocode and Display Japanese Addresses on a Map Using HERE Geocoding and Search API
Last Updated: November 25, 2020
Introduction
Searching for an address in Japan is often complicated as there are often no street names. Also, the Japanese address is mixed because there are three different types of native character sets (Kanji, Hiragana, and Katakana). The HERE Geocoding and Search API make this complex system easy for your applications to find the right location.
In this tutorial, you will learn how to develop a sample geocoding application using the HERE Geocoding and Search API, conduct an address lookup using HERE’s Autosuggestion feature and then plot the location on a JavaScript map.
For more information about Geocoding in Japan and the Search API, check out the documentation.
Access to the Japanese data layer. Please see the HERE JavaScript API Documentation to learn how to enable access for your developer account.
What are You Going to Build
After going through this tutorial, you will have a working geocoding application for Japan. By using this you can search for a location in Japan by providing an address as an input.
Road Map
Create a HERE Map
Implement Geocoding for Japan
Create a HERE Map
The basis of this project is a JavaScript map. Creating a Japanese focused map is very similar to creating a non-Japanese map. You will need to follow the steps below to get started.
Generate Map
To create a basic HERE JavaScript map, follow the steps below:
Open your favorite editor.
Create an index.html file.
Copy the following boilerplate code into the index.html.
If you do not have an API Key, you can follow this video to learn how to create your API key.
Initialize the Platform object normally by passing the API key. Replace the YOUR_API_KEY with your HERE API key.
var platform = new H.service.Platform({
'apikey': '{YOUR_API_KEY}'
});
Render the HERE Map
You will need a dedicated map style for the Japanese market in order to render the map. The following map style is optimized for the display of the Japan data.
To get the Japan specific data, you need to add the below code:
var omvService = platform.getOMVService({ path: "v2/vectortiles/core/mc" });
Now, you will setup the HERE map by adding the following code.
// configure an OMV service to use the `core` endpoint
var omvService = platform.getOMVService({ path: "v2/vectortiles/core/mc" });
var baseUrl = "https://js.api.here.com/v3/3.1/styles/omv/oslo/japan/";
// create a Japan specific style
var style = new H.map.Style(`${baseUrl}normal.day.yaml`, baseUrl);
// instantiate provider and layer for the base map
var omvProvider = new H.service.omv.Provider(omvService, style);
var omvlayer = new H.map.layer.TileLayer(omvProvider, { max: 22 ,dark:true});
// instantiate (and display) a map:
var map = new H.Map(document.getElementById("mapContainer"), omvlayer, {
zoom: 17,
center: { lat: 35.68026, lng: 139.76744 },
});
To make the map interactive, add the below code:
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener("resize", () => map.getViewPort().resize());
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
Final Code to Render the HERE Map
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- libraries -->
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
<!-- end -->
</head>
<body>
<!-- Display the map -->
<div class="main">
<div style="width: 100%; height: 100%; overflow: hidden; position: fixed; top: 0; z-index: 2;" id="mapContainer"></div>
</div>
<!-- end -->
</body>
<script>
// to connect to HERE Services using API Key
var platform = new H.service.Platform({
apikey: "HERE-API-KEY",
});
// configure an OMV service to use the `core` endpoint
var omvService = platform.getOMVService({ path: "v2/vectortiles/core/mc" }); // to get japan data
var baseUrl = "https://js.api.here.com/v3/3.1/styles/omv/oslo/japan/";
// create a Japan specific style
var style = new H.map.Style(`${baseUrl}normal.day.yaml`, baseUrl);
// instantiate provider and layer for the basemap
var omvProvider = new H.service.omv.Provider(omvService, style);
var omvlayer = new H.map.layer.TileLayer(omvProvider, { max: 22 ,dark:true});
// instantiate (and display) a map:
var map = new H.Map(document.getElementById("mapContainer"), omvlayer, {
zoom: 17,
center: { lat: 35.68026, lng: 139.76744 },
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener("resize", () => map.getViewPort().resize());
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
</script>
</html>
Open the index.html page with a browser to see the map.
map
To learn more about the Japan map. Check out the tutorial.
Implement Geocoding for Japan
Autosuggest API
To search for an address or place, you need to use the Autosuggest API to render the results. The Autosuggest API will fetch the results as you type in the input field. It will provide an autosuggestion when you start typing. Below is the endpoint:
Once the HTML map is added, you need to add the code for the search box to the map. Copy the following HTML snippet inside the mapContainer div tag in the index.html after the script tag.
Once you add the above code, you will see the output similar to the below image.
search
When the user starts typing in the search box, you will use the Autosuggest API to return address suggestions and you will get the results. You can select the place from the list to show the marker on the map. Click on the marker to see the selected address.
Add the below code in the index.html file anywhere in the <script> tag:
const GeoCodeAutoSuggest = (q) => {
if (!q || q == null) {
alert("Invalid search key word..");
return;
}
let url = `https://autosuggest.search.hereapi.com/v1/autosuggest?at=35.6990575,139.7623009&q=${q}&limit=3&apikey=${HERE-API-KEY}`;
fetch(url)
.then((res) => res.json())
.then((data) => showAutoSuggestList(data));
};
/* Create a Marker on the map */
const AddDefaultMakerToMap = (cordinates = null, textContent) => {
if (cordinates == null) {
return;
}
isMapAnimated = true
var Marker = new H.map.Marker({ lat: cordinates.lat, lng: cordinates.lng });
Marker.setData(textContent);
var group = new H.map.Group();
map.addObject(group);
// add 'tap' event listener, that opens info bubble, to the group
Marker.addEventListener('tap', function (evt) {
// event target is the marker itself, group is a parent event target
// for all objects that it contains
var bubble = new H.ui.InfoBubble(evt.target.getGeometry(), {
// read custom data
content: evt.target.getData()
});
// show info bubble
ui.addBubble(bubble);
}, false);
map.addObject(Marker);
map.setCenter({ lat: cordinates.lat, lng: cordinates.lng }, isMapAnimated);
};
/* To render the results when users search for a place */
const showAutoSuggestList = (data) => {
map.removeObjects(map.getObjects()) // to remove the marker
document.getElementById("autosuggest-items").innerHTML = "";
data.items.map((item) => {
let li = document.createElement("li");
li.textContent = item.title;
li.dataPosition = item.position;
li.addEventListener("click", (e) => {
document.getElementById("autosuggest-items").innerHTML = "";
document.getElementById("search-box").value = e.target.innerHTML;
AddDefaultMakerToMap(e.target.dataPosition);
});
document.getElementById("autosuggest-items").appendChild(li);
});
};
You are done with adding the JavaScript code to search for an address or place.