gpt4 book ai didi

javascript - 如何从mapbox获取数据到html页面

转载 作者:行者123 更新时间:2023-12-03 02:25:54 25 4
gpt4 key购买 nike

我正在使用 Mapbox gl js。我正在控制台中获取数据,但无法将该数据提取到 HTML,任何人都可以帮助我做到这一点。我需要 map 数据内的数据来显示在 html 页面上,当我单击标记时,showStory 条件必须变为 true。我尝试使用 ngFor 来使用 mapData,但它不起作用,并且当我在 Mapbox 之外进行控制台时,控制台中存在的数据也不会出现。

HTML:

   <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12" (click)="openFav()" *ngIf="showStory">
<ul class="addressClass">
<li><i class="fa fa-star-o" aria-hidden="true"></i></li>
<li><b>Place/Name</b></li>
<li>Country, City</li>
<li>Postal Code, Address</li>
</ul>
</div>

T:

public showStory:boolean = false;
this.ApiService
.getAllPins()
.subscribe(
pins => {
this.places = pins.data;
this.points = this.places.map(function(pins) {
return {"name":pins.name,"lat":pins.lat,"lang":pins.lang,"address":pins.address,"category_name":pins.category_name,
"description":pins.description,"email":pins.email,"phone_number":pins.phone_number,"pin_media":pins.pin_media,
"country":pins.user_details.country,"user_name":pins.user_details.user_name};
});
mapboxgl.accessToken = 'pk.eyJ1IjoicmFrc2hpdGhhMTkiLCJhIjoiY2pjcHl1YW5wMjR5czJ6bzdqdjZrbDRzeSJ9.OOqu6zVyNsXavzCsYoBdPA';
var coOrdinates = this.points;
var map = new mapboxgl.Map({
container: 'maps',
style: 'mapbox://styles/mapbox/streets-v9',
center: [coOrdinates[1].lat,coOrdinates[1].lang],
zoom: 3
});
map.on('load', function () {
for(var i=0; i< coOrdinates.length; i++) {
map.addLayer({
"id": "points" + i,
"type": "circle",
"paint":{
"circle-radius":15,
"circle-color":'#' + (Math.random().toString(16) + "000000").substring(2,8)
},
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {"field":coOrdinates[i],"name":coOrdinates[i].name},
"geometry": {
"type": "Point",
"coordinates": [coOrdinates[i].lat,coOrdinates[i].lang]
}
}]
}
}
});
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on("mouseenter", "points" + i, e => {
this.id = e.features[0].layer.id;
map.setPaintProperty(e.features[0].layer.id, 'circle-radius', 20);
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.name;
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
popup.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});
map.on("mouseleave", "points" + i, e => {
if(this.id) {
map.setPaintProperty(this.id, 'circle-radius', 15);
this.id = undefined;
}
popup.remove();
});
map.on("click", "points" + i, e => {
this.id = undefined;
this.mapData = e.features[0].properties
for(var i=0; i< coOrdinates.length; i++) {
map.setPaintProperty('points'+i, 'circle-radius', 15);
}
map.setPaintProperty(e.features[0].layer.id, 'circle-radius', 20);
this.showStory = true;
this.mapData = JSON.parse(this.mapData.field);
console.log(this.mapData);
});
}
});
}, error => {});

最佳答案

如果要将组件中的数据显示为 HTML,请使用 innerHTML 属性,

html 中,

<li><b [innerHTML]="place"></b></li> 
<li [innerHTML]="Country, City"></li>
<li [innerHTML]="Postal Code, Address"></li>

Ts中:

添加,this.place = this.mapData.place;

map.on("click", "points" + i, e => {
this.id = undefined;
this.mapData = e.features[0].properties
for(var i=0; i< coOrdinates.length; i++) {
map.setPaintProperty('points'+i, 'circle-radius', 15);
}
map.setPaintProperty(e.features[0].layer.id, 'circle-radius', 20);
this.showStory = true;
this.mapData = JSON.parse(this.mapData.field);
this.place = this.mapData.place;
console.log(this.mapData);
});

关于javascript - 如何从mapbox获取数据到html页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48946426/

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