Skip to content

Commit

Permalink
【fix】scale在地图边缘是NaN问题
Browse files Browse the repository at this point in the history
  • Loading branch information
songyumeng committed Sep 8, 2023
1 parent f96f088 commit 4002ed5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/mapboxgl/web-map/control/scale/ScaleViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ export default class ScaleViewModel extends mapboxgl.Evented {
updateScale(map, options) {
const maxWidth = (options && options.maxWidth) || 100;

const y = map._container.clientHeight / 2;
const maxMeters = this._getDistance(map.unproject([0, y]), map.unproject([maxWidth, y]));
const centerPoint = map.project(map.crs.getLngLatCenter());
const left = map.unproject([centerPoint.x, centerPoint.y]);
let right = map.unproject([centerPoint.x + maxWidth, centerPoint.y]);
if (right.lng === left.lng) {
right = map.unproject([centerPoint.x - maxWidth, centerPoint.y]);
}
const maxMeters = this._getDistance(left, right);

if (options && options.unit === 'imperial') {
const maxFeet = 3.2808 * maxMeters;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/mocks/crs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ var Evented = require('mapbox-gl/src/util/evented');
class CRS extends Evented {
constructor(options) {
super();
this.unit='m';
}

getExtent() {
return [-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892];
}
u
getOrigin() { return jest.fn()}
getLngLatCenter() { return [0,0];}
}
CRS.get = baseProjection => {
return {
Expand Down
11 changes: 6 additions & 5 deletions test/unit/mocks/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var LngLatBounds = require('mapbox-gl/src/geo/lng_lat_bounds');
var Evented = require('mapbox-gl/src/util/evented');
// var Transform = require('mapbox-gl/src/geo/transform');
var util = require('mapbox-gl/src/util/util');
var CRS = require('./crs');
// var Style = require('mapbox-gl/src/style/style');

// var Style = require('./style');
Expand Down Expand Up @@ -80,7 +81,7 @@ var Map = function (options) {
var ne = new LngLat(-73.9397, 40.8002);
var llb = new LngLatBounds(sw, ne);
this.bounds = this.options.bounds || llb;

this.crs = new CRS();
try {
this.center = this.options.center ? new LngLat(this.options.center.lng, this.options.center.lat) : new LngLat(0, 0);
} catch (e) {
Expand Down Expand Up @@ -586,11 +587,11 @@ var Map = function (options) {
};
};
this.getCRS = () => {
return {
getExtent: () => jest.fn()
};
return this.crs;
};
this.setCRS = () => {
this.crs = new CRS();
};
this.setCRS = () => {};
this.flyTo = options => {};
this.setRenderWorldCopies = epsgCode => {};
this.triggerRepaint = () => {};
Expand Down

0 comments on commit 4002ed5

Please sign in to comment.