使用者IP取得位置與天氣資訊

結論

用IP只能取得大概位置,而且經常會出錯,我家在新北蘆洲,但時常取得位置位於新北

不確定是否為固定IP的緣故,否則外送平台網頁也不需要彈跳請求取得位置這種麻煩的事情

純屬娛樂性質,以下會有既有的回傳Json格式,

API key申請也是相當容易,不需要信用卡就可以有免費額度可以玩玩

Demo: https://tool.tyeydy.com/weather/ip

API

透過IP 查詢 縣市資料

https://www.geoapify.com

https://api.geoapify.com/v1/ipinfo?apiKey={key}

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
{
"city": {
"names": {
"en": "Taipei"
},
"name": "Taipei"
},
"continent": {
"code": "AS",
"geoname_id": 6255147,
"names": {
"de": "Asien",
"en": "Asia",
"es": "Asia",
"fa": " آسیا",
"fr": "Asie",
"ja": "アジア大陸",
"ko": "아시아",
"pt-BR": "Ásia",
"ru": "Азия",
"zh-CN": "亚洲"
},
"name": "Asia"
},
"country": {
"geoname_id": 1668284,
"iso_code": "TW",
"names": {
"de": "Republik China (Taiwan)",
"en": "Taiwan",
"es": "Taiwán (Provincia de China)",
"fa": "تایوان",
"fr": "Taïwan, Province de Chine",
"ja": "台湾",
"ko": "대만",
"pt-BR": "Taiwan",
"ru": "Тайвань",
"zh-CN": "台湾"
},
"name": "Taiwan",
"name_native": "臺灣",
"phone_code": "886",
"capital": "Taipei",
"currency": "TWD",
"flag": "🇹🇼",
"languages": [
{
"iso_code": "zh",
"name": "Chinese",
"name_native": "中文"
}
]
},
"location": {
"latitude": 25.033,
"longitude": 121.565
},
"subdivisions": [
{
"names": {
"en": "Taiwan"
}
}
],
"state": {
"name": "Taiwan"
},
"datasource": [
{
"name": "IP to City Lite",
"attribution": "<a href='https://db-ip.com'>IP Geolocation by DB-IP</a>",
"license": "Creative Commons Attribution License"
}
],
"ip": "***.***.***.***"
}

透過經緯度取得氣象資料

https://openweathermap.org/api

https://api.openweathermap.org/data/2.5/weather?q=Taipei&appid={key}

https://api.openweathermap.org/data/2.5/weather??lat=24.1632&lon=120.675&appid={key}

必須要有Donate 才能使用 3.0的 API,裡面的API種類滿多樣的,可以玩看看

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
{
"coord": {
"lon": 121.2262,
"lat": 24.959
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02n"
}
],
"base": "stations",
"main": {
"temp": 24.09,
"feels_like": 24.54,
"temp_min": 22.83,
"temp_max": 24.77,
"pressure": 1010,
"humidity": 76
},
"visibility": 10000,
"wind": {
"speed": 5.14,
"deg": 60
},
"clouds": {
"all": 20
},
"dt": 1663772686,
"sys": {
"type": 2,
"id": 2005987,
"country": "TW",
"sunrise": 1663710189,
"sunset": 1663754020
},
"timezone": 28800,
"id": 1668664,
"name": "Songwu",
"cod": 200
}