@@ -13,27 +13,182 @@ L.Control.Attribution.include({
1313} ) ;
1414L . Map . include ( {
1515 /*
16- * 获取精确的像素坐标.
17- * 当需要绘制比较平滑的曲线的时候可调用此方法代替latLngToContainerPoint
18- * @param latlng
19- */
16+ * 获取精确的像素坐标.
17+ * 当需要绘制比较平滑的曲线的时候可调用此方法代替latLngToContainerPoint
18+ * @param latlng
19+ */
2020 latLngToAccurateContainerPoint : function ( latlng ) {
2121 var projectedPoint = this . project ( L . latLng ( latlng ) ) ;
2222 var layerPoint = projectedPoint . _subtract ( this . getPixelOrigin ( ) ) ;
2323 return L . point ( layerPoint ) . add ( this . _getMapPanePos ( ) ) ;
2424 }
2525} ) ;
26+
27+ var projectionNames = [ 'EPSG4326' , 'EPSG3857' , 'EPSG3395' , 'EPSG900913' ] ;
28+
29+ projectionNames . forEach ( function ( projectionName ) {
30+ L . Util . extend ( L . CRS [ projectionName ] , {
31+ curScale : null ,
32+ preZoom : 0 ,
33+ latLngToPoint : function ( latlng , zoom ) {
34+ var projectedPoint = this . projection . project ( latlng ) ;
35+ var scale ;
36+ if ( this . curScale && this . prevZoom === zoom ) {
37+ scale = this . curScale ;
38+ } else {
39+ scale = this . scale ( zoom ) ;
40+ this . curScale = scale ;
41+ this . prevZoom = zoom ;
42+ }
43+ return this . transformation . _transform ( projectedPoint , scale ) ;
44+ }
45+ } ) ;
46+ } ) ;
47+
48+ L . Polyline . include ( {
49+ dirtyBounds : false ,
50+ getBounds : function ( ) {
51+ if ( this . dirtyBounds && this . _latlngs ) {
52+ this . _bounds = new L . latLngBounds ( ) ;
53+ for ( var i = 0 , len = this . _latlngs . length ; i < len ; i ++ ) {
54+ this . _bounds . extend ( this . _latlngs [ i ] ) ;
55+ }
56+ this . dirtyBounds = false ;
57+ }
58+ return this . _bounds ;
59+ } ,
60+ addLatLng : function ( latlng , latlngs ) {
61+ latlngs = latlngs || this . _defaultShape ( ) ;
62+ latlng = toLatLng ( latlng ) ;
63+ latlngs . push ( latlng ) ;
64+ this . dirtyBounds = true ;
65+ return this . redraw ( ) ;
66+ } ,
67+ _setLatLngs : function ( latlngs ) {
68+ this . _latlngs = this . _convertLatLngs ( latlngs ) ;
69+ } ,
70+ _convertLatLngs : function ( latlngs ) {
71+ var result = [ ] ,
72+ flat = L . LineUtil . isFlat ( latlngs ) ;
73+
74+ for ( var i = 0 , len = latlngs . length ; i < len ; i ++ ) {
75+ if ( flat ) {
76+ result [ i ] = toLatLng ( latlngs [ i ] ) ;
77+ this . dirtyBounds = true ;
78+ } else {
79+ result [ i ] = this . _convertLatLngs ( latlngs [ i ] ) ;
80+ }
81+ }
82+ return result ;
83+ } ,
84+ _project : function ( ) {
85+ var pxBounds = new L . Bounds ( ) ;
86+ this . _rings = [ ] ;
87+ this . _projectLatlngs ( this . _latlngs , this . _rings , pxBounds ) ;
88+ if ( pxBounds . isValid ( ) ) {
89+ this . _rawPxBounds = pxBounds ;
90+ this . _updateBounds ( ) ;
91+ }
92+ }
93+ } ) ;
94+
95+ L . GeoJSON . include ( {
96+ initialize : function ( geojson , options ) {
97+ L . Util . setOptions ( this , options ) ;
98+ this . _layers = { } ;
99+ this . defaultGeometryOptions = { } ;
100+ if ( geojson ) {
101+ this . addData ( geojson ) ;
102+ }
103+ } ,
104+ addData : function ( geojson ) {
105+ var features = Array . isArray ( geojson ) ? geojson : geojson . features ,
106+ i , len , feature ;
107+
108+ if ( features ) {
109+ for ( i = 0 , len = features . length ; i < len ; i ++ ) {
110+ feature = features [ i ] ;
111+ if ( feature . geometries || feature . geometry || feature . features || feature . coordinates ) {
112+ this . addData ( feature ) ;
113+ }
114+ }
115+ return this ;
116+ }
117+
118+ var options = this . options ;
119+
120+ if ( options . filter && ! options . filter ( geojson ) ) { return this ; }
121+ var geometry = geojson . type === 'Feature' ? geojson . geometry : geojson ;
122+ var layer = L . GeoJSON . geometryToLayer ( geojson , options ) ;
123+ if ( ! layer ) {
124+ return this ;
125+ }
126+ layer . feature = L . GeoJSON . asFeature ( geojson ) ;
127+
128+ layer . defaultOptions = layer . options ;
129+ var defaultGeometryOptions = this . defaultGeometryOptions [ geometry . type ] ;
130+ if ( defaultGeometryOptions ) {
131+ layer . commonOptions = defaultGeometryOptions ;
132+ } else {
133+ this . defaultGeometryOptions [ geometry . type ] = L . Util . extend ( { } , layer . defaultOptions ) ;
134+ }
135+ this . resetStyle ( layer ) ;
136+
137+ if ( options . onEachFeature ) {
138+ options . onEachFeature ( geojson , layer ) ;
139+ }
140+
141+ return this . addLayer ( layer ) ;
142+ } ,
143+ resetStyle : function ( layer ) {
144+ if ( layer === undefined ) {
145+ return this . eachLayer ( this . resetStyle , this ) ;
146+ }
147+ if ( layer . commonOptions ) {
148+ layer . options = layer . commonOptions
149+ } else {
150+ layer . options = L . Util . extend ( { } , layer . defaultOptions ) ;
151+ }
152+ this . _setLayerStyle ( layer , this . options . style ) ;
153+ return this ;
154+ }
155+ } ) ;
156+
26157wrapToGeoJSON ( [ L . Polyline , L . Polygon , L . Marker , L . CircleMarker , L . Circle , L . LayerGroup ] ) ;
27158
28159function wrapToGeoJSON ( objClassArray ) {
29- objClassArray . map ( ( objClass ) => {
30- objClass . defaultFunction = objClass . prototype . toGeoJSON ;
31- objClass . include ( {
32- toGeoJSON : function ( precision ) {
33- return objClass . defaultFunction . call ( this , precision || L . toGeoJSONPrecision || 15 ) ;
34- }
35- } )
36- return objClass ;
160+ objClassArray . map ( ( objClass ) => {
161+ objClass . defaultFunction = objClass . prototype . toGeoJSON ;
162+ objClass . include ( {
163+ toGeoJSON : function ( precision ) {
164+ return objClass . defaultFunction . call ( this , precision || L . toGeoJSONPrecision || 15 ) ;
165+ }
37166 } )
167+ return objClass ;
168+ } )
169+ }
38170
171+ function toLatLng ( a , b , c ) {
172+ if ( a instanceof L . latLng ) {
173+ return a ;
174+ }
175+ if ( Array . isArray ( a ) && typeof a [ 0 ] !== 'object' ) {
176+ if ( a . length === 3 ) {
177+ return new L . latLng ( a [ 0 ] , a [ 1 ] , a [ 2 ] ) ;
178+ }
179+ if ( a . length === 2 ) {
180+ return new L . latLng ( a [ 0 ] , a [ 1 ] ) ;
181+ }
182+ return null ;
183+ }
184+ if ( a === undefined || a === null ) {
185+ return a ;
186+ }
187+ if ( typeof a === 'object' && 'lat' in a ) {
188+ return new L . latLng ( a . lat , 'lng' in a ? a . lng : a . lon , a . alt ) ;
189+ }
190+ if ( b === undefined ) {
191+ return null ;
192+ }
193+ return new L . latLng ( a , b , c ) ;
39194}
0 commit comments