我正在使用.NET,通过IP可以获取用户所在城市。
是否有一种简单的方法通过经度和纬度或城市名称来获取用户时区?
我想知道Facebook是如何做到的?
谢谢
我正在使用.NET,通过IP可以获取用户所在城市。
是否有一种简单的方法通过经度和纬度或城市名称来获取用户时区?
我想知道Facebook是如何做到的?
谢谢
你可以从系统获取一个时区列表,并将其与城市名称进行比较:
http://msdn.microsoft.com/en-us/library/bb397781.aspx
哪一个是脆弱的。
或者,你可以使用一些网络服务,传递经纬度信息,它会给你返回时区,但这意味着你要依赖于第三方网络服务。
有一个 Web 服务可以通过城市或经纬度获取时区:
我相信这段代码可以被简化,但是它对我来说可以获得时区的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 & "×tamp=" & 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