1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
8
+ < title > Document</ title >
9
+ < style >
10
+ .btn {
11
+ height : 80px ;
12
+ width : 100px ;
13
+ font-size : 32px ;
14
+ padding : 4px ;
15
+ background-color : aquamarine;
16
+ color : black;
17
+ overflow : hidden;
18
+ }
19
+ </ style >
20
+ </ head >
21
+
22
+ < body >
23
+ < h1 > Man i need your location plz give me</ h1 >
24
+ < button class ="btn " onclick ="getLocation() "> Click</ button >
25
+ < br />
26
+ Lat / Lon
27
+ < h2 id ="locationContainer "> </ h2 >
28
+ Ip Address
29
+ < h2 id ="ipContainer "> </ h2 >
30
+ Reverse
31
+ < h2 id ="exactContainer "> </ h2 >
32
+ < script >
33
+ var locationUrl = 'https://ipinfo.io/json' ;
34
+ var locationContainer = document . getElementById ( 'locationContainer' ) ;
35
+ var ipContainer = document . getElementById ( 'ipContainer' ) ;
36
+ var exactContainer = document . getElementById ( 'exactContainer' ) ;
37
+ var la = '' ;
38
+ var lo = '' ;
39
+ var lalo ;
40
+ var longi
41
+ var lati
42
+
43
+ if ( navigator . geolocation ) {
44
+ navigator . geolocation . getCurrentPosition ( successFunction ) ;
45
+ }
46
+ //Get latitude and longitude;
47
+ function successFunction ( position ) {
48
+ lati = position . coords . latitude ;
49
+ longi = position . coords . longitude ;
50
+
51
+ var reverse = `https://us1.locationiq.org/v1/reverse.php?key=9a8051364dc080&lat=${ lati } &lon=${ longi } &format=json` ;
52
+
53
+ fetch ( reverse ) // Call the fetch function passing the url of the API as a parameter
54
+ . then ( ( resp ) => resp . json ( ) )
55
+ . then ( function ( resp ) {
56
+ exactContainer . innerHTML = resp . display_name ;
57
+ } )
58
+ . catch ( function ( ) {
59
+ // This is where you run code if the server returns any errors
60
+ } ) ;
61
+ }
62
+
63
+
64
+ function getLocation ( ) {
65
+ fetch ( locationUrl ) // Call the fetch function passing the url of the API as a parameter
66
+ . then ( ( resp ) => resp . json ( ) )
67
+ . then ( function ( resp ) {
68
+ //console.log(resp.city);
69
+ ipContainer . innerHTML = resp . ip ;
70
+ locationContainer . innerHTML = resp . loc ;
71
+ lalo = resp . loc . split ( ',' ) ;
72
+ } )
73
+ . catch ( function ( ) {
74
+ // This is where you run code if the server returns any errors
75
+ } ) ;
76
+
77
+ }
78
+ // geolocation
79
+ // 需要先请求是否允许。
80
+ // 然后getCurrentPosition会返回一个低精度的值。
81
+ // position.coords.latitude/position.coords
82
+
83
+ // navigator.geolocation.watchPosition(()=>{
84
+ // dosomething////
85
+ // })
86
+ // 可以用这个来做监视器。
87
+ // watchPosition() 函数会返回一个 ID,唯一地标记该位置监视器。您可以将这个 ID 传给 clearWatch() 函数来停止监视用户位置。
88
+ // navigator.geolocation.clearWatch(watchID);
89
+
90
+ // 传参可以调整返回结果
91
+ // getCurrentPosition() 和 watchPosition() 都接受一个成功回调、一个可选的失败回调和一个可选的 PositionOptions 对象。
92
+
93
+ </ script >
94
+ </ body >
95
+
96
+ </ html >
0 commit comments