Skip to content

Commit 2ad52f9

Browse files
committed
Add region map component
1 parent 848b4b8 commit 2ad52f9

11 files changed

+688
-1
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GOOGLE_CLIENT_ID=
2+
GOOGLE_MAP_KEY=

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
.idea
1212
*.iml
1313
.DS_Store
14+
.env
1415
.env.local
1516
.env.development.local
1617
.env.test.local

config/webpack.config.dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ module.exports = {
255255
new HtmlWebpackPlugin({
256256
inject: true,
257257
template: paths.appHtml,
258+
env: process.env,
258259
}),
259260
// Add module names to factory functions so they appear in browser profiler.
260261
new webpack.NamedModulesPlugin(),

config/webpack.config.prod.js

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ module.exports = {
287287
minifyCSS: true,
288288
minifyURLs: true,
289289
},
290+
env: process.env,
290291
}),
291292
// Makes some environment variables available to the JS code, for example:
292293
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"test": "node scripts/test.js --env=jsdom"
5656
},
5757
"devDependencies": {
58+
"@types/googlemaps": "^3.30.0",
5859
"@types/jest": "^21.1.5",
5960
"@types/node": "^8.0.50",
6061
"@types/react": "^16.0.22",

public/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
To begin the development, run `npm start` or `yarn start`.
3838
To create a production bundle, use `npm run build` or `yarn build`.
3939
-->
40+
41+
<script src="https://apis.google.com/js/platform.js"></script>
42+
<script src="https://maps.googleapis.com/maps/api/js?key=<%= htmlWebpackPlugin.options.env.GOOGLE_MAP_KEY %>"></script>
4043
</body>
4144
</html>

src/components/Join.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './Join.scss';
33
import { Button, Card, Col, Form, Icon, Input, Layout, Row } from 'antd';
44
import { Link } from 'react-router-dom';
55
import Sidebar from './Sidebar';
6+
import RegionMap from './RegionMap';
67

78
interface State {
89
//
@@ -46,7 +47,10 @@ export default class Article extends React.Component {
4647
<Input prefix={<Icon type="user" />} placeholder="Telegram Username (不带 @)" />
4748
</Form.Item>
4849

49-
// Map Here
50+
<Form.Item label="主要活动区域">
51+
<p>如你发现战区与现实的行政区矛盾,还是请按照此地图中的战区选择</p>
52+
<RegionMap />
53+
</Form.Item>
5054

5155
<Form.Item label="其他说明">
5256
<p>可以在此简述一下你是怎么入坑的、你的入坑时间、是否认识其他特工等</p>

src/components/RegionMap.css

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#region-map-container {
2+
position: relative;
3+
}
4+
5+
#region-map {
6+
height: 300px;
7+
}
8+
9+
#region-map-hint {
10+
background: #ffffff;
11+
padding: 8px;
12+
font-size: 11px;
13+
line-height: 14px;
14+
position: absolute;
15+
top: 10px;
16+
border-radius: 2px;
17+
box-shadow: rgba(0, 0, 0, 0.3) 0 1px 4px -1px;
18+
display: none;
19+
}

src/components/RegionMap.tsx

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react';
2+
import './RegionMap.css';
3+
import regions from './regions';
4+
5+
export default class RegionMap extends React.Component {
6+
state = {
7+
hint: '',
8+
hintStyle: {}
9+
};
10+
11+
setHint(hint: string) {
12+
const rel = document.querySelector('.gm-style-mtc') as HTMLElement;
13+
14+
this.setState({
15+
hint,
16+
hintStyle: {
17+
left: rel.parentElement !== null ? rel.parentElement!.offsetWidth + 20 : 0,
18+
display: hint === '' ? 'none' : 'block'
19+
}
20+
});
21+
}
22+
23+
componentDidMount() {
24+
if (typeof google === 'undefined') {
25+
throw new Error('Google Maps SDK not loaded');
26+
}
27+
28+
let map = new google.maps.Map(document.getElementById('region-map'), {
29+
center: new google.maps.LatLng(23.1, 113.3),
30+
zoom: 9,
31+
mapTypeId: google.maps.MapTypeId.ROADMAP
32+
});
33+
34+
map.data.addGeoJson(regions);
35+
36+
map.data.setStyle((feature: google.maps.Data.Feature) => {
37+
let color = feature.getProperty('color');
38+
return {
39+
fillColor: color,
40+
strokeColor: color,
41+
strokeWeight: 2
42+
};
43+
});
44+
45+
map.data.addListener('mouseover', (event: google.maps.Data.MouseEvent) => {
46+
map.data.revertStyle();
47+
map.data.overrideStyle(event.feature, {
48+
strokeColor: 'white',
49+
strokeWeight: 3,
50+
zIndex: google.maps.Marker.MAX_ZINDEX
51+
});
52+
this.setHint(event.feature.getProperty('name'));
53+
});
54+
55+
map.data.addListener('mouseout', (event: google.maps.Data.MouseEvent) => {
56+
map.data.revertStyle();
57+
this.setHint('');
58+
});
59+
60+
map.data.addListener('click', (event: google.maps.Data.MouseEvent) => {
61+
// this.$emit('select', event);
62+
});
63+
}
64+
65+
render() {
66+
return (
67+
<div id="region-map-container">
68+
<div id="region-map" />
69+
<div id="region-map-hint" style={this.state.hintStyle}>{this.state.hint}</div>
70+
</div>
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)