Skip to content

Commit

Permalink
优化GeoPoint,本想使用readonly struct,但不知道如何区分无效坐标。
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 28, 2024
1 parent bb066b5 commit 9d91394
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 186 deletions.
2 changes: 1 addition & 1 deletion MapApi/Locations/地图提供者.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static MapProvider FindById(Int32 id)
/// <returns>实体列表</returns>
public static IList<MapProvider> FindAllByKind(NewLife.Map.Models.MapKinds kind)
{
if (kind <= 0) return new List<MapProvider>();
if (kind <= 0) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.Kind == kind);
Expand Down
8 changes: 4 additions & 4 deletions MapApi/Locations/地理数据6位.Biz.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NewLife;
using NewLife;
using NewLife.Data;
using XCode;
using XCode.Membership;
Expand Down Expand Up @@ -146,7 +146,7 @@ public static IList<Geo6> FindAllByCode(Int32 code)
/// <returns>实体列表</returns>
public static IList<Geo6> FindAllByHash(String hash)
{
if (hash.IsNullOrEmpty()) return new List<Geo6>();
if (hash.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.Hash.EqualIgnoreCase(hash));
Expand All @@ -159,7 +159,7 @@ public static IList<Geo6> FindAllByHash(String hash)
/// <returns>实体列表</returns>
public static IList<Geo6> FindAllByHashBd09(String hashBd09)
{
if (hashBd09.IsNullOrEmpty()) return new List<Geo6>();
if (hashBd09.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashBd09.EqualIgnoreCase(hashBd09));
Expand All @@ -172,7 +172,7 @@ public static IList<Geo6> FindAllByHashBd09(String hashBd09)
/// <returns>实体列表</returns>
public static IList<Geo6> FindAllByHashGcj02(String hashGcj02)
{
if (hashGcj02.IsNullOrEmpty()) return new List<Geo6>();
if (hashGcj02.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashGcj02.EqualIgnoreCase(hashGcj02));
Expand Down
6 changes: 3 additions & 3 deletions MapApi/Locations/地理数据7位.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static IList<Geo7> FindAllByCode(Int32 code)
/// <returns>实体列表</returns>
public static IList<Geo7> FindAllByHash(String hash)
{
if (hash.IsNullOrEmpty()) return new List<Geo7>();
if (hash.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.Hash.EqualIgnoreCase(hash));
Expand All @@ -156,7 +156,7 @@ public static IList<Geo7> FindAllByHash(String hash)
/// <returns>实体列表</returns>
public static IList<Geo7> FindAllByHashBd09(String hashBd09)
{
if (hashBd09.IsNullOrEmpty()) return new List<Geo7>();
if (hashBd09.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashBd09.EqualIgnoreCase(hashBd09));
Expand All @@ -169,7 +169,7 @@ public static IList<Geo7> FindAllByHashBd09(String hashBd09)
/// <returns>实体列表</returns>
public static IList<Geo7> FindAllByHashGcj02(String hashGcj02)
{
if (hashGcj02.IsNullOrEmpty()) return new List<Geo7>();
if (hashGcj02.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashGcj02.EqualIgnoreCase(hashGcj02));
Expand Down
6 changes: 3 additions & 3 deletions MapApi/Locations/地理数据8位.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static IList<Geo8> FindAllByCode(Int32 code)
/// <returns>实体列表</returns>
public static IList<Geo8> FindAllByHash(String hash)
{
if (hash.IsNullOrEmpty()) return new List<Geo8>();
if (hash.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.Hash.EqualIgnoreCase(hash));
Expand All @@ -156,7 +156,7 @@ public static IList<Geo8> FindAllByHash(String hash)
/// <returns>实体列表</returns>
public static IList<Geo8> FindAllByHashBd09(String hashBd09)
{
if (hashBd09.IsNullOrEmpty()) return new List<Geo8>();
if (hashBd09.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashBd09.EqualIgnoreCase(hashBd09));
Expand All @@ -169,7 +169,7 @@ public static IList<Geo8> FindAllByHashBd09(String hashBd09)
/// <returns>实体列表</returns>
public static IList<Geo8> FindAllByHashGcj02(String hashGcj02)
{
if (hashGcj02.IsNullOrEmpty()) return new List<Geo8>();
if (hashGcj02.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashGcj02.EqualIgnoreCase(hashGcj02));
Expand Down
6 changes: 3 additions & 3 deletions MapApi/Locations/地理数据9位.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static IList<Geo9> FindAllByCode(Int32 code)
/// <returns>实体列表</returns>
public static IList<Geo9> FindAllByHash(String hash)
{
if (hash.IsNullOrEmpty()) return new List<Geo9>();
if (hash.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.Hash.EqualIgnoreCase(hash));
Expand All @@ -156,7 +156,7 @@ public static IList<Geo9> FindAllByHash(String hash)
/// <returns>实体列表</returns>
public static IList<Geo9> FindAllByHashBd09(String hashBd09)
{
if (hashBd09.IsNullOrEmpty()) return new List<Geo9>();
if (hashBd09.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashBd09.EqualIgnoreCase(hashBd09));
Expand All @@ -169,7 +169,7 @@ public static IList<Geo9> FindAllByHashBd09(String hashBd09)
/// <returns>实体列表</returns>
public static IList<Geo9> FindAllByHashGcj02(String hashGcj02)
{
if (hashGcj02.IsNullOrEmpty()) return new List<Geo9>();
if (hashGcj02.IsNullOrEmpty()) return [];

// 实体缓存
if (Meta.Session.Count < 1000) return Meta.Cache.FindAll(e => e.HashGcj02.EqualIgnoreCase(hashGcj02));
Expand Down
35 changes: 14 additions & 21 deletions NewLife.Map/AMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public AMap()
/// <param name="url">目标Url</param>
/// <param name="result">结果字段</param>
/// <returns></returns>
protected override async Task<T> InvokeAsync<T>(String url, String result)
protected override async Task<T> InvokeAsync<T>(String url, String? result)

Check warning on line 46 in NewLife.Map/AMap.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type doesn't match overridden member.

Check warning on line 46 in NewLife.Map/AMap.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type doesn't match overridden member.

Check warning on line 46 in NewLife.Map/AMap.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type doesn't match overridden member.
{
var dic = await base.InvokeAsync<IDictionary<String, Object>>(url, result);
if (dic == null || dic.Count == 0) return default;
Expand All @@ -54,7 +54,7 @@ protected override async Task<T> InvokeAsync<T>(String url, String result)
var msg = dic["info"] + "";

// 删除无效密钥
if (IsValidKey(msg)) RemoveKey(LastKey, DateTime.Now.AddHours(1));
if (!LastKey.IsNullOrEmpty() && IsValidKey(msg)) RemoveKey(LastKey, DateTime.Now.AddHours(1));

return !ThrowException ? default : throw new Exception(msg);
}
Expand All @@ -71,7 +71,7 @@ protected override async Task<T> InvokeAsync<T>(String url, String result)
/// <param name="city"></param>
/// <param name="coordtype"></param>
/// <returns></returns>
protected async Task<IDictionary<String, Object>> GetGeocoderAsync(String address, String city = null, String coordtype = null)
protected async Task<IDictionary<String, Object>?> GetGeocoderAsync(String address, String? city = null, String? coordtype = null)
{
if (address.IsNullOrEmpty()) throw new ArgumentNullException(nameof(address));

Expand All @@ -91,21 +91,16 @@ protected async Task<IDictionary<String, Object>> GetGeocoderAsync(String addres
/// <param name="coordtype"></param>
/// <param name="formatAddress">是否格式化地址。高德地图默认已经格式化地址</param>
/// <returns></returns>
public async Task<GeoAddress> GetGeoAsync(String address, String city = null, String coordtype = null, Boolean formatAddress = false)
public async Task<GeoAddress?> GetGeoAsync(String address, String? city = null, String? coordtype = null, Boolean formatAddress = false)
{
var rs = await GetGeocoderAsync(address, city);
if (rs == null || rs.Count == 0) return null;

var gp = new GeoPoint();

var ds = (rs["location"] + "").Split(',');
if (ds != null && ds.Length >= 2)
var geo = new GeoAddress
{
gp.Longitude = ds[0].ToDouble();
gp.Latitude = ds[1].ToDouble();
}
Location = new(rs["location"] as String)
};

var geo = new GeoAddress();
var reader = new JsonReader();
reader.ToObject(rs, null, geo);

Expand All @@ -114,11 +109,9 @@ public async Task<GeoAddress> GetGeoAsync(String address, String city = null, St
if (rs["township"] is IList<Object> ts && ts.Count > 0) geo.Township = ts[0] + "";
if (rs["number"] is IList<Object> ns && ns.Count > 0) geo.StreetNumber = ns[0] + "";

geo.Location = gp;

if (formatAddress)
{
var geo2 = await GetReverseGeoAsync(gp, "wcj02");
var geo2 = await GetReverseGeoAsync(geo.Location, "wcj02");
if (geo2 != null)
{
geo = geo2;
Expand Down Expand Up @@ -153,7 +146,7 @@ static void TrimAddress(GeoAddress geo)
/// <param name="point"></param>
/// <param name="coordtype"></param>
/// <returns></returns>
protected async Task<IDictionary<String, Object>> GetReverseGeocoderAsync(GeoPoint point, String coordtype)
protected async Task<IDictionary<String, Object>> GetReverseGeocoderAsync(GeoPoint point, String? coordtype)
{
if (point.Longitude < 0.1 || point.Latitude < 0.1) throw new ArgumentNullException(nameof(point));

Expand All @@ -166,7 +159,7 @@ protected async Task<IDictionary<String, Object>> GetReverseGeocoderAsync(GeoPoi
/// <param name="point"></param>
/// <param name="coordtype">坐标系</param>
/// <returns></returns>
public async Task<GeoAddress> GetReverseGeoAsync(GeoPoint point, String coordtype)
public async Task<GeoAddress?> GetReverseGeoAsync(GeoPoint point, String? coordtype)
{
var rs = await GetReverseGeocoderAsync(point, coordtype);
if (rs == null || rs.Count == 0) return null;
Expand Down Expand Up @@ -243,7 +236,7 @@ public async Task<GeoAddress> GetReverseGeoAsync(GeoPoint point, String coordtyp
/// <param name="coordtype"></param>
/// <param name="type">路径计算的方式和方法</param>
/// <returns></returns>
public async Task<Driving> GetDistanceAsync(GeoPoint origin, GeoPoint destination, String coordtype, Int32 type = 1)
public async Task<Driving?> GetDistanceAsync(GeoPoint origin, GeoPoint destination, String? coordtype, Int32 type = 1)
{
if (origin == null || origin.Longitude < 1 && origin.Latitude < 1) throw new ArgumentNullException(nameof(origin));
if (destination == null || destination.Longitude < 1 && destination.Latitude < 1) throw new ArgumentNullException(nameof(destination));
Expand Down Expand Up @@ -285,9 +278,9 @@ public async Task<IList<GeoArea>> GetAreaAsync(String keywords, Int32 subdistric
var url = $"http://restapi.amap.com/v3/config/district?keywords={keywords}&subdistrict={subdistrict}&filter={code}&extensions=base&output=json";

var list = await InvokeAsync<IList<Object>>(url, "districts");
if (list == null || list.Count == 0) return null;
if (list == null || list.Count == 0) return [];

if (list.FirstOrDefault() is not IDictionary<String, Object> geo) return null;
if (list.FirstOrDefault() is not IDictionary<String, Object> geo) return [];

var addrs = GetArea(geo, 0);

Expand All @@ -296,7 +289,7 @@ public async Task<IList<GeoArea>> GetAreaAsync(String keywords, Int32 subdistric

private IList<GeoArea> GetArea(IDictionary<String, Object> geo, Int32 parentCode)
{
if (geo == null || geo.Count == 0) return null;
if (geo == null || geo.Count == 0) return [];

var addrs = new List<GeoArea>();

Expand Down
Loading

0 comments on commit 9d91394

Please sign in to comment.