如何在传单js中获得经度和纬度的缩放事件

时间:2018-10-01 14:38:35

标签: javascript html leaflet

我正在使用传单js绘制地图。我想在两个事件中获取地图的纬度,经度和缩放级别时遇到的困难,首先是缩放,然后是单击。我搜索了堆栈的帖子: Get Position on zooming in map : leaflet 但找不到帮助。最初,我尝试先获取lat&lng,但是我获得了西南和东北元数据,我如何才能获得理想的lat&lng。

MacBook-Pro:marketing robertotomas$ npm uninstall sass-loader
npm WARN sass-loader@5.0.1 requires a peer of webpack@^2.0.0 but none is installed. You must install peer dependencies yourself.

up to date in 5.986s
MacBook-Pro:marketing robertotomas$ npm install sass-loader@6.0.6
npm WARN sass-loader@5.0.1 requires a peer of webpack@^2.0.0 but none is installed. You must install peer dependencies yourself.
var map = L.map('map').setView([51.505, -0.09], 13);

var tiles = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}.png', {
		maxZoom: 18,
		attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
	    }).addTo(map);

// first i try the on zoomend event to get lat, lng
map.on('zoomend',function(e){
    var map = e.target,
    bounds = map.getBounds();
    // here i get southwest & northeast data how to get lat & lng & zoom level 
   console.log(bounds);	
})

2 个答案:

答案 0 :(得分:1)

要获取坐标,您需要定位点或位置。缩放时无法获取坐标,因为它只是一个区域。您将获得该区域的边界。

在地图上单击时,请监听click事件[docs],以检索您刚刚单击的地图上的位置的坐标(https://leafletjs.com/reference-1.3.4.html#map-mouseeventtolatlng):

map.on('click', function(ev){
  var coordinates = map.mouseEventToLatLng(ev.originalEvent);
  console.log(coordinates.lat + ', ' + coordinates.lng);
});

要放大某个缩放事件,您需要致电:

map.on('zoomend',function(e) {
   console.log(e.target.getZoom());
})

Demo

答案 1 :(得分:0)

getCenter() 将为您提供地图中心的经纬度,getZoom() 将为您提供缩放级别。

map.on('zoomend',function(e) {
   console.log(e.target.getCenter());
   console.log(e.target.getZoom());
})