使用MapBox java脚本SDK显示位置[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

如何使用MapBox SDk转发地理编码功能在HTML页面上显示地址的位置。 SDK已经完成了查询,但我需要能够在html上显示地图及其位置。这是我的示例代码。

<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/[email protected]/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)为我提供了所需的URL,但是如何显示地图并为其添加标记。

javascript html mapbox geocoding
1个回答
0
投票

你需要制作一个<div>标签来保存你的地图。见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>
© www.soinside.com 2019 - 2024. All rights reserved.