2010-04-06 119 views
3

我使用.NET和我能够通过IP来地理定位用户的城市.NET获取时区。按城市或经度和纬度

有没有简单的方法通过长期得到用户的时区。和拉特。或城市名称?

我想知道Facebook是怎么做的呢?

谢谢

+0

这可能帮助:http://stackoverflow.com/questions/41504/timezone-lookup-from-latitude-longitude – 2010-04-06 07:06:04

回答

0

有一个Web服务,您可以使用城市或经度和纬度来获取时区:

http://www.earthtools.org/webservices.htm#timezone

+1

这不是一个真正的全时区IMO。这是当前UTC的偏移量 - 这意味着你不知道DST什么时候会改变等。这是一个耻辱,它不能给出奥尔森的名字。 – 2010-04-06 07:13:11

+0

确实如此,但响应确实包括当地时间(考虑夏令时,如果有效),UTC时间,以及夏时制是否生效 - 可能足以满足多种用途。举个例子: http://www.earthtools.org/timezone-1.1/57.09/02.05 – Cocowalla 2010-04-06 07:23:00

+1

这并没有考虑到DST欧洲以外的,所以它基本上是毫无价值的。 – 2012-05-08 22:01:59

0

你可以从系统时区列表并将其与城市名称:

http://msdn.microsoft.com/en-us/library/bb397781.aspx

这是脆弱的。

另外也有少数网络服务在那里,你可以使用,通过它长/ lat和它给你的时区,但是这意味着你是拴在第三部分Web服务。

+0

如果你想运行自己的服务,而不是依赖于Web服务,你可以试用http://askgeo.com,它出售一个Java库,给出给定经纬度的时区ID。 – 2012-05-08 22:06:53

0

我敢肯定,这个代码可以简化,但这个工作对我来说,得到时区ID和名称。

Private Sub checkTZ() 
    Dim wc As WebClient = New WebClient() 
    Dim str As String = Nothing 
    Dim latPattern As String = "\bLatitude.*\<{1}" 
    Dim latRegSearch As Regex = New Regex(latPattern) 
    Dim lat As String = Nothing 
    Dim lonPattern As String = "\bLongitude.*\<{1}" 
    Dim lonRegSearch As Regex = New Regex(lonPattern) 
    Dim lon As String = Nothing 

    Try 
     str = wc.DownloadString("http://www.ipinfodb.com/my_ip_location.php") 
     lat = latRegSearch.Match(str).Value 
     lon = lonRegSearch.Match(str).Value 
    Catch ex As Exception 
     str = "Not Found" 
    End Try 

    Dim pattern As String = "\<|\s|\:|\bLatitude|\bLongitude" 
    Dim rgx As New Regex(pattern) 
    lat = rgx.Replace(lat, "") 
    lon = rgx.Replace(lon, "") 

    Dim firefoxEpochDate As Date = "1/1/1970" 
    Dim s_CurrentGoogleAPI As Long = DateDiff(DateInterval.Second, firefoxEpochDate, DateTime.UtcNow) 
    Dim xmldoc As XmlDocument = New XmlDocument() 
    Dim results2 As String = wc.DownloadString("https://maps.googleapis.com/maps/api/timezone/xml?location=" & lat & "," & lon & "&timestamp=" & s_CurrentGoogleAPI) 
    xmldoc.LoadXml(results2) 
    Dim tz_offset_hours As Double = (Convert.ToDouble(xmldoc.DocumentElement.Item("raw_offset").InnerText) + Convert.ToDouble(xmldoc.DocumentElement.Item("dst_offset").InnerText))/3600 
End Sub 
相关问题