-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmapir.js
142 lines (131 loc) · 3.8 KB
/
mapir.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// function _extends() {
// _extends =
// Object.assign ||
// function (target) {
// for (var i = 1; i < arguments.length; i++) {
// var source = arguments[i];
// for (var key in source) {
// if (Object.prototype.hasOwnProperty.call(source, key)) {
// target[key] = source[key];
// }
// }
// }
// return target;
// };
// return _extends.apply(this, arguments);
// }
import React from 'react';
import ReactMapboxGl, {
Layer,
Feature,
GeoJSONLayer,
Source,
Image,
ZoomControl,
ScaleControl,
RotationControl,
Popup,
Cluster,
} from 'react-mapbox-gl';
import MapirRaster from './components/MapirRaster.js';
import MarkerComponent from './components/MarkerComponent.js';
import {
setRTLTextPlugin,
GeolocateControl,
AttributionControl,
} from 'mapbox-gl';
import DrawControl from 'react-mapbox-gl-draw';
import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css';
import 'mapbox-gl/dist/mapbox-gl.css';
import './assets/mapir.css';
setRTLTextPlugin(
'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js'
);
class Mapir extends React.Component {
constructor(props) {
super(props);
this.setAttribution = this.setAttribution.bind(this);
}
componentDidMount() {
fetch(`https://map.ir/vector/load?x-api-key=${this.props.apiKey}`, {
method: 'POST',
})
.then(function (res) {})
.catch(function (err) {});
}
setAttribution(map) {
if (this.props?.onStyleLoad) this.props.onStyleLoad();
if (this.props.userLocation) {
map.addControl(
new GeolocateControl({
positionOptions: {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 10000,
},
trackUserLocation: true,
})
);
}
const getElement = document.getElementsByClassName('mapboxgl-ctrl-logo');
// getElement[0].href = 'http://map.ir';
map.addControl(new AttributionControl({ compact: true }));
}
render() {
const Map = this.props.Map;
const onStyleLoadProp = this.props.onStyleLoad
return (
<Map
{...this.props}
style={
this.props.style ||
`https://map.ir/vector/styles/main/mapir-xyz-style.json`
}
minZoom={this.props.minZoom || 12}
center={this.props.center || [51.42047, 35.729054]}
tms={this.props.tms || true}
containerStyle={
this.props.containerStyle || {
height: '100vh',
width: '100vw',
}
}
onStyleLoad={(map) => {
this.setAttribution(map);
if(onStyleLoadProp) onStyleLoadProp(map);
}}
/>
);
// return React.createElement(
// Map,
// _extends({}, this.props, {
// style:
// this.props.style ||
// `https://map.ir/vector/styles/main/mapir-xyz-style.json`,
// minZoom: this.props.minZoom || 12,
// center: this.props.center || [51.42047, 35.729054],
// tms: true,
// containerStyle: this.props.containerStyle || {
// height: '100vh',
// width: '100vw',
// },
// onStyleLoad: (map) => this.setAttribution(map),
// })
// );
}
}
Mapir.Layer = Layer;
Mapir.Feature = Feature;
Mapir.Raster = MapirRaster;
Mapir.Source = Source;
Mapir.GeoJSONLayer = GeoJSONLayer;
Mapir.Image = Image;
Mapir.ZoomControl = ZoomControl;
Mapir.ScaleControl = ScaleControl;
Mapir.RotationControl = RotationControl;
Mapir.Popup = Popup;
Mapir.Marker = MarkerComponent;
Mapir.Cluster = Cluster;
Mapir.setToken = ReactMapboxGl;
Mapir.DrawControl = DrawControl;
export default Mapir;