2010-04-06 122 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 
相關問題