Leaflet 1.0.X fitBounds带有自定义投影

时间:2017-02-15 15:41:42

标签: javascript leaflet

我正在努力将项目从传单0.7.3迁移到传单1.0.3。我的地图有一个自定义投影,这似乎导致了fitBounds功能的问题。

https://jsfiddle.net/4c2oxh89/这是fitBounds与传单0.7.3

一起正常工作的一个例子

https://jsfiddle.net/jsywsgah/是一个fitBounds与传单1.0.3

工作不正常的例子
L.Projection.CustomProjection = {
  tileSize: 256,
  resolutionNum: 72,
  inchesPerUnit: 39.3701,
  originShift: Math.PI * 6378137,
  mapConfig: {"aScales":[69885283.0036,34942641.5018,17471320.7509,8735660.37545,4367830.18772,2183915.09386,1200000,600000,300000,144000,68247.3466832,34123.6733416,17061.8366708,8530.9183354,4265.4591677,2132.72958385],"nCurrentScale":8,"nScale":300000,"initZoom":8,"initTop":-44485.818459823,"initLeft":-81008.552342608,"tileWidth":256,"tileHeight":256,"currentMap":"world_navteq_day","labelOpacity":10,"fallbackMap":"add"},

  // https://github.com/Leaflet/Leaflet/blob/63fd4edc76893ab2a2f83d54e703e0a4da73de7b/src/geo/projection/Projection.SphericalMercator.js
  bounds: (() => {
    const d = 6378137 * Math.PI;
    return L.bounds([-d, -d], [d, d]);
  })(),

  latLonToMeters: function(lat, lon){
    const mx = lon * this.originShift / 180.0;
    let my = Math.log(Math.tan((90 + lat) * Math.PI / 360.0)) / (Math.PI / 180.0);
    my *= this.originShift / 180.0;
    return [mx, my];
  },

  metersToLatLon: function(mx, my){
    const lon = (mx / this.originShift) * 180.0;
    let lat = (my / this.originShift) * 180.0;
    lat = 180 / Math.PI * (2 * Math.atan( Math.exp( lat * Math.PI / 180.0)) - Math.PI / 2.0);
    return [lat, lon];
  },

  latLonToPixels: function(lat, lon, zoom){
    const m = this.latLonToMeters(lat, lon);
    return this.metersToPixels(m[0], m[1], zoom);
  },

  metersToPixels: function(mx, my, zoom){
    const scale = this.resolution(zoom);
    const px = (mx / scale);
    const py = (-my / scale);
    return [Math.floor(px), Math.floor(py)];
  },

  pixelsToMeters: function(px, py, zoom){
    const scale = this.resolution(zoom);
    const gx = px * scale;
    const gy = -py * scale;
    return [gx, gy];
  },

  resolution: function(zoom){
    return (this.mapConfig.aScales[zoom] / (this.resolutionNum * this.inchesPerUnit));
  },

  project: function(latLng, zoom){
    const pixels = this.latLonToPixels(latLng.lat, latLng.lng, zoom);
    return new L.Point(pixels[0], pixels[1]);
  },

  unproject: function(point, zoom){
    const meters = this.pixelsToMeters(point.x, point.y, zoom);
    const latLon = this.metersToLatLon(meters[0], meters[1]);
    return new L.LatLng(latLon[0], latLon[1]);
  },
};

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

确定。所以。我只是复制/粘贴了0.7.3版本的getBoundsZoom并且它有效。我不喜欢那个解决方案但是:/