เอกสาร MapScale
แพลตฟอร์มและ SDK

ย้ายจาก Longdo Map มา MapScale

ย้ายการใช้งาน Longdo Map JavaScript API รวมถึง web service สำหรับค้นหา reverse geocoding และเส้นทาง ไปใช้ MapScale และ MapLibre GL JS สำหรับนักพัฒนาไทย

ปานกลาง

สิ่งที่เปลี่ยน

ทั้งสองบริการเน้นประเทศไทย Longdo มี object API ชื่อ longdo.Map และ REST web service ของตนเอง ส่วน MapScale ให้ style ที่ใช้กับ MapLibre ได้และ REST API พิกัดเปลี่ยนจากออบเจ็กต์ { lon, lat } เป็นอาร์เรย์ [longitude, latitude] Publishable key (pk_) ใช้ในเบราว์เซอร์และแอป ส่วน secret key (sk_) ใช้บนเซิร์ฟเวอร์เท่านั้นสำหรับ geocode, reverse, route, matrix และ isochrone ตัวอย่างฝั่ง Longdo อ้างอิงเอกสาร JavaScript v2 โปรดตรวจชื่อกับเวอร์ชันที่คุณใช้

สิ่งที่เปลี่ยน
Longdo Map JavaScript API            MapScale
new longdo.Map({ placeholder })      new maplibregl.Map({ container, style, center, zoom })
{ lon, lat } objects                 [lng, lat] arrays (GeoJSON order)
map.location(...), map.zoom(...)     center / zoom options, map.flyTo(...)
longdo.Marker + Overlays.add         maplibregl.Marker + maplibregl.Popup
search.longdo.com .../suggest        GET /v1/autocomplete?q=   (publishable key)
search.longdo.com .../search         GET /v1/geocode?q=        (secret key, server)
api.longdo.com/map/services/address  GET /v1/reverse?point=lng,lat   (secret key, server)
RouteService .../geojson/route       GET /v1/route?coordinates=lng,lat;lng,lat&profile=driving
locale=th                            lang=th

เปลี่ยนแผนที่

แทน longdo.Map ด้วยแผนที่ MapLibre ที่โหลด style ของ MapScale ใช้ lang=th หรือ lang=en ที่ style URL เพื่อกำหนดภาษาของป้ายชื่อ

เปลี่ยนแผนที่
// Before: Longdo Map JavaScript API (v2 style, from the Longdo documentation)
// <script src="https://api.longdo.com/map/?key=YOUR_LONGDO_KEY"></script>
const map = new longdo.Map({ placeholder: document.getElementById("map") })
map.location({ lon: 100.5018, lat: 13.7563 }, true)
map.zoom(11, true)

// After: MapScale style rendered by MapLibre GL JS
const key = "pk_test_your_key_here"
const map = new maplibregl.Map({
	container: "map",
	style: `https://api.mapscale.io/styles/v1/streets?key=${key}&lang=th`,
	center: [100.5018, 13.7563], // [lng, lat]
	zoom: 11,
})

เปลี่ยน marker

longdo.Marker ที่มี title และ detail เปลี่ยนเป็น marker ของ MapLibre พร้อม popup ใช้ setText สำหรับข้อความธรรมดา

เปลี่ยน marker
// Before: Longdo Map JavaScript API
const marker = new longdo.Marker(
	{ lon: 100.5018, lat: 13.7563 },
	{ title: "Bangkok", detail: "Example marker" }
)
map.Overlays.add(marker)

// After: MapLibre GL JS
const popup = new maplibregl.Popup().setText("Bangkok: example marker")
new maplibregl.Marker().setLngLat([100.5018, 13.7563]).setPopup(popup).addTo(map)

Web service suggest และ search ของ Longdo ตรงกับ autocomplete (publishable key ใช้ในเบราว์เซอร์ได้) และ geocode (secret key ใช้บนเซิร์ฟเวอร์เท่านั้น) ส่ง place_id จาก prediction ไปที่ GET /v1/place/{id} เพื่อรับพิกัด

เปลี่ยนการค้นหาสถานที่
// Before: Longdo web service (key in the query string)
// https://search.longdo.com/mapsearch/json/suggest?keyword=siam&limit=10&key=YOUR_LONGDO_KEY
// https://search.longdo.com/mapsearch/json/search?keyword=siam&locale=th&limit=20&key=YOUR_LONGDO_KEY

// After, suggestions in the browser (publishable key):
const params = new URLSearchParams({ q: "siam", lang: "th", key: "pk_test_your_key_here" })
const suggestResponse = await fetch(`https://api.mapscale.io/v1/autocomplete?${params}`)
if (!suggestResponse.ok) throw new Error(`MapScale autocomplete failed: ${suggestResponse.status}`)
const { predictions } = await suggestResponse.json()
// predictions[0].place_id -> GET /v1/place/{id} for latitude and longitude

// After, full geocoding on your server (secret key):
const serverParams = new URLSearchParams({ q: "siam", lang: "th", limit: "20" })
const geocodeResponse = await fetch(`https://api.mapscale.io/v1/geocode?${serverParams}`, {
	headers: { "X-MapScale-Key": process.env.MAPSCALE_SECRET_KEY },
})
if (!geocodeResponse.ok) throw new Error(`MapScale geocode failed: ${geocodeResponse.status}`)
const collection = await geocodeResponse.json()

เปลี่ยน reverse geocoding

Reverse geocoding ทำงานบนเซิร์ฟเวอร์ด้วย secret key รหัสตัวเลขจาก endpoint admin-areas เป็นรหัสของ MapScale ไม่ใช่ PCODE หากต้องเชื่อมข้อมูลด้วยรหัสเขตปกครองของไทย ให้ใช้ตารางอ้างอิง PCODE เพื่อค้นรหัสของแต่ละพื้นที่

เปลี่ยน reverse geocoding
// Before: Longdo address web service
// https://api.longdo.com/map/services/address?lon=100.53726&lat=13.72427&key=YOUR_LONGDO_KEY

// After: your server, secret key
const params = new URLSearchParams({ point: "100.53726,13.72427", lang: "th" }) // lng,lat
const response = await fetch(`https://api.mapscale.io/v1/reverse?${params}`, {
	headers: { "X-MapScale-Key": process.env.MAPSCALE_SECRET_KEY },
})
if (!response.ok) throw new Error(`MapScale reverse failed: ${response.status}`)
const collection = await response.json()
const place = collection.features[0].properties // formattedAddress, subdistrict, district, province, postalCode

// Administrative areas only:
// GET /v1/admin-areas/reverse?point=100.53726,13.72427&lang=th
// returns subdistrict, district, province and country as { id, name }.

เปลี่ยนเส้นทาง

พารามิเตอร์ flon, flat, tlon และ tlat ของ Longdo เปลี่ยนเป็นรายการ coordinates เดียว Route API ระบุโปรไฟล์ driving, walking และ cycling กับ flag alternatives แต่ไม่ได้ระบุตัวเลือก mode (การจราจร) และ type (ประเภทยานพาหนะ) แบบ Longdo จึงควรทดสอบเส้นทางที่คุณต้องพึ่งพา

เปลี่ยนเส้นทาง
// Before: Longdo RouteService (GeoJSON)
// https://api.longdo.com/RouteService/geojson/route?flon=100.53726&flat=13.72427&tlon=100.54061&tlat=13.72669&locale=th&key=YOUR_LONGDO_KEY

// After: your server, secret key
const params = new URLSearchParams({
	coordinates: "100.53726,13.72427;100.54061,13.72669", // from;to as lng,lat
	profile: "driving", // or walking, cycling
	lang: "th",
})
const response = await fetch(`https://api.mapscale.io/v1/route?${params}`, {
	headers: { "X-MapScale-Key": process.env.MAPSCALE_SECRET_KEY },
})
if (!response.ok) throw new Error(`MapScale route failed: ${response.status}`)
const { routes } = await response.json()
// routes[0].distance (meters), routes[0].duration (seconds), routes[0].geometry (LineString)

ตรวจสอบก่อนใช้งานจริง

แสดงข้อความ attribution ที่ได้จาก API และบนแผนที่ เปรียบเทียบผลลัพธ์กับที่อยู่ ชื่อสถานที่ และเส้นทางของคุณเอง เพราะ coverage และการจัดอันดับต่างกันในแต่ละบริการ เปิดใช้ทั้งสองบริการหลัง feature flag จนกว่าจะผ่านการตรวจ แล้วจึงถอด script และ key ของ Longdo

On this page

ตั้งค่า Analytics