Rog*_*rHN 4 javascript dictionary leaflet
我有一个大小为8576x8576px的图像,我想让坐标与1:1匹配.另外我想要图像中心的坐标0,0(现在中心是-128,128).而且我也希望显示坐标.我想为用户插入坐标放置一个locate按钮,然后在地图上找到它们.像这样的东西:http://xero-hurtworld.com/map_steam.php (我使用相同的图像,但更大).瓷砖尺寸我的268px.
我的代码到目前为止:
https://jsfiddle.net/ze62dte0/
<!DOCTYPE html>
<html>
<head>
<title>Map</title>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
<![endif]-->
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js" charset="utf-8"></script>
<script>
function init() {
var mapMinZoom = 0;
var mapMaxZoom = 3;
var map = L.map('map', {
maxZoom: mapMaxZoom,
minZoom: mapMinZoom,
crs: L.CRS.Simple
}).setView([0, 0], mapMaxZoom);
window.latLngToPixels = function(latlng){
return window.map.project([latlng.lat,latlng.lng], window.map.getMaxZoom());
};
window.pixelsToLatLng = function(x,y){
return window.map.unproject([x,y], window.map.getMaxZoom());
};
var mapBounds = new L.LatLngBounds(
map.unproject([0, 8576], mapMaxZoom),
map.unproject([8576, 0], mapMaxZoom));
map.fitBounds(mapBounds);
L.tileLayer('{z}/{x}/{y}.jpg', {
minZoom: mapMinZoom, maxZoom: mapMaxZoom,
bounds: mapBounds,
noWrap: true,
tms: false
}).addTo(map);
L.marker([0, 0]).addTo(map).bindPopup("Zero");
L.marker([-128, 128]).addTo(map).bindPopup("center");
var popup = L.popup();
<!-- Click pop-up>
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked in " + e.latlng.toString ())
.openOn(map);
}
map.on('click', onMapClick);
}
</script>
<style>
html, body, #map { width:100%; height:100%; margin:0; padding:0; }
</style>
</head>
<body onload="init()">
<div id="map"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,你想要一个类似于L.CRS.Simple那个位置的瓷砖0/0/0(瓷砖尺寸268px,即8576 /2⁵),这样:
[0, 0]位于该图块的中心.[-8576/2, -8576/2]变为[8576/2, 8576/2].您只需要L.CRS.Simple使用适当的变换进行调整,以考虑这个1 /2⁵= 1/32(而不仅仅是1)的比例,并且偏移量为8576*1/32/2 = 268/2 = 134(而不是0.5).
L.CRS.MySimple = L.extend({}, L.CRS.Simple, {
transformation: new L.Transformation(1 / 32, 134, -1 / 32, 134)
});
var map = L.map('map', {
maxZoom: mapMaxZoom,
minZoom: mapMinZoom,
crs: L.CRS.MySimple
}).setView([0, 0], mapMaxZoom);
Run Code Online (Sandbox Code Playgroud)
演示:http://plnkr.co/edit/5SQqp7SP4nf8muPM5iso?p=preview(我用Plunker代替的jsfiddle因为你提供HTML一整页的代码,而的jsfiddle希望你到你的HTML,CSS和JavaScript代码分割成独立的模块).
至于显示坐标和"定位"按钮,它实现起来非常容易,因此它类似于你提到的例子.如果您需要帮助,请随时打开新问题.
在上面的演示中,我使用Leaflet.Coordinates插件快速实现两个功能(请参阅地图左下角的控件;您必须开始在地图上移动鼠标以显示坐标;单击该控件以打开版本模式).
编辑:
对于Leaflet.Coordinates插件,它包装显示的坐标经度保持在[-180; 180]度.
在您的坐标不是度数的情况下,没有包装经度的点.
我认为这是点击弹出窗口和控件之间坐标差异的原因.
只需修补插件代码以防止包装:
// Patch first to avoid longitude wrapping.
L.Control.Coordinates.include({
_update: function(evt) {
var pos = evt.latlng,
opts = this.options;
if (pos) {
//pos = pos.wrap(); // Remove that instruction.
this._currentPos = pos;
this._inputY.value = L.NumberFormatter.round(pos.lat, opts.decimals, opts.decimalSeperator);
this._inputX.value = L.NumberFormatter.round(pos.lng, opts.decimals, opts.decimalSeperator);
this._label.innerHTML = this._createCoordinateLabel(pos);
}
}
});
Run Code Online (Sandbox Code Playgroud)
更新的演示:http://plnkr.co/edit/M3Ru0xqn6AxAaSb4kIJU?p = preview