問題描述
我有一個尺寸為 8576x8576px 的圖像,我想讓坐標匹配 1:1.我還想要圖像中心的坐標 0,0(現在中心是 -128,128).我也想顯示坐標.我想為用戶插入坐標放置一個定位按鈕,然后在地圖上找到它們.像這樣:http://xero-hurtworld.com/map_steam.php(我使用相同的圖像但更大).我設置的 tile 大小為 268px.
I have an image which size is 8576x8576px, and I want to make the coordinates match 1:1. Also I want the coordinates 0,0 in the center of the image (now the center is -128,128). And I want to show the coordinates too. I want to put a locate button for the user insert coordinates and then find them on the map. Something like this: http://xero-hurtworld.com/map_steam.php (I am using the same image but bigger). The tile size I made its 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" />
<!--[if lte IE 8]>
<link rel="stylesheet" />
<![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>
推薦答案
如果我理解正確,你想要一個類似于 L.CRS.Simple
的 CRS,它放置 tile 0/0/0(tile大小為 268px,即 8576/2?),這樣:
If I understand correctly, you want a CRS similar to L.CRS.Simple
that places tile 0/0/0 (tile size 268px, which is 8576 / 2?) so that:
- 位置
[0, 0]
位于該圖塊的中心. - 整個世界(即整個 tile 0/0/0)從位置
[-8576/2, -8576/2]
到[8576/2, 8576/2]
.
- Position
[0, 0]
is at the center of that tile. - The entire world (i.e. entire tile 0/0/0) goes from position
[-8576/2, -8576/2]
to[8576/2, 8576/2]
.
您只需要使用適當的轉換來調整 L.CRS.Simple
,以說明 1/2? = 1/32(而不僅僅是 1)的比例和 8576 的偏移量* 1/32/2 = 268/2 = 134(而不是 0.5).
You would just need to adjust the L.CRS.Simple
with the appropriate transformation, to account for this scale of 1/2? = 1/32 (instead of just 1) and offset of 8576 * 1/32 / 2 = 268 / 2 = 134 (instead of 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);
演示:http://plnkr.co/edit/5SQqp7SP4nf8muPM5iso?p=preview(我使用 Plunker 而不是 jsfiddle,因為您提供了帶有 HTML 的完整頁面代碼,而 jsfiddle 希望您將 HTML、CSS 和 JavaScript 代碼拆分為單獨的塊).
Demo: http://plnkr.co/edit/5SQqp7SP4nf8muPM5iso?p=preview (I used Plunker instead of jsfiddle because you provided a full page code with HTML, whereas jsfiddle expects you to split your HTML, CSS and JavaScript codes into separate blocks).
至于顯示坐標和定位"按鈕,它很容易實現,因此與您提到的示例相似.如果您需要幫助,請隨時提出新問題.
As for showing the coordinates and a "locate" button, it would be quite easy to implement so that it is similar to the example you mention. Feel free to open new questions if you need help.
在上面的演示中,我使用 Leaflet.Coordinates 插件 來快速實現這兩個功能(參見地圖左下角的控件;您必須開始在地圖上移動鼠標才能顯示坐標;單擊該控件以打開編輯模式.
In the above demo, I used Leaflet.Coordinates plugin to implement quickly both functionalities (see the control on bottom left corner of the map; you have to start moving your mouse on the map for the coordinates to appear; click on that control to open the edition mode).
對于 Leaflet.Coordinates 插件,它將顯示的坐標經度包裹起來以保持在 [-180;180] 度.
As for the Leaflet.Coordinates plugin, it wraps displayed coordinates longitude to stay within [-180; 180] degrees.
在坐標不是度數的情況下,包裹經度沒有意義.
In your case where coordinates are not degrees, there is no point wrapping the longitude.
我認為這是造成點擊彈窗與控件坐標不一致的原因.
I think this is the cause for the discrepancy of coordinates between the click popup and the control.
只需修補插件代碼以防止包裝:
Simply patch the plugin code to prevent wrapping:
// 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);
}
}
});
更新的演示:http://plnkr.co/edit/M3Ru0xqn6AxAaSb4kIJU?p=preview一個>
這篇關于圖片上的傳單自定義坐標的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!