我正拼命尝试通过使用带有typescript的mapbox-gl-js向mapbox地图添加一些标记,并且从mapbox网站上删除了一些教程,但对我来说不起作用。
mapboxgl.accessToken = 'mykey';
this.map = new mapboxgl.Map({
container: 'map',
style: this.style,
zoom: 13,
center: [12, 12]
});
this.map.on('load', (event) => {
this.map.addSource('testSrc', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ 12, 12]
},
properties: {
title: 'Mapbox',
description: 'Washington, D.C.'
}
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-122.414, 37.776]
},
properties: {
title: 'Mapbox',
description: 'San Francisco, California'
}
}]
}
});
this.map.addLayer({
id: 'testSrc',
source: 'testSrc',
type: 'circle',
layout: {
'text-field': '{message}',
'text-size': 24,
'text-transform': 'uppercase',
'icon-image': 'rocket-15',
'text-offset': [0, 1.5]
},
paint: {
'text-color': '#f16624',
'text-halo-color': '#fff',
'text-halo-width': 2
}
});
this.map.addControl(new mapboxgl.NavigationControl());
});
我用一些静态数据创建了一个源,并将其设置为mapboxgl地图对象的一层。 该地图正在显示,没有任何问题,但是我无法使用必需的geojson格式添加一些标记。
我的目标是动态添加标记,但是在这里我不得不添加一些静态标记。
您知道这里有什么问题吗?
关于Marko
答案 0 :(得分:1)
您可以通过两种主要方法将“标记”添加到Mapbox GL JS,
使用标记,例如https://docs.mapbox.com/mapbox-gl-js/example/custom-marker-icons/,它将为图像添加DOM元素。
使用符号,例如https://docs.mapbox.com/mapbox-gl-js/example/center-on-symbol/,它将图像添加为WebGL画布的一部分。您需要确保已加载使用的图像,例如。 https://docs.mapbox.com/mapbox-gl-js/example/add-image/
答案 1 :(得分:0)
尝试一下-这对我有用(如果您不想要,请删除弹出窗口)
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title> My Map </title>
<!--This where you put all your dependencies-->
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title> My Map </title>
<!--This where you put all your dependencies-->
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox-gl-js/v1.11.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v1.11.0/mapbox-gl.css' rel='stylesheet' />
<style>
/*this CSS section is where you style your page*/
/*this is how you comment in CSS*/
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
.marker {
background-image: url('mapbox-icon.png');
background-size: cover;
width: 50px;
height: 50px;
border-radius: 50%;
cursor: pointer;
}
.mapboxgl-popup {
max-width: 200px;
}
.mapboxgl-popup-content {
text-align: center;
font-family: 'Open Sans', sans-serif;
}
</style>
</head>
<body>
<div id='map'></div>
<!--The below script section is a javascript section in your html file-->
<script>
//This is where you put your Javascript for interaction
mapboxgl.accessToken = 'pk.eyJ1IjoiY3dpbG1vdHQiLCJhIjoiY2s2bWRjb2tiMG1xMjNqcDZkbGNjcjVraiJ9.2nNOYL23A1cfZSE4hdC9ew'; //Put your own mapbox token
var map = new mapboxgl.Map({
container: 'map', // container id specified in the HTML
style: 'mapbox://styles/cwilmott/ckbowmfzd3r761il84bnji13t' //change to your style
});
var geojson = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
time: '2020-06-19T09:47:13Z',
title: '10:47:13',
description: '19 Jun 2020 at 10:47:13'
},
geometry: {
type: 'Point',
coordinates: [
-2.219255366395865,
53.457215089777584,
]
}
},
{
type: 'Feature',
properties: {
time: '2020-06-19T09:47:19Z',
title: 'home',
description: '19 Jun 2020 at 10:47:19'
},
geometry: {
type: 'Point',
coordinates: [
-2.219227672420135,
53.457351307993434
]
}
}]
}
// add markers to map
geojson.features.forEach(function(marker) {
// create a HTML element for each feature
var el = document.createElement('div');
el.className = 'marker';
// make a marker for each feature and add to the map
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.setLngLat(marker.geometry.coordinates)
.setPopup(new mapboxgl.Popup({ offset: 25 }) // add popups
.setHTML('<h3>' + marker.properties.title + '</h3><p>' + marker.properties.description + '</p>'))
.addTo(map);
});
</script>
</body>
</html>
如果要添加外部GeoJson,请删除var geojson部分中的要素,而放:
var geojson = {
type: 'geojson',
data: 'path-to-your-file.geojson'
}
(我认为应该可以)