@@ -483,13 +483,21 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
483483 /**
484484 * Set a new center to the map.
485485 * @param {Coordinate } center
486+ * @param {Object } [padding]
487+ * @param {Number } [padding.paddingLeft] - Sets the amount of padding in the left of a map container
488+ * @param {Number } [padding.paddingTop] - Sets the amount of padding in the top of a map container
489+ * @param {Number } [padding.paddingRight] - Sets the amount of padding in the right of a map container
490+ * @param {Number } [padding.paddingBottom] - Sets the amount of padding in the bottom of a map container
486491 * @return {Map } this
487492 */
488- setCenter ( center ) {
493+ setCenter ( center , padding ) {
489494 if ( ! center ) {
490495 return this ;
491496 }
492497 center = new Coordinate ( center ) ;
498+ if ( padding ) {
499+ center = this . _getCenterByPadding ( center , this . getZoom ( ) , padding ) ;
500+ }
493501 const projection = this . getProjection ( ) ;
494502 const pcenter = projection . project ( center ) ;
495503 if ( ! this . _verifyExtent ( pcenter ) ) {
@@ -856,22 +864,51 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
856864 return this ;
857865 }
858866
859-
867+ /**
868+ * Get the padding Size
869+ * @param {Object } options
870+ * @param {Number } [options.paddingLeft] - Sets the amount of padding in the left of a map container
871+ * @param {Number } [options.paddingTop] - Sets the amount of padding in the top of a map container
872+ * @param {Number } [options.paddingRight] - Sets the amount of padding in the right of a map container
873+ * @param {Number } [options.paddingBottom] - Sets the amount of padding in the bottom of a map container
874+ * @returns {Object|null }
875+ */
876+ _getPaddingSize ( options = { } ) {
877+ if ( options [ 'paddingLeft' ] || options [ 'paddingTop' ] || options [ 'paddingRight' ] || options [ 'paddingBottom' ] ) {
878+ return {
879+ width : ( options [ 'paddingLeft' ] || 0 ) + ( options [ 'paddingRight' ] || 0 ) ,
880+ height : ( options [ 'paddingTop' ] || 0 ) + ( options [ 'paddingBottom' ] || 0 )
881+ } ;
882+ }
883+ return null ;
884+ }
860885 /**
861886 * Caculate the zoom level that contains the given extent with the maximum zoom level possible.
862887 * @param {Extent } extent
863888 * @param {Boolean } isFraction - can return fractional zoom
889+ * @param {Object } [padding] [padding] - padding
890+ * @param {Object } [padding.paddingLeft] - Sets the amount of padding in the left of a map container
891+ * @param {Object } [padding.paddingTop] - Sets the amount of padding in the top of a map container
892+ * @param {Object } [padding.paddingRight] - Sets the amount of padding in the right of a map container
893+ * @param {Object } [padding.paddingBottom] - Sets the amount of padding in the bottom of a map container
864894 * @return {Number } zoom fit for scale starting from fromZoom
865895 */
866- getFitZoom ( extent , isFraction ) {
896+ getFitZoom ( extent , isFraction , padding ) {
867897 if ( ! extent || ! ( extent instanceof Extent ) ) {
868898 return this . _zoomLevel ;
869899 }
870900 //It's a point
871901 if ( extent [ 'xmin' ] === extent [ 'xmax' ] && extent [ 'ymin' ] === extent [ 'ymax' ] ) {
872902 return this . getMaxZoom ( ) ;
873903 }
874- const size = this . getSize ( ) ;
904+ let size = this . getSize ( ) ;
905+ const paddingSize = this . _getPaddingSize ( padding ) ;
906+ if ( paddingSize ) {
907+ size = {
908+ width : size . width - ( paddingSize . width || 0 ) ,
909+ height : size . height - ( paddingSize . height || 0 )
910+ } ;
911+ }
875912 const containerExtent = extent . convertTo ( p => this . coordToPoint ( p ) ) ;
876913 const w = containerExtent . getWidth ( ) ,
877914 h = containerExtent . getHeight ( ) ;
@@ -941,19 +978,61 @@ class Map extends Handlerable(Eventable(Renderable(Class))) {
941978 return res / max ;
942979 }
943980
981+ /**
982+ * Get center by the padding.
983+ * @private
984+ * @param {Coordinate } center
985+ * @param {Number } zoom
986+ * @param {Object } padding
987+ * @param {Number } [padding.paddingLeft] - Sets the amount of padding in the left of a map container
988+ * @param {Number } [padding.paddingTop] - Sets the amount of padding in the top of a map container
989+ * @param {Number } [padding.paddingRight] - Sets the amount of padding in the right of a map container
990+ * @param {Number } [padding.paddingBottom] - Sets the amount of padding in the bottom of a map container
991+ * @return {Coordinate }
992+ */
993+ _getCenterByPadding ( center , zoom , padding = { } ) {
994+ const point = this . coordinateToPoint ( center , zoom ) ;
995+ const { paddingLeft = 0 , paddingRight = 0 , paddingTop = 0 , paddingBottom = 0 } = padding ;
996+ let pX = 0 ;
997+ let pY = 0 ;
998+ if ( paddingLeft || paddingRight ) {
999+ pX = ( paddingRight - paddingLeft ) / 2 ;
1000+ }
1001+ if ( paddingTop || paddingBottom ) {
1002+ pY = ( paddingTop - paddingBottom ) / 2 ;
1003+ }
1004+ const newPoint = new Point ( {
1005+ x : point . x + pX ,
1006+ y : point . y + pY
1007+ } ) ;
1008+ return this . pointToCoordinate ( newPoint , zoom ) ;
1009+ }
1010+
9441011 /**
9451012 * Set the map to be fit for the given extent with the max zoom level possible.
9461013 * @param {Extent } extent - extent
9471014 * @param {Number } zoomOffset - zoom offset
1015+ * @param {Object } [options={}] - options
1016+ * @param {Object } [options.animation]
1017+ * @param {Object } [options.duration]
1018+ * @param {Object } [options.zoomAnimationDuration]
1019+ * @param {Object } [options.easing='out']
1020+ * @param {Number } [options.paddingLeft] - Sets the amount of padding in the left of a map container
1021+ * @param {Number } [options.paddingTop] - Sets the amount of padding in the top of a map container
1022+ * @param {Number } [options.paddingRight] - Sets the amount of padding in the right of a map container
1023+ * @param {Number } [options.paddingBottom] - Sets the amount of padding in the bottom of a map container
9481024 * @return {Map } - this
9491025 */
9501026 fitExtent ( extent , zoomOffset , options = { } , step ) {
9511027 if ( ! extent ) {
9521028 return this ;
9531029 }
9541030 extent = new Extent ( extent , this . getProjection ( ) ) ;
955- const zoom = this . getFitZoom ( extent ) + ( zoomOffset || 0 ) ;
956- const center = extent . getCenter ( ) ;
1031+ const zoom = this . getFitZoom ( extent , false , options ) + ( zoomOffset || 0 ) ;
1032+ let center = extent . getCenter ( ) ;
1033+ if ( this . _getPaddingSize ( options ) ) {
1034+ center = this . _getCenterByPadding ( center , zoom , options ) ;
1035+ }
9571036 if ( typeof ( options [ 'animation' ] ) === 'undefined' || options [ 'animation' ] )
9581037 return this . _animateTo ( {
9591038 center,
@@ -2349,7 +2428,7 @@ Map.include(/** @lends Map.prototype */{
23492428 * Convert a geographical coordinate to the container point. <br>
23502429 * Batch conversion for better performance <br>
23512430 * A container point is a point relative to map container's top-left corner. <br>
2352- * @param {Coordinate[] } - coordinates
2431+ * @param {Coordinate[] } Coordinate - coordinates
23532432 * @param {Number } [zoom=undefined] - zoom level
23542433 * @return {Point[] }
23552434 * @function
0 commit comments