@@ -27,7 +27,6 @@ import {
2727 withViewFn ,
2828 ThemeContext ,
2929 chartColorPalette ,
30- loadScript ,
3130} from "lowcoder-sdk" ;
3231import { getEchartsLocale , trans } from "i18n/comps" ;
3332import { ItemColorComp } from "comps/chartComp/chartConfigs/lineChartConfig" ;
@@ -49,7 +48,13 @@ let ChartTmpComp = (function () {
4948ChartTmpComp = withViewFn ( ChartTmpComp , ( comp ) => {
5049 const apiKey = comp . children . mapApiKey . getView ( ) ;
5150 const mode = comp . children . mode . getView ( ) ;
52- const onEvent = comp . children . onEvent . getView ( ) ;
51+ const mapCenterPosition = {
52+ lng : comp . children . mapCenterLng . getView ( ) ,
53+ lat : comp . children . mapCenterLat . getView ( ) ,
54+ }
55+ const mapZoomlevel = comp . children . mapZoomLevel . getView ( ) ;
56+ const onUIEvent = comp . children . onUIEvent . getView ( ) ;
57+ const onMapEvent = comp . children . onMapEvent . getView ( ) ;
5358
5459 const echartsCompRef = useRef < ReactECharts | null > ( ) ;
5560 const [ chartSize , setChartSize ] = useState < ChartSize > ( ) ;
@@ -81,15 +86,15 @@ ChartTmpComp = withViewFn(ChartTmpComp, (comp) => {
8186 //log.log("chart select change", param);
8287 if ( param . fromAction === "select" ) {
8388 comp . dispatch ( changeChildAction ( "selectedPoints" , getSelectedPoints ( param , option ) ) ) ;
84- onEvent ( "select" ) ;
89+ onUIEvent ( "select" ) ;
8590 } else if ( param . fromAction === "unselect" ) {
8691 comp . dispatch ( changeChildAction ( "selectedPoints" , getSelectedPoints ( param , option ) ) ) ;
87- onEvent ( "unselect" ) ;
92+ onUIEvent ( "unselect" ) ;
8893 }
8994 } ) ;
9095 // unbind
9196 return ( ) => echartsCompInstance ?. off ( "selectchanged" ) ;
92- } , [ mode , onEvent ] ) ;
97+ } , [ mode , onUIEvent ] ) ;
9398
9499 const echartsConfigChildren = _ . omit ( comp . children , echartsConfigOmitChildren ) ;
95100 const option = useMemo ( ( ) => {
@@ -103,36 +108,50 @@ ChartTmpComp = withViewFn(ChartTmpComp, (comp) => {
103108 return mapScriptLoaded || window ?. google ;
104109 } , [ mapScriptLoaded ] )
105110
106- const loadGoogleMapsData = ( ) => {
107- setTimeout ( ( ) => {
108- const echartsCompInstance = echartsCompRef ?. current ?. getEchartsInstance ( ) ;
109- if ( ! echartsCompInstance ) {
110- return _ . noop ;
111- }
111+ const loadGoogleMapData = ( ) => {
112+ const echartsCompInstance = echartsCompRef ?. current ?. getEchartsInstance ( ) ;
113+ if ( ! echartsCompInstance ) {
114+ return _ . noop ;
115+ }
112116
113- let mapInstance = undefined ;
114- mapInstance = echartsCompInstance ?. getModel ( ) ?. getComponent ( "gmap" ) ?. getGoogleMap ( ) ;
115- comp . dispatch ( changeChildAction ( "mapInstance" , mapInstance ) ) ;
116- } , 500 )
117+ comp . children . mapInstance . dispatch ( changeValueAction ( echartsCompInstance ) )
118+ onMapEvent ( 'mapReady' )
117119 }
118120
121+ const handleOnMapScriptLoad = ( ) => {
122+ setMapScriptLoaded ( true ) ;
123+ loadGoogleMapData ( ) ;
124+ }
125+
119126 useEffect ( ( ) => {
120127 if ( mode !== 'map' ) {
121- comp . dispatch ( changeChildAction ( "mapInstance" , undefined ) ) ;
128+ comp . children . mapInstance . dispatch ( changeValueAction ( undefined ) )
122129 return ;
123130 }
124131
125132 const gMapScript = loadGoogleMapsScript ( apiKey ) ;
126133 if ( isMapScriptLoaded ) {
127- loadGoogleMapsData ( ) ;
134+ handleOnMapScriptLoad ( ) ;
128135 return ;
129136 }
130- gMapScript . addEventListener ( 'load' , function ( ) {
131- setMapScriptLoaded ( true ) ;
132- loadGoogleMapsData ( ) ;
133- } ) ;
137+ gMapScript . addEventListener ( 'load' , handleOnMapScriptLoad ) ;
138+ return ( ) => {
139+ gMapScript . removeEventListener ( 'load' , handleOnMapScriptLoad ) ;
140+ }
134141 } , [ mode , apiKey , option ] )
135142
143+ useEffect ( ( ) => {
144+ if ( mode === 'map' ) {
145+ onMapEvent ( 'centerPositionChange' )
146+ }
147+ } , [ mode , mapCenterPosition . lat , mapCenterPosition . lng ] )
148+
149+ useEffect ( ( ) => {
150+ if ( mode === 'map' ) {
151+ onMapEvent ( 'zoomLevelChange' )
152+ }
153+ } , [ mode , mapZoomlevel ] )
154+
136155 return (
137156 < ReactResizeDetector
138157 onResize = { ( w , h ) => {
@@ -287,8 +306,38 @@ ChartComp = withMethodExposing(ChartComp, [
287306 name : "getMapInstance" ,
288307 } ,
289308 execute : ( comp ) => {
290- return comp . children . mapInstance . getView ( )
309+ return new Promise ( resolve => {
310+ let intervalCount = 0 ;
311+ const mapInstanceInterval = setInterval ( ( ) => {
312+ const instance = comp . children . mapInstance . getView ( ) ;
313+ const mapInstance = instance ?. getModel ( ) ?. getComponent ( "gmap" ) ?. getGoogleMap ( )
314+ if ( mapInstance || intervalCount === 10 ) {
315+ clearInterval ( mapInstanceInterval )
316+ resolve ( mapInstance )
317+ }
318+ intervalCount ++ ;
319+ } , 1000 ) ;
320+ } )
321+ }
322+ } ,
323+ {
324+ method : {
325+ name : "getMapZoomLevel" ,
326+ } ,
327+ execute : ( comp ) => {
328+ return comp . children . mapZoomLevel . getView ( ) ;
329+ }
330+ } ,
331+ {
332+ method : {
333+ name : "getMapCenterPosition" ,
291334 } ,
335+ execute : ( comp ) => {
336+ return {
337+ lng : comp . children . mapCenterLng . getView ( ) ,
338+ lat : comp . children . mapCenterLat . getView ( ) ,
339+ } ;
340+ }
292341 } ,
293342] )
294343
0 commit comments