是否可以将GeoJSON标记放入变量中,以便在“当前位置”和“最近的标记”之间划一条线?

时间:2019-05-15 02:02:29

标签: javascript mapbox

我正在用美国的所有塔式天线设置地图。我正在尝试做到这一点,以便当您按当前位置或输入地址时,与您所在位置的“落脚”最近的天线塔会在两者之间画一条线。

我能够将geolocate放入一个变量中,当我按下位置​​图标时,它将在我和我已经在其中放置的任何坐标之间绘制一条线(例如[[111,111])。但是我也想将GeoJSON图标转换为坐标,该怎么做?

请参阅codepen:https://codepen.io/richardv/pen/bJgOKE

<html>
<head>
<meta charset='utf-8' />
<title>Add a geocoder</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.54.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.54.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>

<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.2.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.2.0/mapbox-gl-geocoder.css' type='text/css' />
<div id='map'></div>

<script>



mapboxgl.accessToken = 'pk.eyJ1IjoicnZhbGVudGEiLCJhIjoiY2p1YWRrOGEyMDFrMTN5cXR3eW9qazA4NyJ9.CBomAqHnL7P5Miq88svWiw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/rvalenta/cjuadldsc5dfu1fppjl4gxc6k',
center: [-117, 33],
zoom: 10
});

map.addControl(new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl
}));
  // Add geolocate control to the map.
//map.addControl(new mapboxgl.GeolocateControl({
//positionOptions: {
//enableHighAccuracy: true
//},
//trackUserLocation: true
//}));




//It will return an array ["latitude","longitude"]

//convert the first element of the array to float num

//var lat = parseFloat([-117]);
//convert the second element of the array to float num

//var lng = parseFloat([33]);

var geolocate = new mapboxgl.GeolocateControl();

map.addControl(geolocate);

geolocate.on('geolocate', function(e) {
      var lon = e.coords.longitude;
      var lat = e.coords.latitude;
      var position = [lon, lat]; 
      var tvlon = 111;
      var tvlat = 111;
      console.log(position);

geolocate.on('geolocate', function(e) { 

map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[lon, lat],
[tvlon, tvlat]
]
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "green",
"line-width": 8
}
});
});
});


</script>

</body>
</html>

0 个答案:

没有答案