This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmap-view.js
124 lines (102 loc) · 3.29 KB
/
map-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React from 'react';
import { connect } from 'react-redux';
import EsriLoaderReact from 'esri-loader-react';
import { MapLegend, setInitialLegend } from '../../../src/';
import isWebGLEnabled from 'is-webgl-enabled';
import isMobile from 'is-mobile';
class MapUi extends React.PureComponent {
initialState = {
title: null
};
state = this.initialState;
getUrlParameter = name => {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
loadMap = ({loadedModules: [Map, View, MapImageLayer, FeatureLayer], containerNode}) => {
const { mapId, initLegend } = this.props;
const layer2 = new MapImageLayer({
url: 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer',
visible: false
});
const layer3 = new MapImageLayer({
url: 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer'
});
const layer4 = new FeatureLayer({
url: 'https://services2.arcgis.com/j80Jz20at6Bi0thr/arcgis/rest/services/HawaiiLavaFlowHazardZones/FeatureServer/0'
});
const view = new View({
container: containerNode,
map: new Map({
basemap: 'streets',
layers: [layer2, layer3, layer4]
}),
padding: { right: 280 }
});
// calling this initialises the legend control
initLegend(view, mapId);
layer2.when(function(lyr) {
view.goTo(lyr.fullExtent);
});
}
loadWebmap = ({loadedModules: [Map, MapView, WebMap], containerNode}) => {
const { mapId, initLegend } = this.props;
const view = new MapView({
container: containerNode,
map: new WebMap({
portalItem: {
id: this.getUrlParameter('webmap')
}
}),
padding: { right: 280 }
});
view.map.portalItem.when((portalItem) => {
this.setState({ title: portalItem.title });
});
// calling this initialises the legend control
initLegend(view, mapId);
}
render() {
const { mapId, options } = this.props;
const { title } = this.state;
const webMapId = this.getUrlParameter('webmap');
const modules = webMapId
? [
'esri/Map',
'esri/views/MapView',
'esri/WebMap',
]
: [
'esri/Map',
isWebGLEnabled() && !isMobile() ? 'esri/views/SceneView' : 'esri/views/MapView',
'esri/layers/MapImageLayer',
'esri/layers/FeatureLayer',
];
return (
<EsriLoaderReact
mapContainerClassName='fullSize'
options={options}
modulesToLoad={modules}
onReady={webMapId ? this.loadWebmap : this.loadMap}
>
{this.props.legend ? <MapLegend className='thirtyPercent' mapId={mapId} title={title} /> : null}
</EsriLoaderReact>
);
}
}
const mapStateToProps = (state, props) => {
const legend = state.mapLegendConfig.legends[props.mapId];
return {
legend,
};
};
const mapDispatchToProps = dispatch => {
return {
initLegend: (view, mapId) => {
dispatch(setInitialLegend(view, mapId));
}
};
};
export default connect(mapStateToProps, mapDispatchToProps)(MapUi);