/
Location.cs
262 lines (244 loc) · 10.2 KB
/
Location.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) Aurora Studio. All rights reserved.
//
// Licensed under the MIT License. See LICENSE in the project root for license information.
using Com.Aurora.AuWeather.Models.HeWeather;
using Com.Aurora.Shared.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Com.Aurora.AuWeather.Core.Models.Amap.JsonContract;
using Com.Aurora.AuWeather.Core.Models.IP;
using Com.Aurora.AuWeather.Core.Models;
using Com.Aurora.AuWeather.Core.Models.OpenStreetMap;
using Com.Aurora.Shared.Extensions;
using Com.Aurora.AuWeather.Core.SQL;
using System.Linq.Expressions;
namespace Com.Aurora.AuWeather.Models
{
public class Location
{
public override string ToString()
{
return string.Format("{0}, {1}", Latitude > 0 ? Latitude.ToString("0.0") + "° N" : Latitude < 0 ? (-Latitude).ToString("0.0") + "° S" : "0.0°", Longitude > 0 ? Longitude.ToString("0.0") + "° E" : Longitude < 0 ? (-Longitude).ToString("0.0") + "° W" : "0.0°");
}
private float latitude;
private float longitude;
/// <summary>
/// -90.0°~90.0°, negative-south, positive-north
/// </summary>
public float Latitude
{
get
{
return latitude;
}
set
{
if (value < -90 || value > 90)
latitude = 0;
else
latitude = value;
}
}
/// <summary>
/// -180.0°~180.0°, any input will be converted into this range. negative-west, poitive-east
/// </summary>
public float Longitude
{
get
{
return longitude;
}
set
{
var p = value % 360;
if (p > 180)
{
p -= 360;
}
else if (p < -180)
{
p += 360;
}
longitude = p;
}
}
/// <summary>
///
/// </summary>
/// <param name="lat">纬度</param>
/// <param name="lon">经度</param>
public Location(float lat, float lon)
{
this.Latitude = lat;
this.Longitude = lon;
}
private const double EARTH_RADIUS = 6378.137;//地球半径
/// <summary>
/// 计算两点距离
/// </summary>
/// <param name="source"></param>
/// <param name="dest"></param>
/// <returns></returns>
public static float CalcDistance(float lat, float lon, Location dest)
{
try
{
var lat1 = Tools.DegreesToRadians(lat);
var lat2 = Tools.DegreesToRadians(dest.Latitude);
var a = lat1 - lat2;
var b = Tools.DegreesToRadians(lon) - Tools.DegreesToRadians(dest.Longitude);
var s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(b / 2), 2)));
s *= EARTH_RADIUS;
return (float)Math.Round(s * 10000) / 10000f;
}
catch (Exception)
{
return float.MaxValue;
}
}
public static City GetNearsetLocation(Location source)
{
var list = SQLAction.GetAll();
float dis = float.MaxValue;
City fi = null;
foreach (var item in list)
{
var cal = CalcDistance(item.Latitude, item.Longitude, source);
if (dis > cal)
{
dis = cal;
fi = item;
}
}
return fi;
}
public static async Task<AmapContract> AmapReGeoAsync(double latitude, double longitude)
{
var requestUrl = "http://restapi.amap.com/v3/geocode/regeo?key={0}&location={1},{2}&radius=1000&extensions=all&batch=false&roadlevel=1";
// 高德经纬度颠倒
var result = await ApiRequestHelper.RequestWithFormattedUrlAsync(requestUrl, new string[] { Key.amap, longitude.ToString("0.######"), latitude.ToString("0.######") });
// json 生成类
if (result == null)
return null;
var amapRes = JsonHelper.FromJson<AmapContract>(result);
return amapRes;
}
public static async Task<OpenCageContract> OpenMapReGeoAsync(double latitude, double longitude)
{
var OpenStreetUrl = "http://api.opencagedata.com/geocode/v1/json?q={0}+{1}&key={2}&language=zh-cn";
var result = await ApiRequestHelper.RequestWithFormattedUrlAsync(OpenStreetUrl, new string[] { latitude.ToString("0.######"), longitude.ToString("0.######"), Key.OpenCage });
// json 生成类
if (result == null)
return null;
var omapRes = JsonHelper.FromJson<OpenCageContract>(result);
return omapRes;
}
public async static Task<HeWeather.Location> ReGeobyIpAsync()
{
var ipUrl = "http://api.ip138.com/query/?token={0}";
var result = await ApiRequestHelper.RequestWithFormattedUrlAsync(ipUrl, new string[] { Key.ip138 });
if (result == null)
return null;
var ipRes = JsonHelper.FromJson<IpContract>(result);
var city = await Request.RequestbyIpAsync(ipRes.ip);
var fetchresult = HeWeatherModel.Generate(city, DataSource.HeWeather);
return fetchresult.Location;
}
public async static Task<string> HeWeatherGeoLookup(string name)
{
var url = "https://free-api.heweather.com/v5/search?key={0}&city={1}";
var result = await ApiRequestHelper.RequestWithFormattedUrlAsync(url, new string[] { Key.ip138, name });
if (result == null)
return null;
var rees = JsonHelper.FromJson<HeWeatherCity>(result);
if (!rees.HeWeather5.IsNullorEmpty())
return rees.HeWeather5[0].basic.id;
else
{
return "";
}
}
public async static Task<City> ReverseGeoCode(float lat, float lon, LocateRoute[] routes)
{
City final = null;
foreach (var r in routes)
{
switch (r)
{
case LocateRoute.unknown:
var near = GetNearsetLocation(new Location(lat, lon));
final = near;
break;
case LocateRoute.Amap:
var acontract = await AmapReGeoAsync(lat, lon);
if (acontract != null)
{
var li = new City
{
CityEn = acontract.regeocode.addressComponent.district,
CityZh = acontract.regeocode.addressComponent.district,
LeaderEn = acontract.regeocode.addressComponent.city,
LeaderZh = acontract.regeocode.addressComponent.city,
CountryCode = "CN",
CountryEn = acontract.regeocode.addressComponent.country,
//Id = await Models.Location.HeWeatherGeoLookup(acontract.regeocode.addressComponent.district),
Latitude = (float)lat,
Longitude = (float)lon,
ProvinceEn = acontract.regeocode.addressComponent.province,
ProvinceZh = acontract.regeocode.addressComponent.province
};
final = li;
}
break;
case LocateRoute.Omap:
var ocontract = await OpenMapReGeoAsync(lat, lon);
if (ocontract != null && !ocontract.results.IsNullorEmpty())
{
var li = new City
{
CityEn = ocontract.results[0].components.county,
CityZh = ocontract.results[0].components.county,
LeaderEn = ocontract.results[0].components.region,
LeaderZh = ocontract.results[0].components.region,
CountryCode = ocontract.results[0].components.country_code,
CountryEn = ocontract.results[0].components.country,
//Id = await HeWeatherGeoLookup(ocontract.results[0].components.county),
Latitude = lat,
Longitude = lon,
ProvinceEn = ocontract.results[0].components.state,
ProvinceZh = ocontract.results[0].components.state
};
final = li;
}
break;
case LocateRoute.IP:
var id = await ReGeobyIpAsync();
if (id != null)
{
final = SQLAction.Find(id.CityId);
}
break;
case LocateRoute.Gmap:
break;
default:
break;
}
if (final != null)
{
break;
}
}
if (final != null)
{
return final;
}
else
{
throw new Exception("Locate Failed.");
}
}
}
}