Solutions Day 21-25: #100DaysOfCode
Week 5 of #100DaysOfCode with HERE whaaat! If you have no idea what I'm talking about, take a look at this blog post which will tell you everything about #100DaysOfCode. In this blog post, I will cover solutions for days 21 through 25. If you have missed the solutions for days 0-20, you can read them in my previous blogs posts or in the video format.
Let's begin!
Day 21/100
We are starting day 21 with the mighty Geocoder. The Geocoding and Search API is a powerful tool to find the location of a place based on the postal address. The geocoder responds with a latitude and longitude as well as many other helpful parameters to a query with the address of the place. In the next few days, we will explore the Geocoding and Search API and all its different endpoints.
Day 21 in itself is the most simple use of the geocoder. You pass a srtring with a partial address and you get all possible locations that match your string. I entered the most common street name here 'Hauptstarße' to see how many results I get and to no surprise found out that there are atleast 20 'Hauptstraße' on the Earth!
// Get an instance of the geocoding service:
var geocoder = platform.getSearchService();
function geocodeAndSearch(){
let geocoderParams = {
q : 'hauptstraße'
}
function onResult(result){
console.log(result);
}
geocoder.geocode(geocoderParams,onResult,alert);
}
geocodeAndSearch();
Day 22/100
The query from day 21 gives us too many results and does not make it easier in the situation of places with common names. On day 22, we will limit the number of results you can get with an address search. The Geocoding and Search API has different filters that you can apply to limit your search. You can read about different filters in the documentation. Here we are simply going to use the input parameter limit
to limit the results.
// Get an instance of the geocoding service:
var geocoder = platform.getSearchService();
function geocodeAndSearch(){
let geocoderParams = {
q : 'hauptstraße',
limit : 5
}
function onResult(result){
console.log(result);
}
geocoder.geocode(geocoderParams,onResult,alert);
}
geocodeAndSearch();
Day 23/100
Limiting the search by the number of results is one thing, although it makes more sense to limit it by geography. For day 23, we will limit the results by country. If you check the in
parameter in the Geocoding and Search API, you will see that it has the option to limit the search to a country or countries when separated by commas. This filter is a hard filter thus it will not give you results beyond the countries you have specified. The countries are specified using the ISO 3166-1 alpha-3 country codes. Thus Germany will be represented as 'DEU'
// Get an instance of the geocoding service:
var geocoder = platform.getSearchService();
function geocodeAndSearch(){
let geocoderParams = {
q : 'hauptstraße',
in: 'countryCode:DEU',
limit : 5
}
function onResult(result){
console.log(result);
}
geocoder.geocode(geocoderParams,onResult,alert);
}
geocodeAndSearch();
Day 24/100
Limiting the results to a country made the results a little more meaningful. Although, there still are 5 'hauptstraße' in Germany. This time, I will enter a structured address with more information about the place I wish to find. This can be done with a qualified query. It is still a free form text, but a little structured where you can specify which part of the address is the street name, which is the house number, etc. As a result, I get the street 'hauptstraße' in Berlin, Germany.
// Get an instance of the geocoding service:
var geocoder = platform.getSearchService();
function geocodeAndSearch(){
let geocoderParams = {
qq : 'street=hauptstraße;city=Berlin;country=Germany'
}
function onResult(result){
console.log(result);
}
geocoder.geocode(geocoderParams,onResult,alert);
}
geocodeAndSearch();
Day 25/100
The above queries show filters needed when the address has common names in it. For certain strings, the geocder directly gives the desired place. For instance, if we do a free form search for 'Invalidenstraße 116, Berlin', we get the location of the desired place immediately. What is interesting is the parameters that come along with the location. For day 25, we are interested in the parameter 'access'. Not only do we get the 'position' of the place, but we also get the access point of the building/ house.
// Get an instance of the geocoding service:
var geocoder = platform.getSearchService();
function geocodeAndSearch(){
let geocoderParams = {
q : 'invalidenstraße 116, Berlin'
}
function onResult(result){
console.log(result);
map.addObject(new H.map.Marker(result.items[0].access[0]));
}
geocoder.geocode(geocoderParams,onResult,alert);
}
geocodeAndSearch();
If you expand the results, you will see that under the 'items' you get a parameter called 'access' which will give you the access point of that property
{…}
items: (1) […]
0: {…}
access: (1) […]
0: {…}
lat: 52.531
lng: 13.38461
<prototype>: Object { … }
length: 1
This was the first week of the Geocoding and Search on #100DaysOfCode. We will have more of the this in the days to come. Hope you enjoyed this week of #100DayOfCode with HERE. Keep following us on Twitter for more tasks and complete all 100 days. If you want to watch the video version of these solutions, take a look at our playlist for #100DaysOfCode on YouTube.
While you're at it, why don't you take a look at the blog post announcing the release of our new mobile SDKs.
Want to know how you can use maps to gamify biking? Check out the blog post by the participants of BCX20 who won third place in the mobility stream by building Speedy.bike.
Also take a look at the step by step tutorial for the Geocoding and Search API.
Happy coding!
Have your say
Sign up for our newsletter
Why sign up:
- Latest offers and discounts
- Tailored content delivered weekly
- Exclusive events
- One click to unsubscribe