Định tuyến

eKMap Platform Navigation API giúp bạn dễ dàng sử dụng các Rest API về truy vấn một cách nhanh chóng và dễ dàng nhất, bao gồm:

  • Dịch vụ tìm tuyến đường nhanh và tính khoảng cách, thời gian di chuyển giữa các điểm - Routing API
  • Dịch vụ tính toán vùng di chuyển - Isochrones API
  • Dịch vụ tính khoảng cách quãng đường, thời gian di chuyển - Matrix API
  • Dịch vụ tìm điểm gần nhất trên mạng lưới đường giao thông - Nearest API
  • Dịch vụ khớp các điểm dữ liệu xây dựng lại quỹ đạo di chuyển - Nearest API
  • Dịch vụ tìm kiếm lộ trình tối ưu nhất cho một đội phương tiện tới một tập hợp các điểm đến - Vehicle Routing Problem (VRP) API
  • Dịch vụ tìm, tính khoảng cách, thời gian di chuyển tối ưu nhất cho một phương tiện tới một tập hợp các điểm đến - Traveling Salesman Problem (TSP) API

Route API

Dịch vụ cung cấp thông tin tìm tuyến đường nhanh và cung cấp khoảng cách, thời gian của tuyến đường nhanh nhất giữa tất cả các tọa độ được cung cấp

new ekmapplf.service.Routing(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ
profilestringLoại phương thức vận chuyển giữa các điểm. Có các kiểu vận chuyển như car, bicycle hoặc foot

Methods

Phương thứcGiá trị trả vềMô tả
setProfile(profile: string)Hàm cập nhật loại phương tiện di chuyển giữa các tọa độ. Giá trị nhận vào car, bicycle hoặc foot
setCoordinates(coordinates: string)Hàm cập nhật danh sách các cặp tọa độ của các điểm. Giá trị ví dụ 105.809,21.0127;105.80925,21.0131
getRoute(parameter: object, callback: Function)Hàm lấy thông tin tuyến đường nhanh nhất dựa theo loại phương tiện, danh sách các cặp tọa độ các điểm và parameter của dịch vụ Direction API
getTable(parameter: object, callback: Function)Hàm lấy thông tin khoảng thời gian của tuyến đường nhanh nhất giữa tất cả các cặp tọa độ theo tham số parameter đầu vào

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel='stylesheet' />
<title>Tìm đường đi giữa hai điểm</title>
<style>
.map {
width: 100%;
height: calc(100vh - 15px);
}
body {
overflow: hidden;
}
</style>
</head>
<body>
<div id="map">
<div
style="
position: absolute;
z-index: 1;
left: 10px;
top: 10px;
background-color: white;
padding: 5px 10px;
font-size: 1rem;
"
> Click trên bản đồ để xác định điểm tìm đường
</div>
</div>
<script>
var apiKey = 'YOUR_API_KEY' // "Nhập khóa API eKMap Platform của bạn"
var map = new maplibregl.Map({
container: 'map',
center: [105, 17],
zoom: 4
});
/* Bản đồ nền */
var basemap = new ekmapplf.VectorBaseMap('OSM:Bright', apiKey);
basemap.addTo(map);
//khởi tạo
var routing= new ekmapplf.service.Routing({
apiKey: apiKey,
profile: 'foot'
});
//ví dụ click trên bản đồ để xác định 2 điểm
var startPoint, endPoint, markerStart, markerEnd;
map.on('click', function (evt) {
if (!startPoint) startPoint = evt.lngLat;
if (!endPoint) endPoint = evt.lngLat;
else {
startPoint = endPoint;
endPoint = evt.lngLat;
}
if (!markerStart)
markerStart = new maplibregl.Marker()
.setLngLat(startPoint.toArray())
.addTo(map);
else markerStart.setLngLat(startPoint.toArray());
routing();
});
//gọi dịch vụ tìm đường
function routing() {
if (startPoint && endPoint && startPoint != endPoint) {
if (!markerEnd)
markerEnd = new maplibregl.Marker({ color: 'red' })
.setLngLat(endPoint.toArray())
.addTo(map);
else markerEnd.setLngLat(endPoint.toArray());
var coordinates = startPoint.toArray().toString() + ";" + endPoint.toArray().toString();
routingService.setCoordinates(coordinates); // thiết lập cặp điểm đầu và cuối
//xác định được đường đi
var paramRoute = {
overview: "full",
alternatives: false,
steps: false,
geometries: "geojson",
}
//xác định được đường đi
routingService.getRoute(paramRoute, function(error, data){
var featureData = {
'type': 'Feature',
'properties': {},
'geometry': data.routes[0].geometry
};
if (map.getSource('route')) {
map.getSource('route').setData(featureData);
} else {
map.addSource('route', {
'type': 'geojson',
'data': featureData
});
map.addLayer({
'id': 'route',
'type': 'line',
'source': 'route',
'paint': {
'line-color': '#4882c5',
'line-width': 7
}
});
}
});
}
}
</script>
</body>
</html>

Isochrones API

Dịch vụ tính toán vùng di chuyển theo từng loại phương tiện trong phạm vi thời gian hoặc khoảng cách

new ekmapplf.service.Isochrones(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ
profilestringLoại phương tiện vận chuyển trong vùng. Có các kiểu phương tiện như driving-car, cycling-regular hoặc foot-walking

Methods

Phương thứcGiá trịMô tả
setProfile(profile: string)driving-car, cycling-regular hoặc foot-walkingHàm đặt giá trị loại phương tiện vận chuyển trong vùng.
setLocations([lngLat: array])Hàm cập nhật tọa độ của các điểm muốn tìm vùng di chuyển
setRange(range: array)Hàm đặt giá trị phạm vi tối đa vùng di chuyển với thời gian là giây và khoảng cách là meter
setInterval(interval: number)Hàm đặt khoảng cách của các vùng di chuyển hoặc cách đều nhau.
setRangeType(type: string)time(mặc định), distanceHàm đặt giá trị phân tích phạm vi tối đa của vùng di chuyển.
setUnits(range: string)m (mặc định), km, mileHàm đặt đơn vị khoảng cách (Chỉ khi setRangeType() có giá trị distance)
run(callback: Function)Hàm gọi dịch vụ lấy thông tin vùng di chuyển dựa theo loại phương tiện và phạm vi về thời gian hoặc khoảng cách

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Xác định vùng di chuyển</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; background:#ddd;}
</style>
</head>
<body>
<div id="map">
<div style="
position: absolute;
z-index: 1;
left: 10px;
top: 5px;
background-color: white;
padding: 5px 10px;
font-size: 1rem;"
>
<div style="width: 100%;">
Khoảng cách di chuyển bằng
<select id="profile">
<option value="driving-car">ô tô</option>
<option value="cycling-regular">xe đạp</option>
<option value="foot-walking">đi bộ</option>
</select>
trong
<input id="range" type="number" style="width: 50px;" value="500">
<select id="range_type">
<option value="distance">mét</option>
<option value="time">giây</option>
</select>
</div>
<button style="height: 25px; width: 60px; margin-top: 5px;" id="submit">Tìm</button>
</div>
</div>
<script>
var map = new maplibregl.Map({
container: 'map',
center: [105.84104, 21.02421],
zoom: 14
});
var basemap = new ekmapplf.VectorBaseMap('OSM:Standard', '{YOUR_API_KEY}').addTo(map);
var marker = new maplibregl.Marker()
.setLngLat([105.84104, 21.02421])
.addTo(map);
document.getElementById('submit').addEventListener('click',function(){
var range_type = document.getElementById('range_type').value;
var profile = document.getElementById('profile').value;
var range = document.getElementById('range').value;
var Isochrones= new ekmapplf.service.Isochrones({
apiKey: '{YOUR_API_KEY}',
profile: profile
});
IsochronesService.setLocations([
[105.84104, 21.02421]
]);
IsochronesService.setRange(
[range]
);
IsochronesService.setRangeType(range_type);
IsochronesService.run(function (error, response) {
console.log(response);
map.fitBounds(response.bbox,{
padding: {top: 75, bottom:25, left: 20, right: 20}
});
if (map.getSource('isochrone')) {
map.getSource('isochrone').setData(response.features[0]);
} else {
map.addSource('isochrone',{
type: 'geojson',
data: response.features[0]
});
}
if ( map.getLayer('isochrone-layer') ) map.removeLayer('isochrone-layer');
map.addLayer({
'id': 'isochrone-layer',
'type': 'fill',
'source': 'isochrone',
'layout': {},
'paint': {
'fill-color': '#eda43e',
'fill-opacity': 0.5
}
});
})
})
</script>
</body>
</html>

Matrix API

Dịch vụ tính khoảng cách khoảng cách, thời gian chuyển giữa các điểm

new ekmapplf.service.Matrix(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ
profilestringLoại phương tiện vận chuyển trong vùng. Có các kiểu phương tiện như driving-car, cycling-regular hoặc foot-walking

Methods

Phương thứcGiá trịMô tả
setProfile(profile: string)driving-car, cycling-regular hoặc foot-walkingHàm đặt giá trị loại phương tiện vận chuyển trong vùng.
setLocations([lngLat: array])Hàm cập nhật các cặp tọa độ bao gồm kinh độ, vĩ độ của các điểm
setMetrics(range: array)duration (mặc định),distanceHàm đặt giá trị phép phân tích trả về với thời gian là giây và khoảng cách là mặc định là meter
setResolveLocations(type: boolean)false (mặc định), trueHàm đặt tùy chọn có trả về chi tiết các vị trí hay không. Nếu giá trị tham số là true, thì mọi phần tử trong destinationssources sẽ chứa phần tử name xác định tên của con phố gần nhất.
setUnits(range: string)m (mặc định), km, mileHàm đặt đơn vị khoảng cách.
run(callback: Function)Hàm gọi dịch vụ phân tích dựa theo loại phương tiện và giá trị về thời gian hay khoảng cách

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Ma trận tính khoảng cách khoảng cách, thời gian</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; background:#ddd;}
</style>
</head>
<body>
<div style="display:flex;height: 100vh;">
<div style="width: 30%; padding: 16px 0px; background: #fff; box-shadow: 2px 0px 7px -2px rgba(0,0,0,0.31); z-index: 1;">
<ul id="result" style="margin:0; padding-inline-start: 20px;"></ul>
</div>
<div id="map"></div>
<script>
var map = new maplibregl.Map({
container: 'map',
center: [105.816, 21.038],
zoom: 13
});
var basemap = new ekmapplf.VectorBaseMap('OSM:Standard', '{YOUR_API_KEY}').addTo(map);
var markerA = new maplibregl.Marker({ color: '#FF2340' })
.setLngLat([105.83641, 21.04303])
.setPopup(new maplibregl.Popup({'closeOnMove': false, 'closeOnClick': false}).setHTML("Vị trí A"))
.addTo(map)
.togglePopup();
var markerB = new maplibregl.Marker({ color: '#C014DF' })
.setLngLat([105.84888, 21.03171])
.setPopup(new maplibregl.Popup({'closeOnMove': false, 'closeOnClick': false}).setHTML("Vị trí B"))
.addTo(map)
.togglePopup();
var markerC = new maplibregl.Marker({ color: '#3EBC5D' })
.setLngLat([105.804159, 21.0304848])
.setPopup(new maplibregl.Popup({'closeOnMove': false, 'closeOnClick': false}).setHTML("Vị trí C"))
.addTo(map)
.togglePopup();
map.on('load',function() {
var Matrix= new ekmapplf.service.Matrix({
apiKey: '{YOUR_API_KEY}',
profile: 'driving-car'
});
MatrixService.setLocations([
[105.83641, 21.04303],
[105.84888, 21.03171],
[105.804159, 21.0304848]
]);
MatrixService.setMetrics([
"duration",
"distance"
]);
MatrixService.setUnits("km");
MatrixService.run(function (error, response) {
console.log(response);
document.getElementById('result').innerHTML=
'<li> Vị trí A → Vị trí B: '+ response.distances[0][1] +' km ('+ response.durations[0][1] +'s)</li>' +
'<li> Vị trí A → Vị trí C: '+ response.distances[0][2] +' km ('+ response.durations[0][2] +'s)</li>' +
'<li> Vị trí B → Vị trí A: '+ response.distances[1][0] +' km ('+ response.durations[1][0] +'s)</li>' +
'<li> Vị trí B → Vị trí C: '+ response.distances[1][2] +' km ('+ response.durations[1][2] +'s)</li>' +
'<li> Vị trí C → Vị trí A: '+ response.distances[2][0] +' km ('+ response.durations[2][0] +'s)</li>' +
'<li> Vị trí C → Vị trí B: '+ response.distances[2][1] +' km ('+ response.durations[2][1] +'s)</li>';
})
});
</script>
</body>
</html>

Nearest API

Dịch vụ tìm điểm gần nhất trên mạng lưới đường giao thông từ một vị trí địa lý bất kỳ.

new ekmapplf.service.Nearest(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ
profilestringLoại phương thức vận chuyển giữa các điểm. Có các kiểu vận chuyển như car, bicycle hoặc foot

Methods

Phương thứcGiá trịMô tả
setProfile(profile: string)car, bicycle hoặc foot
Ví dụ:car
Hàm cập nhật loại phương tiện di chuyển để tìm đường giao thông phù hợp .
setCoordinates(coordinates: string){longitude},{latitude}
Ví dụ:105.809,21.0127
Hàm cập nhật tọa độ của điểm.
run( callback: Function)Hàm lấy thông tin đểm gần nhất trên mạng lưới đường giao thông nhanh nhất dựa theo loại phương tiện, tọa độ các điểm

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Điểm gần nhất trên đường giao thông</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; background:#ddd;}
</style>
</head>
<body>
<div id="map">
<div
style="
position: absolute;
z-index: 1;
left: 10px;
top: 10px;
background-color: white;
padding: 5px 10px;
font-size: 1rem;
"
>Click trên bản đồ để tìm điểm gần nhất trên đường giao thông
</div>
</div>
<script>
var map = new maplibregl.Map({
container: 'map',
center: [105.85243, 21.02884],
zoom: 15.5
});
var basemap = new ekmapplf.VectorBaseMap('OSM:Bright', '{YOUR_API_KEY}');
basemap.addTo(map);
var Nearest= new ekmapplf.service.Nearest({
apiKey: '{YOUR_API_KEY}',
profile: 'car'
});
var Point, marker;
map.on('click', function (evt) {
Point = evt.lngLat;
if (!marker) marker = new maplibregl.Marker().setLngLat(Point.toArray()).addTo(map);
else marker.setLngLat(Point.toArray());
GetNearest();
});
//gọi dịch vụ
function GetNearest() {
if (Point) {
var coordinates = Point.toArray().toString();
NearestService.setCoordinates(coordinates);
NearestService.run(function (error, data) {
console.log(data);
if (data.code == 'Ok') {
var featureCollection = {
type: 'FeatureCollection',
features: []
};
var featurePoint = {
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: data.waypoints[0].location
}
};
featureCollection.features.push(featurePoint);
var featureLine = {
type: 'Feature',
properties: {
distance: data.waypoints[0].distance.toFixed(3) + ' m',
},
geometry: {
type: 'LineString',
coordinates: [Point.toArray(), data.waypoints[0].location]
}
};
featureCollection.features.push(featureLine);
console.log(featureCollection)
if (map.getSource('Nearest')) {
map.getSource('Nearest').setData(featureCollection);
} else {
map.addSource('Nearest', {
type: 'geojson',
data: featureCollection
});
map.addLayer({
id: 'Nearest-point',
type: 'circle',
source: 'Nearest',
paint: {
'circle-color': '#ffc500',
'circle-radius': 5
},
'filter': ['in', '$type', 'Point']
});
map.addLayer({
id: 'Nearest-line',
type: 'line',
source: 'Nearest',
paint: {
"line-color": "#0000ff",
"line-width": 2,
"line-dasharray": [2.6, 2.6]
},
filter: ['in', '$type', 'LineString']
},'Nearest-point'
);
map.addLayer({
id: 'Nearest-label',
type: 'symbol',
source: 'Nearest',
layout: {
'symbol-placement': 'line-center',
'text-field': '{distance}'
},
paint: {
'text-translate': [0, -10],
'text-color': 'red',
'text-halo-color': '#ffffff',
'text-halo-width': 2
},
filter: ['in', '$type', 'LineString']
});
}
}
});
}
}
</script>
</body>
</html>

Snap to Roads API

Dịch vụ cung cấp tính năng cho phép khớp các điểm dữ liệu GPS (thường không nằm trên đường giao thông do luôn có sai số với dữ liệu GPS) vào mạng lưới đường để xác định, xây dựng lại quỹ đạo di chuyển hoặc đường đi tương ứng trên bản đồ.

new ekmapplf.service.SnaptoRoads(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ
profilestringLoại phương thức vận chuyển giữa các điểm. Có các kiểu vận chuyển như car, bicycle hoặc foot

Methods

Phương thứcGiá trịMô tả
setProfile(profile: string)car, bicycle hoặc foot
Ví dụ:car
Hàm cập nhật loại phương tiện di chuyển.
getProfile()Hàm trả về giá trị loại phương tiện di chuyển.
setCoordinates(coordinates: string){longitude},{latitude};{longitude},{latitude};...
Ví dụ: 105.809,21.0127;105.80925,21.0131
Hàm cập nhật mảng cặp tọa độ bao gồm kinh độ, vĩ độ. Ít nhất có hai cặp tọa độ.
getCoordinates()Hàm trả về tọa độ của điểm.
run( parameter: object,callback: Function)Hàm lấy thông tin đểm gần nhất trên mạng lưới đường giao thông nhanh nhất dựa theo loại phương tiện, tọa độ các điểm và parameter của dịch vụ Snap to Roads API

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Snap to Roads</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; background:#ddd;}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new maplibregl.Map({
container: 'map',
center: [105.78787, 21.04602],
zoom: 15.5
});
var basemap = new ekmapplf.VectorBaseMap('OSM:Bright', '{YOUR_API_KEY}');
basemap.addTo(map);
var SnaptoRoads= new ekmapplf.service.SnaptoRoads({
apiKey: '{YOUR_API_KEY}',
profile: 'car'
});
//danh sach toa do
var arrCoord = [
[105.78330175145024, 21.04614441795959],
[105.78538121305992, 21.04616450160572],
[105.78787668451292, 21.04602061221084],
[105.79028419291438, 21.04602061221084],
[105.79281262639466, 21.046143730461935],
];
map.on('load', function () {
var featureCollection = {
type: 'FeatureCollection',
features: []
};
arrCoord.forEach((coordinate) => {
var featurePoint = {
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: coordinate
}
};
featureCollection.features.push(featurePoint);
});
map.addSource('coordinate', {
type: 'geojson',
data: featureCollection
});
map.addLayer({
id: 'Nearest-point',
type: 'circle',
source: 'coordinate',
paint: {
'circle-color': '#0000ff',
'circle-radius': 5
},
filter: ['in', '$type', 'Point']
});
getRoad()
});
//gọi dịch vụ
function getRoad() {
var coordinates = '';
arrCoord.forEach((coord) => {
if(coordinates) coordinates = coordinates + ';' + coord.toString();
else coordinates = coord.toString();
})
SnaptoRoadsService.setCoordinates(coordinates);
var paramRoute = {
overview: 'full',
geometries: 'geojson'
};
SnaptoRoadsService.run(paramRoute, function (error, data) {
console.log(data);
if (data.code == 'Ok') {
var featureData = {
type: 'Feature',
properties: {},
geometry: data.matchings[0].geometry
};
if (map.getSource('route')) {
map.getSource('route').setData(featureData);
} else {
map.addSource('route', {
type: 'geojson',
data: featureData
});
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: {
'line-color': '#4882c5',
'line-width': 5,
'line-opacity': 0.6
}
});
}
}
});
}
</script>
</body>
</html>

Vehicle Routing Problem (VRP) API

Dịch vụ tìm các tuyến đường hiệu quả nhất cho một đội phương tiện di chuyển đến một tập hợp các điểm đến và quay trở lại điểm xuất phát trong khi đáp ứng một số diều kiện ràng buộc, chẳng hạn như công suất và thời gian.

new ekmapplf.service.VRP(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ

Methods

Phương thứcGiá trịMô tả
setJobs(array)Mảng các đối tượng job mô tả các địa điểm cần tới.Hàm gán các đối tượng job.
getJobs()Hàm trả về giá trị các đối tượng job.
setVehicles(profile: string)Mảng các đối tượng vehicle mô tả các phương tiện có sẵn.Hàm gán các đối tượng vehicle.
getVehicles()Hàm trả về giá trị các đối tượng vehicle.
setShipments(array)Mảng các đối tượng shipment mô tả công việc nhận và giao hàng.Hàm gán các đối tượng shipment
getShipments()Hàm trả về giá trị các đối tượng shipment.
run(callback: Function)Hàm lấy tìm các tuyến đường hiệu quả nhất dựa theo loại phương tiện, tọa đồ công việc cũng như các điều kiện ràng buộc khác

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tìm lộ trình hiệu quả nhất</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; background:#ddd;}
</style>
</head>
<body>
<div style="display: flex; height: 100vh">
<div class="Panel" style=" position: absolute; z-index: 1; width: 250px; padding: 15px; margin: 10px; background: #fff;">
<h3 style="margin: 5px 0;">Lộ trình tối ưu</h3>
<span style="width: 100%;" id="duration"></span>
<ol style="margin:0; padding-inline-start: 20px;" id="steps"></ol>
</div>
<div id="map"></div>
</div>
<script>
var map = new maplibregl.Map({
container: 'map',
center: [105.77909, 21.03941],
zoom: 11.5
});
var baseMap = new ekmapplf.VectorBaseMap('OSM:Bright', '{YOUR_API_KEY}').addTo(map);
var featureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
type: 'vehicle',
description: 'Điểm bắt đầu/ kết thúc'
},
geometry: {
type: 'Point',
coordinates: [105.81665, 21.00113]
}
},
{
type: 'Feature',
properties: {
type: 'job',
description: 'Công việc 1'
},
geometry: {
type: 'Point',
coordinates: [105.91135, 21.04782]
}
},
{
type: 'Feature',
properties: {
type: 'job',
description: 'Công việc 2'
},
geometry: {
type: 'Point',
coordinates: [105.75535, 21.00520]
}
},
{
type: 'Feature',
properties: {
type: 'job',
description: 'Công việc 3'
},
geometry: {
type: 'Point',
coordinates: [105.77678, 21.05333]
}
}
]
};
map.on('load', function () {
map.addSource('points', {
type: 'geojson',
data: featureCollection
});
map.addLayer({
id: 'points-layer',
type: 'circle',
source: 'points',
paint: {
'circle-color': ['case', ['all', ['==', ['get', 'type'], 'vehicle']], '#FF2340', '#39AA54'],
'circle-radius': 8,
}
});
featureCollection.features.forEach(feature => {
var coordinates = feature.geometry.coordinates;
var description = feature.properties.description;
var popup = new maplibregl.Popup({
offset: {'bottom': [0, -10]},
closeButton: false,
closeOnClick: false
});
popup.setLngLat(coordinates).setHTML(description).addTo(map);
})
});
var VRPService = new ekmapplf.service.VRP({
apiKey: '{YOUR_API_KEY}'
});
var jobs = [
{
id: 1,
description: 'Công việc 1',
location: [105.91135, 21.04782],
},
{
id: 2,
description: 'Công việc 2',
location: [105.75535, 21.00520],
},
{
id: 3,
description: 'Công việc 3',
location: [105.77678, 21.05333],
}
];
var vehicles = [
{
id: 0,
start: [105.81665, 21.00113],
end: [105.81665, 21.00113],
profile: 'driving-car'
}
];
VRPService.setJobs(jobs);
VRPService.setVehicles(vehicles);
VRPService.run(function (error, response) {
console.log(response);
document.getElementById('duration').innerHTML = 'Thời gian di chuyển: <b>' + (response.routes[0].duration / 60).toFixed() + ' phút </b>';
var html = '';
response.routes[0].steps.forEach(step => {
if (step.type == 'job'){
html += '<li>' + step.type + ': ' + step.description + '</li>'
} else {
html += '<li>' + step.type + '</li>'
}
});
document.getElementById('steps').innerHTML = html;
});
</script>
</body>
</html>

Traveling Salesman Problem (TSP) API

Dịch vụ tìm điểm gần nhất trên mạng lưới đường giao thông từ một vị trí địa lý bất kỳ.

new ekmapplf.service.TSP(Options: Object)

Parameters

 options(Object)

Thuộc tínhKiểuMô tả
apiKeystringKey này sẽ được đưa vào trong tất cả các yêu cầu của dịch vụ
profilestringLoại phương thức vận chuyển giữa các điểm. Có các kiểu vận chuyển như car, bicycle hoặc foot

Methods

Phương thứcGiá trịMô tả
setProfile(profile: string)car, bicycle hoặc foot
Ví dụ:car
Hàm cập nhật loại phương tiện di chuyển để tìm đường giao thông phù hợp .
getProfile()Hàm trả về giá trị loại phương tiện di chuyển.
setCoordinates(coordinates: string){longitude},{latitude};{longitude},{latitude};...
Ví dụ:105.809,21.0127;105.80925,21.0131
Hàm cập nhật tọa độ của các điểm.
getCoordinates()Hàm trả về tọa độ của csác điểm.
run(parameter: object, callback: Function)Hàm lấy thông tin tuyến đường tối ưu nhất dựa theo loại phương tiện, tọa độ các điểm và parameter của dịch vụ Traveling Salesman Problem (TSP) API

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tìm và hiển thị tuyến đường tối ưu nhất</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.js"></script>
<link href="https://files.ekgis.vn/sdks/v2.0.0/ekmap-platform.min.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; background:#ddd;}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new maplibregl.Map({
container: 'map',
center: [105.81909, 21.03941],
zoom: 11
});
var baseMap = new ekmapplf.VectorBaseMap('OSM:Bright', '{YOUR_API_KEY}').addTo(map);
var featureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
type: 'vehicle',
description: 'Điểm bắt đầu/ kết thúc'
},
geometry: {
type: 'Point',
coordinates: [105.81665, 21.00113],
}
},
{
type: 'Feature',
properties: {
type: 'job',
description: 'Công việc 1'
},
geometry: {
type: 'Point',
coordinates: [105.91135, 21.04782],
}
},
{
type: 'Feature',
properties: {
type: 'job',
description: 'Công việc 2'
},
geometry: {
type: 'Point',
coordinates: [105.75535, 21.00520],
}
},
{
type: 'Feature',
properties: {
type: 'job',
description: 'Công việc 3'
},
geometry: {
type: 'Point',
coordinates: [105.77678, 21.05333],
}
}
]
};
map.on('load', function () {
map.addSource('points', {
type: 'geojson',
data: featureCollection
});
map.addLayer({
id: 'points-layer',
type: 'circle',
source: 'points',
paint: {
'circle-color': ['case', ['all', ['==', ['get', 'type'], 'vehicle']], '#FF2340', '#39AA54'],
'circle-radius': 8,
}
});
featureCollection.features.forEach(feature => {
var coordinates = feature.geometry.coordinates;
var description = feature.properties.description;
var popup = new maplibregl.Popup({
offset: {'bottom': [0, -10]},
closeButton: false,
closeOnClick: false
});
popup.setLngLat(coordinates).setHTML(description).addTo(map);
})
getRoute()
});
function getRoute() {
var TSPService = new ekmapplf.service.TSP({
apiKey: '{YOUR_API_KEY}',
profile: 'car'
});
var arrCoord = [
[105.81665, 21.00113],
[105.91135, 21.04782],
[105.75535, 21.00520],
[105.77678, 21.05333],
];
var coordinates = '';
arrCoord.forEach((coord) => {
if (coordinates) coordinates = coordinates + ';' + coord.toString();
else coordinates = coord.toString();
})
TSPService.setCoordinates(coordinates);
var paramRoute = {
overview: 'full',
geometries: 'geojson',
};
TSPService.run(paramRoute, function (error, data) {
console.log(data);
if (data.code == 'Ok') {
var featureData = {
type: 'Feature',
properties: {},
geometry: data.trips[0].geometry
};
if (map.getSource('route')) {
map.getSource('route').setData(featureData);
} else {
map.addSource('route', {
type: 'geojson',
data: featureData
});
map.addLayer({
id: 'route',
type: 'line',
source: 'route',
paint: {
'line-color': '#4882c5',
'line-width': 5,
'line-opacity': 0.9
}
});
}
}
});
}
</script>
</body>
</html>