gpt4 book ai didi

javascript - 使用 MapBox java-script SDK 显示位置

转载 作者:行者123 更新时间:2023-11-28 03:50:07 26 4
gpt4 key购买 nike

如何使用 MapBox SDk 正向地理编码功能在 HTML 页面上显示地址的位置。SDK 已经执行查询,但我需要能够在 html 上显示 map 及其位置。这是我的示例代码。

<html>
<head>
<meta charset="utf-8">
<title>MapBox</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.43.0/mapbox-gl.js"></script>
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.43.0/mapbox-gl.css" rel="stylesheet">
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>

<body style="word-wrap: break-word;">
<script src='https://unpkg.com/mapbox@1.0.0-beta9/dist/mapbox-sdk.min.js'></script>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibGF3aXgxMCIsImEiOiJjamJlOGE1bmcyZ2V5MzNtcmlyaWRzcDZlIn0.ZRQ73zzVxwcADIPvsqB6mg';
console.log(mapboxgl.accessToken);

var client = new MapboxClient(mapboxgl.accessToken);
console.log(client);

var address = '6 Antares Drive, Ottawa, Ontario, K2E 6AE, Canada'
var test= client.geocodeForward(address, function(err, data, res) {
// data is the geocoding result as parsed JSON
// res is the http response, including: status, headers and entity properties

console.log(res);
console.log(res.url);
console.log(data);
console.log(err);

});

console.log(test);





// how do I display the address on the HTML?





</script>
</body>

</html>

console.log(res.url) 为我提供了所需的网址,但如何显示 map 并向其添加标记。

最佳答案

您需要创建一个 <div>将保存您的 map 的标签。请参阅here for an example

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Display a map</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.43.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.43.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>

<div id='map'></div> // this is the line you're looking for
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibGF3aXgxMCIsImEiOiJjamJlOGE1bmcyZ2V5MzNtcmlyaWRzcDZlIn0.ZRQ73zzVxwcADIPvsqB6mg';
var map = new mapboxgl.Map({
container: 'map', // id of the div you assigned above
style: 'mapbox://styles/mapbox/streets-v9', // stylesheet of basemap you want
center: [-74.50, 40], // starting position [lng, lat]
zoom: 9 // starting zoom
});
</script>

</body>
</html>

关于javascript - 使用 MapBox java-script SDK 显示位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48066274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com