1

I'm using the following npm module to convert to/from lat/lng and pixels:

https://www.npmjs.com/package/viewport-mercator-project

Here's the usage:

// Create a new viewport. 
var ViewportMercator = require('viewport-mercator-project');
// NOTE: `ViewportMercator` objects are immutable by design. 
var viewport = ViewportMercator({
  center: [0, 0],
  zoom: 0,
  tileSize: 512,
  dimensions: [600, 800]
});

var lnglat = [0, 0];
var pixels = viewport.project(lnglat); // returns [300, 400] 

viewport.unproject(pixels); // returns [0, 0]

However, this always seems to fail in my use case, where my center is -122~:

ViewportMercator({
      center: [37.7833, -122.4167],
      zoom: 13,
      tileSize: 512,
      dimensions: [1000, 778]
    });
> viewport.project([37.7833, -122.4167])
[ 500, NaN ] // should return (500, 389)

I know this has something to do with the fact that my lng value (in the center) is outside of the range -90 < lng < 90, but how can I fix this? My attempted viewport conversion is definitely valid...

4

1 回答 1

0

在我看来,您只是将经度和纬度颠倒了。

[longitude, latitude]ViewportMercator 需要一个格式为in的数组,center作为 的参数project

经度-122.4167 和纬度37.7833的点位于旧金山。

http://www.openstreetmap.org/search?query=37.7833%2C%20-122.4167#map=10/37.7833/-122.4167

于 2015-11-15T03:36:57.623 回答