-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.jsx
62 lines (52 loc) · 1.3 KB
/
Location.jsx
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
export default class Location extends Component {
static propTypes = {
children: PropTypes.array.isRequired
}
state = {
address: null
}
componentDidMount = () => {
this.map = this.props.map;
this.getLocation()
}
// 浏览器定位
getLocationByGeo = () => {
if (!window.BMap) {
throw new Error('BMap is not an Object.');
}
return new Promise((resolve, reject) => {
const geoLocation = new window.BMap.Geolocation();
geoLocation.enableSDKLocation();
geoLocation.getCurrentPosition(function getCurrPos(r) {
if (this.getStatus() === window.BMAP_STATUS_SUCCESS) {
resolve(r);
} else {
reject();
}
});
});
}
getLocationByIP = () => {
if (!window.BMap) {
throw new Error('BMap is not an Object.');
}
return new Promise((resolve) => {
const localCity = new window.BMap.LocalCity();
localCity.get((r) => { resolve(r); });
});
};
getLocation = async () => {
try {
const r = await getLocationByGeo();
return Promise.resolve(r);
} catch (e) {
const r = await getLocationByIP();
return Promise.resolve(r);
}
};
render() {
return children;
}
}