Javascript Jquery:';类型';只能在.ts文件中使用
我正在使用传单地图进行Jquery项目。我的项目是获取航班位置的json坐标数据。我下面的代码运行良好,但问题是数据更新时地图上有重复标记。我做了一些步骤删除以前的标记位置,然后显示新的标记位置,但我有一个错误Javascript Jquery:';类型';只能在.ts文件中使用,javascript,leaflet,marker,Javascript,Leaflet,Marker,我正在使用传单地图进行Jquery项目。我的项目是获取航班位置的json坐标数据。我下面的代码运行良好,但问题是数据更新时地图上有重复标记。我做了一些步骤删除以前的标记位置,然后显示新的标记位置,但我有一个错误“类型”只能在.ts文件中使用。 数据更新时在地图上重复标记 我的代码 $(document).ready(function () { var heading ; coords: L.Marker[] var type ; // map ini
“类型”只能在.ts文件中使用。
数据更新时在地图上重复标记
我的代码
$(document).ready(function () {
var heading ;
coords: L.Marker[]
var type ;
// map initializing
var coords = [33,44]; // the geographic center of our map
var zoomLevel = 6 // the map scale.
var map = L.map('map').setView(coords, zoomLevel);
L.tileLayer('https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}.png', {
attribution: 'Map data ©',
maxZoom: 18
}).addTo(map);
// Aircraft data
setInterval(function(){
$.ajax('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', {
type: 'GET',
dataType: 'jsonp',
timeout: 5000
}).done(function (data, textStatus, jqXHR) {
// Error is here ------------ under marker //
coords ? coords.forEach((marker:marker) => {
marker.remove();
}) : '';
coords = [];
Object.keys(data)
.map(key => data[key])
.map((position) => ({
lat: position[1],
lng: position[2],
callSign: position[13],
heading: position[3],
AircraftType: position[8]
})).filter(position => position.lat && position.lng && position.callSign
&& position.heading
&& position.AircraftType).forEach(i => {
console.log(i.lat, i.lng, i.heading)
coords.push( new L.Marker([i.lat, i.lng], {
icon: new L.DivIcon({
html: ` <div style="width:120px;";>
<div class="temp-${i.heading} ${type}"></div>
<span style="color:white;background-color: midnightblue;margin: 4px;padding: 4px;border-radius: 3px;">
${i.callSign}
</span
></div>`
})
}).addTo(marker))
})
},5000)
});
$(文档).ready(函数(){
风险值标题;
坐标:L.Marker[]
var类型;
//映射初始化
var coords=[33,44];//我们地图的地理中心
var zoomLevel=6//地图比例。
var map=L.map('map').setView(coords,zoomLevel);
L.tileLayer('https://{s}.tile.thunderforest.com/transport dark/{z}/{x}/{y}.png'{
属性:“地图数据©;”,
最大缩放:18
}).addTo(地图);
//飞机数据
setInterval(函数(){
$.ajax('xxxxxxxxxxxxxxxxxxxxxxxxxxxx'{
键入:“GET”,
数据类型:“jsonp”,
超时:5000
}).done(函数(数据、文本状态、jqXHR){
//错误在这里----------标记下//
coords?coords.forEach((标记:标记)=>{
marker.remove();
}) : '';
坐标=[];
对象。键(数据)
.map(key=>data[key])
.map((位置)=>({
横向:位置[1],
液化天然气:位置[2],
呼号:位置[13],
标题:位置[3],
飞机类型:位置[8]
})).filter(位置=>position.lat&&position.lng&&position.callSign
&&方位
&&位置.AircraftType).forEach(i=>{
控制台日志(i.lat、i.lng、i.heading)
协调推进(新的L标记([i.lat,i.lng]{
图标:新的L.DivIcon({
html:`
${i.callSign}
`
})
}).addTo(marker))
})
},5000)
});
看起来您在这里使用的是一种类型:
coords?coords.forEach((marker:marker)=>{
第一个标记是变量,冒号后的第二个标记是TypeScrypt中使用的类型。请尝试删除冒号和标记
coords ? coords.forEach((marker) => {
我在
coords:l.Marker[]下有errro
Uncaught SyntaxError:Unexpected token']
coords:l.Marker[]
这可能根本不需要,或者应该只是var coords;
Uncaught TypeError:Marker.remove不是一个函数。因此我必须使用coords:l.Marker[]
要删除标记,这可能是由于您没有创建标记,但您的coords有值,所以如果您有coords,请在ajax调用之前创建第一个标记。