Leaflet custom coordinates on image

后端 未结 1 873
谎友^
谎友^ 2020-12-06 08:25

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,1

相关标签:
1条回答
  • 2020-12-06 09:01

    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:

    • 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].

    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);
    

    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.

    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).


    EDIT:

    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);
        }
      }
    });
    

    Updated demo: http://plnkr.co/edit/M3Ru0xqn6AxAaSb4kIJU?p=preview

    0 讨论(0)
提交回复
热议问题