Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import coordtransform from "coordtransform";
mapboxgl.accessToken = '<your mapbox access token>';
let exampleMap;
// CHANGE WITH COORDINATES FROM HERE http://api.map.baidu.com/lbsapi/getpoint/
const bd09Coords = [113.341334, 23.193119];
function renderMap() {
if (exampleMap) exampleMap.remove();
// this is where conversion happens
const gcj02 = coordtransform.bd09togcj02(bd09Coords[0], bd09Coords[1]);
console.log('GCJ-02: ', gcj02);
const wgs84Coordinates = coordtransform.gcj02towgs84(gcj02[0], gcj02[1]);
console.log('WGS-84: ', wgs84Coordinates);
// don't forget to add <div id='mapbox-map-id' style='width: 100%; height: 300px;'></div>
// somewhere in your html
exampleMap = new mapboxgl.Map({
container: 'mapbox-map-id',
style: 'mapbox://styles/mapbox/streets-zh-v1',
zoom: 15,
center: wgs84Coordinates
});
exampleMap.addControl(new window.mapboxgl.NavigationControl());
new window.mapboxgl.Marker().setLngLat(wgs84Coordinates).addTo(exampleMap);
}
renderMap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.