2016-05-13 59 views
2

我正在使用一種方法從IP地址獲取IipLocation對象。這是返回的Location對象的代碼:GeoIpLocation - 如何獲得州和縣? (或者,如何從郵政編碼獲得州和縣)C#

public IIpLocation GetIpLocation(IPAddress ipAddress) 
{ 
    ipAddress.CheckNull("ipAddress"); 

    IIpLocation ipLocation = null; 
    try 
    { 
     //Try to look up the location using MaxMind. 
     Location location = GetMaxMindIpLocation(ipAddress); 

     //If the postal code is not present, then try to look it up with GeoNames. 
     if (location != null && (location.PostalCode == null || location.PostalCode.Equals(""))) 
     { 
      location.PostalCode = GetPostalCodeFromGeoNames(location); 
     } 

     if (location != null) 
     { 
      ipLocation = new GeoIpLocation(location); 
     } 
    } 
    catch (NullReferenceException) 
    { 
     Log.ErrorFormat(Resources.log_maxMindLookupFailed, ipAddress); 
    } 

    return ipLocation; 
} 

的IipLocation對象具有以下屬性:RegionRegionNameCountryCodeCityPostal CodeLatitudeLongitude。但是,我需要州和縣。我如何從我擁有的信息中獲得州和郡?

+0

我有點困惑。 'GetMaxMindIpLocation()'來自哪裏? [MaxMind的官方API](https://github.com/maxmind/GeoIP2-dotnet#city-database)包含您正在查找的所有數據。 –

回答

相關問題