简体   繁体   English

如何使移动对象保持在Leafllet的中心?

[英]How can I Keep a moving object centered on Leafllet?

I have positioned this satellite on a leaflet map, it is changing locations every 5 seconds. 我已将此卫星定位在传单地图上,它每5秒更改一次位置。 I want to keep the moving object centered on the map so it does not get lost and disappear from sight. 我想使移动物体在地图上居中,以免丢失或消失。 here is HTML and JS code: 这是HTML和JS代码:

JS JS

 var map = L.map('map').setView([0,0], 2); function moveISS () { $.getJSON('http://api.open-notify.org/iss-now.json?callback=?', function(data) { var lat = data['iss_position']['latitude']; var lon = data['iss_position']['longitude']; iss.setLatLng([lat, lon]); isscirc.setLatLng([lat, lon]); map.panTo([lat, lon], animate=true); }); setTimeout(moveISS, 5000); } var styled = L.gridLayer.googleMutant({ type: 'roadmap', styles: [ {elementType: 'labels', stylers: [{visibility: 'off'}]}, {featureType: 'water', stylers: [{color: '#444444'}]} ] }).addTo(map); var roads = L.gridLayer.googleMutant({ type: 'terrain' // valid values are 'roadmap', 'satellite', 'terrain' and 'hybrid' }).addTo(map); L.terminator().addTo(map); //L.terminator().addTo(map);// //http://static-cdn2.ustream.tv/favicon.ico// //https://www.n2yo.com/inc/saticon.php?t=0&s=25544 //L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar'}).addTo(map);// var ISSIcon = L.icon({ iconUrl: 'file:///C:/Users/rez/Downloads/noun_24414.svg', iconSize: [50, 30], iconAnchor: [25, 15], popupAnchor: [50, 25], shadowUrl: 'file:///C:/Users/rez/Downloads/noun_24414.svg', shadowSize: [30, 20], shadowAnchor: [15, 8] }); var iss = L.marker([0, 0], {icon: ISSIcon}).addTo(map); moveISS(); 
 <p id="isstime">WHERE IS ISS?</p> <br> <div id="map"> </div> <script src="iss.js"> </script> <br> 

I removed some parts of your code and modified the moveISS function a little: 我删除了代码的某些部分,并稍微修改了moveISS函数:

var map = L.map('map').setView([0, 0], 4);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

function moveISS() {
  $.getJSON('http://api.open-notify.org/iss-now.json?callback=?', function(data) {
    var lat = data['iss_position']['latitude'];
    var lon = data['iss_position']['longitude'];
    iss.setLatLng([lat, lon]);
    map.panTo([lat, lon], {animate: true});
  });
  setTimeout(moveISS, 5000);
}

var iss = L.marker([0, 0]).addTo(map);
moveISS();

The moving marker is kept centered on the map. 移动标记保持在地图中心。

Check it out in this JSBin . 在此JSBin中签出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM