【问题标题】:Get country name and code based on lat/long c#根据纬度/经度c#获取国家名称和代码
【发布时间】:2016-06-24 05:08:16
【问题描述】:

目前我发现的最好的解决方案是,

using Two10.CountryLookup;

var lookup = new ReverseLookup();
try
{   // get country details based on lat/long
    var country = lookup.Lookup(float.Parse(scandata.gpslat), float.Parse(scandata.gpslong));
  string cName = country.name;
   string cCode = country.code;
}
catch(Exception e)
{
    Console.WriteLine("Exception :"+e);
} 

但是这个物体太重了,我有几百万条记录要处理。这个对象使我的脚本变慢。还有其他更快的方法吗?

【问题讨论】:

  • 什么是ReverseLookup?这是什么来的?
  • 您是否正在为要查找的每条记录创建一个新的 ReverseLookup 实例?
  • 使用 Google Geocoding API 怎么样,比如this
  • @ChrisS Hes 试图定位数百万条记录。进行网络查找会很慢。

标签: c# geolocation


【解决方案1】:

如果您阅读网站上的自述文件,它会告诉您创建 ReverseLookup 对象的成本很高。所以他们希望你创建一次然后重复使用它。

https://github.com/vansha/Two10.CountryLookup/blob/master/readme.md

它很昂贵,因为它在构造函数中加载和解析整个 7.1MB 区域列表。

我刚刚进行了一些测试。每次查询 20 个位置并创建 ReverseLookup 对象需要 10 秒。创建一次并重复使用 20 次需要 0.6 秒。重复使用 2000 次需要 2 秒。

【讨论】:

  • @D4Developer 看看 The Anathema 的解决方案。他创建了一个非常好的单例示例。
【解决方案2】:

如果您阅读库的源代码,它会加载一个区域列表。

this.Regions = ParseInput(LoadFile()).ToArray();

即使ParseInput()LoadFile() 已延迟执行它们,它也会立即将IEnumerable<Region> 转换为一个数组,该数组会执行它并强制对每个构造函数进行评估。这是一个昂贵的操作,所以应该设置一次。

您应该构造一次ReverseLookup 项目,或者在Singleton. 中实现它

public class RegionLocator
{
    private static RegionLocator instance;
    private static ReverseLookup ReverseLookup;

    private RegionLocator() { }

    public static RegionLocator Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new RegionLocator();
                ReverseLookup = new ReverseLookup();
            }
            return instance;
        }
    }

    public Region Lookup(float lat, float lng, RegionType[] types)
    {
        return ReverseLookup.Lookup(lat, lng, types);
    }

    public Region[] Regions()
    {
        return ReverseLookup.Regions;
    }
}

这样使用:

RegionLocator.Instance.Lookup(float.Parse(scandata.gpslat), float.Parse(scandata.gpslong));

【讨论】:

  • 完美..:) 你让我很开心.. 每次都初始化它很愚蠢:(.
  • @D4Developer,是的,使用 Singleton 处理 5000 组纬度/经度需要 1.3 秒,每次初始化后处理 50 组需要 23 秒。
猜你喜欢
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 2020-04-16
相关资源
最近更新 更多