MapScale Docs
Platforms & SDKs

Migrate from Google Maps Platform to MapScale

Map the Google Maps JavaScript API calls for map display, markers, autocomplete, geocoding and directions onto MapScale endpoints and MapLibre GL JS for Thailand projects.

Intermediate

What changes

MapScale renders with MapLibre GL JS, an open-source library, and covers Thailand; check fit before using it for other regions. Coordinates become [longitude, latitude] arrays instead of { lat, lng } objects. Publishable keys (pk_) are for browsers and apps and reach styles, tiles, autocomplete, place details and static maps. Secret keys (sk_) are server-only and are required for geocode, reverse, route, matrix and isochrone. Calls that need a secret key move from browser code to your own server.

What changes
Google Maps JavaScript API            MapScale
new google.maps.Map(el, options)     new maplibregl.Map({ container, style, center, zoom })
{ lat, lng } objects                 [lng, lat] arrays (GeoJSON order)
google.maps.Marker                   maplibregl.Marker + maplibregl.Popup
Geocoder.geocode({ address })        GET /v1/geocode?q=        (secret key, server)
Places Autocomplete                  GET /v1/autocomplete?q=   (publishable key)
Place details                        GET /v1/place/{id}        (publishable key)
Geocoder.geocode({ location })       GET /v1/reverse?point=lng,lat   (secret key, server)
DirectionsService.route(...)         GET /v1/route?coordinates=lng,lat;lng,lat&profile=driving
travelMode DRIVING / WALKING         profile=driving / walking
travelMode BICYCLING                 profile=cycling

Replace the map

Swap the Google map constructor for a MapLibre map that loads a MapScale style. Load maplibre-gl from npm or a CDN as shown in the Web JavaScript guide, and keep the key restricted to your site's origin.

Replace the map
// Before: Google Maps JavaScript API
const map = new google.maps.Map(document.getElementById("map"), {
	center: { lat: 13.7563, lng: 100.5018 },
	zoom: 11,
})

// 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,
})
map.addControl(new maplibregl.NavigationControl(), "top-right")

Replace markers and info windows

A Google marker and info window become a MapLibre marker with a popup. Use setText for plain text so user-supplied strings are never parsed as HTML.

Replace markers and info windows
// Before: Google Maps JavaScript API
const marker = new google.maps.Marker({
	position: { lat: 13.7563, lng: 100.5018 },
	map,
	title: "Bangkok",
})
const info = new google.maps.InfoWindow({ content: "Bangkok" })
marker.addListener("click", () => info.open({ map, anchor: marker }))

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

Replace Places Autocomplete

Autocomplete and place details work with a publishable key, so the search box can stay in the browser. Reuse one session_token from the first keystroke to the selected place.

Replace Places Autocomplete
// After: browser code with a publishable key. One session token covers the
// typing and the final place lookup.
const key = "pk_test_your_key_here"
const sessionToken = crypto.randomUUID()

async function suggest(text) {
	const params = new URLSearchParams({ q: text, lang: "th", session_token: sessionToken, key })
	const response = await fetch(`https://api.mapscale.io/v1/autocomplete?${params}`)
	if (!response.ok) throw new Error(`MapScale autocomplete failed: ${response.status}`)
	const { predictions } = await response.json()
	// Each prediction: place_id, description, structured_formatting.main_text / secondary_text
	return predictions
}

async function select(placeId) {
	const params = new URLSearchParams({ lang: "th", session_token: sessionToken, key })
	const response = await fetch(
		`https://api.mapscale.io/v1/place/${encodeURIComponent(placeId)}?${params}`
	)
	if (!response.ok) throw new Error(`MapScale place lookup failed: ${response.status}`)
	const place = await response.json() // latitude, longitude, formattedAddress, province, district ...
	map.flyTo({ center: [place.longitude, place.latitude], zoom: 16 })
}

Replace the Geocoder

Forward geocoding needs a secret key, so call it from your server and return only what the page needs. Reverse geocoding follows the same pattern with GET /v1/reverse and a point parameter in lng,lat order.

Replace the Geocoder
// Before: Google Maps JavaScript API Geocoder (browser)
// const { results } = await new google.maps.Geocoder().geocode({ address: "Siam Paragon" })
// const { lat, lng } = results[0].geometry.location

// After: your server, secret key from the environment (never ship sk_ to a browser)
const params = new URLSearchParams({ q: "Siam Paragon", lang: "th", limit: "5" })
const response = await fetch(`https://api.mapscale.io/v1/geocode?${params}`, {
	headers: { "X-MapScale-Key": process.env.MAPSCALE_SECRET_KEY },
})
if (!response.ok) throw new Error(`MapScale geocode failed: ${response.status}`)
const collection = await response.json() // GeoJSON FeatureCollection
const [lng, lat] = collection.features[0].geometry.coordinates
const address = collection.features[0].place_name
// Show collection.attribution wherever you display the result.

Replace Directions

The route API accepts two or more waypoints and the profiles driving, walking and cycling. Transit directions are not part of the documented route API, so keep another source if you depend on them. Use POST /v1/matrix for many-to-many travel times.

Replace Directions
// Before: Google Maps JavaScript API DirectionsService (browser)
// directionsService.route({ origin, destination, travelMode: "DRIVING" })

// After: your server calls /v1/route with a secret key and returns the geometry
const params = new URLSearchParams({
	coordinates: "100.5018,13.7563;100.5348,13.7466", // lng,lat;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()
const route = routes[0] // distance (meters), duration (seconds), geometry (GeoJSON LineString)

// In the browser, draw the geometry your server returned:
map.addSource("route", { type: "geojson", data: { type: "Feature", properties: {}, geometry: route.geometry } })
map.addLayer({
	id: "route",
	type: "line",
	source: "route",
	paint: { "line-color": "#1d4ed8", "line-width": 5 },
})

Check before you ship

Display the attribution string returned in API responses and on the map, as described in the attribution guide. Compare results for your own addresses and routes rather than assuming parity. Keep both providers running behind a feature flag until your checks pass, then remove the Google script tag and key.

On this page

Analytics settings