公開日:2020.04.01 更新日:2022.04.19

Displaying MapFan API search results in Leaflet

X Facebook LinkedIn

MapFan API offers various search functions (geocoding) for obtaining latitude and longitude from addresses and place names, as well as a function for obtaining addresses from latitude and longitude (reverse geocoding).

This time, we'll introduce the functions of various search APIs and how to display their results in Leaflet.

For more information on searching (geocoding and reverse geocoding), please see this article, which provides an overview.

Download the MapFan API specification here >

MapFan API Search API

MapFan API provides the following search APIs:

  • Address search API (addr)
  • Address hierarchy search API (addrlist)
  • Address reverse lookup API (addrname)
  • Spot search API (spot)
  • Spot Area Search API (spotarea)
  • Station search API (sta)
  • Station hierarchy search API (stalist)
  • Nearest Station Search API (staarea)
  • Postal code search API (zip)

We will now explain how to use the search functions highlighted in bold above.

Address search API (addr)

The address search API (addr) allows you to retrieve latitude, longitude, and address hierarchy code values for an address string specified using free text.
We also support searching by street name in Kyoto.

If the entered address does not exist due to a typo or other reason, the search will be performed using the addresses up to the highest level found.
You can check how far down the search hierarchy the match reached by looking at the "level" included in the response.

Furthermore, it also supports variations in notation, including the following:

  • Variations in numerical representation (half-width characters, full-width characters, Chinese numerals)
  • Variations in the expression of block numbers and house numbers (block number, house number, hyphen)
  • Variations in particles within place names
    • ga / ke / ke (e.g., Midorigaoka)
    • Tsu / tsu / tsu / tsu (Example: Mitsuzawa)
    • の / ノ / 之 (Example: Nishinoshima)
    • る / ル (Example: 西入下る)
  • Presence or absence of the building name at the end (e.g., XX Building)
  • Presence or absence of large village/small village

// 住所検索API(addr)
var url = 'https://api-srch.mapfan.com/v1/addr?' + 'key=' + '【アクセスキー】' + '&addr=' + encodeURI('【住所文字列】') + '&callback=addrResult';
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);

// 実行結果
function addrResult(response) {
    if(response.status === 'success') {
        for(var result of response.results) {
            var marker = L.marker([result.lat, result.lon]).addTo(map);
            var content = result.zipcd + '<br>' + result.name + '<br>' + 'level: ' + response.level;
            switch(response.level) {
                case 0:
                    content += '(都道府県階層)'
                    break;
                case 1:
                    content += '(市区町村階層)'
                    break;
                case 2:
                    content += '(条町丁目階層)'
                    break;
                case 3:
                    content += '(番地階層)'
                    break;
                case 4:
                    content += '(号階層)'
                    break;
            }
            marker.bindPopup(content).openPopup();
        }
    }
}

Address hierarchy search API (addrlist)

The address hierarchy search API (addrlist) can retrieve a list of addresses for each address hierarchy level.

When retrieving an address string, you can prevent errors caused by manual input by having the user select from a list in the following order: "Prefecture → City/Ward/Town/Village → Town/District → Street Number → House Number".

var resultMarkers = L.featureGroup();
resultMarkers.addTo(map);

// 住所階層検索API(addrlist)
function executeAddrlist(level, prefcd = null, citycd = null, tyocd = null, bancd = null) {
	// levelで指定された階層の検索に必要なパラメータの指定有無をチェック
    if((level >= 1 && prefcd == null) || (level >= 2 && citycd == null) || (level >= 3 && tyocd == null) || (level >= 4 && bancd == null)) {
        alert('parameter error');
        return;
    }
    var url = 'https://api-srch.mapfan.com/v1/addrlist?'
              + 'key=' + '【アクセスキー】'
              + '&level=' + level
              + (prefcd !== null ? '&prefcd=' + String(prefcd) : '')
              + (citycd !== null ? '&citycd=' + String(citycd) : '')
              + (tyocd !== null ? '&tyocd=' + String(tyocd) : '')
              + (bancd !== null ? '&bancd=' + String(bancd) : '')
              + '&callback=addrlistResult';
    var script = document.createElement('script');
    script.src = url;
    document.head.appendChild(script);
}

// 実行結果
function addrlistResult(response) {
    if(response.status === 'success') {
        resultMarkers.clearLayers();
        
        // Markerクラスでは検索結果情報の引き回しが難しいため、GeoJsonクラスを利用
        for(var result of response.results) {
            var geoJson = L.geoJSON({
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [result.lon, result.lat]
                },
                "properties": {
                    "name": result.name,
                    "level": result.level,
                    "prefcd": result.prefcd,
                    "citycd": result.citycd,
                    "tyocd": result.tyocd,
                    "bancd": result.bancd
                },
            }, {
                pointToLayer: function(feature, latlng) {
                    var marker = L.marker(latlng).bindTooltip(feature.properties.name);
                    
                    // クリックで下階層を検索
                    marker.on('click', e => {
                        executeAddrlist(parseInt(feature.properties.level) + 1, feature.properties.prefcd, feature.properties.citycd, feature.properties.tyocd, feature.properties.bancd)
                    });
                    return marker;
                }
            }).addTo(resultMarkers);
        }
        map.fitBounds(resultMarkers.getBounds());
    }
}

// 都道府県一覧を取得
executeAddrlist(0);

Address reverse lookup API (addrname)

The address reverse lookup API (addrname) can retrieve the address for a specified latitude and longitude.

You can obtain the center coordinates and address of the clicked location on the displayed map, and you can add address information to each route point when searching for a route by selecting the starting point, intermediate points, and destination on the map, as well as to the latitude and longitude track obtained by GPS, etc.

var resultMarker;

// クリック位置の住所を取得
map.on('click', e => {
    resultMarker = L.marker(e.latlng).addTo(map);
    
    // 住所逆引き検索API(addrname)
    var url = 'https://api-srch.mapfan.com/v1/addrname?' + 'key=' + '【アクセスキー】' + '&lonlat=' + e.latlng.lng + ',' + e.latlng.lat + '&callback=addrnameResult';
    var script = document.createElement('script');
    script.src = url;
    document.head.appendChild(script);
});

// 実行結果
function addrnameResult(response) {
    if(response.status === 'success') {
        if(map.hasLayer(resultMarker)) {
            resultMarker.bindPopup(response.zipcode + '<br>' + response.address).openPopup();
        }
    }
}

Spot search API (spot)

The Spot Search API (spot) allows you to retrieve the latitude, longitude, address, phone number, and other information of a spot specified by a free word.
You can also narrow down the data by specifying genres, prefectures, cities, towns, etc.

// スポット検索API(spot)
var url = 'https://api-srch.mapfan.com/v1/spot?' + 'key=' + '【アクセスキー】' + '&name=' + encodeURI('【スポット名】') + '&callback=spotResult';
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);

// 実行結果
function spotResult(response) {
    if(response.status === 'success') {
        for(var result of response.results) {
            var marker = L.marker([result.lat, result.lon]).addTo(map);
            var content = '<p class="contentTitle">' + result.name + '</p>' + result.addr;
            content += result.tel ? '<br>' + result.tel : '';
            content += result.comment ? '<br>' + result.comment : '';
            marker.bindPopup(content);
        }
    }
}

Spot Area Search API (spotarea)

The SpotArea API allows you to retrieve spots around a specified latitude and longitude, sorted by proximity.
You can also narrow down the data by specifying the search range, keywords, genre, etc.

var resultMarkers = L.featureGroup();
resultMarkers.addTo(map);

// クリック位置周辺のコンビニ(gnrcd=15)を取得
map.on('click', e => {
    map.setView(e.latlng, map.getZoom());
    
    // スポット周辺検索API(spotarea)
    var url = 'https://api-srch.mapfan.com/v1/spotarea?' + 'key=' + '【アクセスキー】' + '&lonlat=' + e.latlng.lng + ',' + e.latlng.lat + '&gnrcd=15' + '&callback=spotareaResult';
    var script = document.createElement('script');
    script.src = url;
    document.head.appendChild(script);
});

// 実行結果
function spotareaResult(response) {
    if(response.status === 'success') {
        resultMarkers.clearLayers();

        for(var result of response.results) {
            var marker = L.marker([result.lat, result.lon]).addTo(resultMarkers);
            var content = '<p class="contentTitle">' + result.name + '<br>地図中心から ' + Math.round(result.distance) + ' [m]</p>' + result.addr;
            content += result.tel ? '<br>' + result.tel : '';
            content += result.comment ? '<br>' + result.comment : '';
            marker.bindPopup(content);
        }
    }
}

Station search API (sta)

The Station Search API (sta) allows you to retrieve information such as the latitude, longitude, and line name of a station specified by a free word.
Additionally, by enabling the station entrance/exit search setting, you can retrieve a list of entrances/exits for the searched station.

var resultMarkers = L.featureGroup();
resultMarkers.addTo(map);

var stationGateways = {};

// 駅検索API(sta)
var url = 'https://api-srch.mapfan.com/v1/sta?' + 'key=' + '【アクセスキー】' + '&name=' + encodeURI('【駅名】') + '&gateway=1' + '&callback=staResult';
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);

// 実行結果
function staResult(response) {
    if(response.status === 'success') {
        // 駅検索結果
        var stations = L.featureGroup();
        stations.addTo(map);
        for(var resultStation of response.station.results) {
            var marker = L.marker([resultStation.lat, resultStation.lon]).bindTooltip(resultStation.name).addTo(stations);

            // 駅出入口検索結果
            var gateways = L.featureGroup();
            for(var resultGateway of response.stationGateway.results) {
               	// 鉄道分類コード・鉄道会社コード・鉄道路線コード・路線内シーケンス番号 が一致する駅出入口を紐付け
                const hit = resultGateway.train_infos.item.some(item => {
                    return (resultStation.typecd === item.train_info.item[0].code && resultStation.corpcd === item.train_info.item[1].code && resultStation.linecd === item.train_info.item[2].code && resultStation.lineseq === item.train_info.item[3].code)
                });
                if(hit) {
                    gateways.addLayer(L.circleMarker([resultGateway.lat, resultGateway.lon], {
                        color: '#ff0000',
                        fillColor: '#ff0000',
                        fillOpacity: 0.6,
                    }).bindTooltip(resultGateway.name));
                }
            }
            stationGateways[resultStation.name] = gateways

            marker.on('click', e => {
                resultMarkers.clearLayers();
                var currentGateways = stationGateways[e.target._tooltip._content];
                currentGateways.addTo(resultMarkers);
                map.fitBounds(currentGateways.getBounds());
            });
        }
        map.fitBounds(stations.getBounds());
    }           
}

summary

In addition to the options introduced here, MapFan API also offers other options such as the ability to use information from the Yellow Pages and the ability to obtain search results for nearby locations in multiple languages.

If you have any questions about the features, please contact us using the form below.

For inquiries about MapFan API please click here >

Download the MapFan API specification here >

For details on MapFan API, click here >

Inquiries regarding services for corporate clients
For inquiries, please click here.

Related articles

For requests for brochures or information about our services,
please feel free to contact us using the form below.
​