MapFan API allows you to search for routes using our own high-quality, high-precision, and up-to-date road data covering over 1.26 million km nationwide (as of January 2022).
In addition to a car route search function with features equivalent to a typical car navigation system, you can use a variety of route search functions, including routes optimized for pedestrians and large vehicles, and routes that efficiently visit multiple locations.
This time, we'll introduce the basic usage of the route search API and how to display its results in Leaflet.
[For Developers] Download the MapFan API specification here.
What you can do with route search
MapFan API 's route search has the following characteristics:
- Obtain the total route distance, estimated travel time, toll road fees, and turn-by-turn directions.
- In addition to the departure and destination points, you can specify up to 30 intermediate points.
Furthermore, it is also possible to acquire a "patrol route" that efficiently visits various locations.
In addition to considering regulations for large vehicles, it is possible to specify vehicle height, weight, width, and load capacity in detail using numerical values.
- In addition to searching for car routes, it is also possible to search for pedestrian routes.
- It is also possible to output elevation values for the route shape. *This is an additional paid option.
While MapFan API allows you to specify various conditions for route searching, this time we will simply show you how to search for a route between two points, a starting point and a destination, and how to display the obtained route on a map using Leaflet.
[For Developers] Download the MapFan API specification here.
Implementing map click events
In MapFan API route searches, route points such as the starting point and destination are specified using latitude and longitude.
This time, we will obtain the latitude and longitude by clicking on the map.
In this article, we will implement the map click event within the callback of the authentication API for the map display page using Leaflet.
The map click event for Leaflet is created as follows:
// 地図クリックイベント
map.on('click', function(e) {
// 処理を実装
});
ArgumentseThe latitude and longitude of the clicked location (latlng) and screen coordinates (layerPoint) is stored here.
First, place a pin at the click position (MarkerLet's try setting it up.
// 地図クリックイベント
map.on('click', function(e) {
// クリック位置にピンを立てる
var marker = L.marker(e.latlng).addTo(map);
});
If you're happy with the default icon design, the above process alone will place a pin at the click location.
If you want to use any icon image,IconCreateMarkerSpecify it as an option.
// スタートアイコン
var startIcon = L.icon({
iconUrl: './img/start.png',
iconAnchor: [16, 44],
});
// クリック位置にピンを立てる
var marker = L.marker(e.latlng, {
icon: startIcon,
}).addTo(map);
IconIf specified,MarkerThe latitude and longitude you specified will be set to the upper left corner of the icon image.
In the case of a pin design image like the one provided here, the base of the pin image should be positioned at the specified latitude and longitude.iconAnchorNow, let's offset the image appropriately.
![]()
For this version, setting the start and goal points will be simple: when you click on the map, a start pin will be placed if one hasn't been set yet, and a goal pin will be placed if one has already been set. Once both the start and goal points are set, a route search will be performed.
Start pin and finish pinMarkerThe existence check is,Map.hasLayer()We will do it this way.
Also, for subsequent processing,mapWe will also change this to a global variable.
var map;
var startMarker;
var goalMarker;
// 認証API コールバック
function authCallback(result) {
// 中略
// 地図クリックイベント
map.on('click', function(e) {
// スタートピン・ゴールピンともに設置済み
if(map.hasLayer(startMarker) && map.hasLayer(goalMarker)) {
return;
}
// スタートピンを設置
if(!map.hasLayer(startMarker)) {
// スタートアイコン
var startIcon = L.icon({
iconUrl: './img/start.png',
iconAnchor: [16, 44],
});
// クリック位置にピンを立てる
startMarker = L.marker(e.latlng, {
icon: startIcon,
}).addTo(map);
}
// ゴールピンを設置
else {
// ゴールアイコン
var goalIcon = L.icon({
iconUrl: './img/goal.png',
iconAnchor: [16, 44],
});
// クリック位置にピンを立てる
goalMarker = L.marker(e.latlng, {
icon: goalIcon,
}).addTo(map);
}
if(map.hasLayer(startMarker) && map.hasLayer(goalMarker)) {
// ルート検索を実行
}
});
}
With the implementation so far, you can create a screen like the one below.

Route search API: Executing calcroute
MapFan API Route Search API (calcroute) will be executed.
Executing various MapFan API requires the access key obtained through the authentication API, so store it in a variable within the authentication callback.
The route search API requires the latitude and longitude of the origin and destination as mandatory parameters.
We will use the latitude and longitude obtained from the map click event that we implemented earlier.
var accessKey;
// 認証API コールバック
function authCallback(result) {
if(result.status === 'success') {
accessKey = result.key;
// 中略
// 地図クリックイベント
map.on('click', function(e) {
// 中略
if(map.hasLayer(startMarker) && map.hasLayer(goalMarker)) {
// ルート検索を実行
executeCalcroute(startMarker.getLatLng(), goalMarker.getLatLng());
}
});
}
}
// ルート検索API実行
function executeCalcroute(startLatLng, goalLatLng) {
// MapFan API の ルート探索API
var url = 'https://api-route.mapfan.com/v1/calcroute?'
// アクセスキー
url += 'key=' + accessKey + '&';
// 出発地・目的地緯度経度
url += 'start=' + startLatLng.lng + ',' + startLatLng.lat + '&';
url += 'destination=' + goalLatLng.lng + ',' + goalLatLng.lat + '&';
// コールバック関数
url += 'callback=calcrouteResult';
// API実行
var script = document.createElement('script')
script.src = url
document.head.appendChild(script)
}
Plotting route search results on a map
Next, we will plot the results of the route search API on a map.
The route is,PolylineWe will use this to draw.
The response of the route search API has the following structure (excerpt), so perform two loops within the response.PolylineGet the latitude and longitude to be given to.
root
├ 検索結果 サマリー
│ ├ 距離
│ ├ 所要時間
│ └ 料金
├ 誘導データ[]
└ ルート形状データ[] ★ループ1
├ ルート形状に関する情報
│ └ 道路種別
└ 緯度経度点列[] ★ループ2
├ 緯度経度1
├ 緯度経度2
├ ...
└ 緯度経度n
// ルート検索API実行結果
function calcrouteResult(response) {
var latlngs = [];
// ルート形状データループ ★ループ1
for (var i=0; i<response.shape.length; i++) {
// 緯度経度点列ループ ★ループ2
for (var j=0; j<response.shape[i].shapePoints.length; j++) {
latlngs.push([response.shape[i].shapePoints[j].lat, response.shape[i].shapePoints[j].lon]);
}
}
// ルート形状をポリライン(ライム)で描画
var route = L.polyline(latlngs, {
color: 'lime',
weight: 6,
}).addTo(map);
// 地図表示をルート全体にフィット
map.fitBounds(route.getBounds());
}
With the implementation so far, the following process has been completed: "Click on the map → Execute the route search API → Draw the route line".

summary
In this article, we introduced simple methods for executing MapFan API 's route search API and for rendering the route search results in Leaflet.
The route search API offers a wide range of other route search criteria, and it can also handle things like changing the color of roads according to their type, similar to MapFan. I'd like to introduce those techniques on another occasion.
For inquiries about GeoTechnologies, Inc.' map API MapFan API please contact us here.
[For Developers] Download the MapFan API specification here.
Inquiries regarding services for corporate clients
For inquiries, please click here.


