我在弹出窗口中嵌入推文时遇到问题。我使用Mapbox GL JS和一个twitter小部件来设置弹出内容的样式。我将小部件放在click函数中,如下所示:
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on('click', 'places', function (e) {
//twitter widjet
window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
new mapboxgl.Popup()
.setLngLat(e.features[0].geometry.coordinates)
.setHTML('<h3>' + e.features[0].properties.title + '</h3><p>' + e.features[0].properties.description + '</p>')
.addTo(map);
});
&#13;
使用Twitter的嵌入链接将推文本身硬编码到Geojson的描述部分:
map.on('load', function () {
map.addLayer({
"id": "places",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-6.2285,53.3475]
},
"properties": {
"title": "3arena",
"icon": "stadium",
"description": "<blockquote class='twitter-tweet' data-lang='en'><p lang='en' dir='ltr'>.<a /*TWEET INFO*/ </a></blockquote>"
}
}]
}
},
"layout": {
"icon-image": "{icon}-15",
"icon-size": 2,
"icon-allow-overlap": true
}
});
});
&#13;
tweet popup只运行一次,但在此之后无法设置弹出窗口样式,即使click功能继续有效。任何有关此事的帮助将不胜感激。这是一个有效的例子:
<head>
<meta charset='utf-8' />
<title>Example</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.39.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.mapboxgl-popup {
max-width: 400px;
font: 12px/20px 'Work Sans', Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiamFtZXNqaGVucnkiLCJhIjoiY2o0cHBzNjlzMmFrNjMybzR3aTZsbmNxeSJ9.GYnleaLc8niIQskdkFroLw'; // access token
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v10', // style URL
center: [-6.2285,53.3475],
zoom: 14
});
var nav = new mapboxgl.NavigationControl();
map.addControl(nav, 'top-left');
// Add geolocate control to the map.
map.addControl(new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
}));
map.on('load', function () {
map.addLayer({
"id": "places",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-6.2285,53.3475]
},
"properties": {
"title": "3arena",
"icon": "stadium",
"description": "<blockquote class='twitter-tweet' data-lang='en'><p lang='en' dir='ltr'>A tweet</p>— James Henry (@The_Hendawg) <a href='https://twitter.com/The_Hendawg/status/899998799728451584'>August 22, 2017</a></blockquote>"
}
}]
}
},
"layout": {
"icon-image": "{icon}-15",
"icon-size": 2,
"icon-allow-overlap": true
}
});
});
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on('click', 'places', function (e) {
//twitter widjet
window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
new mapboxgl.Popup()
.setLngLat(e.features[0].geometry.coordinates)
.setHTML('<h3>' + e.features[0].properties.title + '</h3><p>' + e.features[0].properties.description + '</p>')
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the places layer.
map.on('mouseenter', 'places', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'places', function () {
map.getCanvas().style.cursor = '';
});
</script>
</body>
</html>
&#13;