[英].toArray(); is not a function
I have a issue with converting longitute and latitute into a array my code is like this: 我在将经度和纬度转换为数组时遇到问题,我的代码如下所示:
<div id="right" class="map">
<div id='map' class="map" style='width: 100%; height: 100%; margin: 0px;'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibGl2ZS1vbGRoYW0iLCJhIjoiY2ozbXk5ZDJ4MDAwYjMybzFsdnZwNXlmbyJ9.VGDuuC92nvPbJo-qvhryQg';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v10',
center: [-1.77, 52.73],
zoom: 3
});
map.addControl(new MapboxGeocoder({
accessToken: mapboxgl.accessToken
}));
map.on('click', function(e) {
var test = JSON.stringify(e.lngLat);
test.toArray();
console.log(test);
mapboxgl.Marker()
.setLngLat(test)
.addTo(map);
});
test is returning value as: 测试返回的值为:
{"lng":-2.8246875000017155,"lat":52.72999999999914}
However, the value needs to be as: 但是,该值必须为:
[-2.8246875000017155, 52.72999999999914]
So toArray should work but I get 所以toArray应该可以,但是我得到了
Uncaught TypeError: test.toArray is not a function
未捕获的TypeError:test.toArray不是函数
at e.在e。 (1:181)
(1:181)
at e.Evented.fire (evented.js:87)在e.Evented.fire(evented.js:87)
at h (bind_handlers.js:139)在h(bind_handlers.js:139)
at HTMLDivElement.s (bind_handlers.js:114)在HTMLDivElement.s(bind_handlers.js:114)
Edit 编辑
map.on('click', function(e) {
var test = e.lngLat;
console.log(test);
test.toArray();
mapboxgl.Marker()
.setLngLat(test)
.addTo(map);
});
But now I get: 但是现在我得到:
Uncaught TypeError: Cannot read property 'bind' of undefined
未捕获的TypeError:无法读取未定义的属性“ bind”
The issue is you have to create a new marker. 问题是您必须创建一个新标记。 If you follow the code in the documentation it should work.
如果您遵循文档中的代码,它应该可以工作。
Try the following modification to your code 尝试对您的代码进行以下修改
map.on('click', function (e) {
new mapboxgl.Marker()
.setLngLat(e.lngLat)
.addTo(map);
});
Edit 编辑
Nothing will appear as a marker, you have to style that yourself as far as I'm aware, try adding this CSS to see something. 什么都不会作为标记出现,就我所知,您必须自行设置样式,尝试添加此CSS来查看内容。
.mapboxgl-marker {
width: 10px;
height: 10px;
background-color: purple;
}
You could try the following: 您可以尝试以下方法:
var longLatArray = [];
longLatArray.push(e.lng);
longLatArray.push(e.lat);
mapboxgl.Marker()
.setLngLat(longLatArray)
.addTo(map);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.