-
Notifications
You must be signed in to change notification settings - Fork 284
/
LocationService.ts
executable file
·60 lines (48 loc) · 1.77 KB
/
LocationService.ts
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
import Utils = require('./Utils');
//http://lbs.qq.com/tool/component-geolocation.html
export class LocationData{
module: string;//geolocation
type: string;//h5,ip
adcode: string;//110105
nation: string;//中国
province: string;//北京市
city: string;//北京市
district: string;//朝阳区
addr: string;//朝阳区崔各庄乡顺白路何各庄村公交站西南
lat: number;
lng: number;
accuracy: number;//25
}
const targetOrigin:string = 'https://apis.map.qq.com';
var iframe = document.createElement("iframe");
class LocationService{
private static init(){
window.addEventListener('message', function(event){
var data:LocationData = event.data;
if(data && data.module === 'geolocation'){
Utils.publish('location', event.data);
}
}, false);
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("scrolling", "no");
iframe.style.display = "none";
iframe.setAttribute("src", `${targetOrigin}/tools/geolocation?key=YLZBZ-XDPKU-LWMV6-2WNPB-PL5W5-H6BGL&referer=WebGlobe`);
document.body.appendChild(iframe);
}
public static getLocation(){
iframe.contentWindow.postMessage('getLocation', targetOrigin);
}
public static getRobustLocation(){
iframe.contentWindow.postMessage('getLocation.robust', targetOrigin);
}
public static watchPosition(){
iframe.contentWindow.postMessage('watchPosition', targetOrigin);
}
public static clearWatch(){
iframe.contentWindow.postMessage('clearWatch', targetOrigin);
}
}
(LocationService as any).init();
export default LocationService;