Import Vietmap WebGL API to your project.
npm install @vietmap/vietmap-gl-js
import vietmapgl from "@vietmap/vietmap-gl-js/dist/vietmap-gl";
import '@vietmap/vietmap-gl-js/dist/vietmap-gl.css';
const [map, setMap] = useState<vietmapgl.Map | null>(null);
useEffect(() => {
if (!mapContainerRef.current) return;
// Get API key from environment variables
const apiKey = import.meta.env.VITE_VIETMAP_API_KEY || '';
const mapInstance = new vietmapgl.Map({
container: mapContainerRef.current,
style: `https://maps.vietmap.vn/mt/tm/style.json?apikey=${apiKey}`,
center: [106.6755666, 10.7588867], // Vietnam centered
zoom: 12,
});
mapInstance.addControl(new vietmapgl.NavigationControl(), 'top-right');
// Wait for map to load
mapInstance.on('load', () => {
setMap(mapInstance);
});
// Clean up on unmount
return () => {
mapInstance.remove();
};
}, []);
const marker = new vietmapgl.Marker()
.setLngLat([106.6755666, 10.7588867])
.addTo(map);
const popup = new vietmapgl.Popup()
.setLngLat([106.6755666, 10.7588867])
.setHTML('<h1>Hello Vietmap</h1>')
.addTo(map);
map.flyTo({
center: [106.6755666, 10.7588867],
zoom: 12,
duration: 1000,
});
Add the following code to your HTML file.
<script src="https://unpkg.com/@vietmap/vietmap-gl-js@5.0.2/dist/vietmap-gl.js"></script>
<link href="https://unpkg.com/@vietmap/vietmap-gl-js@5.0.2/dist/vietmap-gl.css" rel="stylesheet" />
Follow the documentation here to get started. Contact vietmap.vn to register a valid key.