.NET 按城市或经纬度获取时区

发布于 2024-08-27 19:06:00 字数 120 浏览 1 评论 0原文

我正在使用 .NET,并且能够通过 IP 对用户城市进行地理定位。

有没有简单的方法可以长时间获取用户时区。和纬度。或城市名称?

我想知道 Facebook 是如何做到的?

谢谢

I'm using .NET and I'm able to geolocate user city by IP.

Is there easy way to get user timezone by long. and lat. or city name?

I wonder how facebook is doing it?

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-09-03 19:06:00

您可以使用一个网络服务来按城市或经度和纬度获取时区:

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

There is a web service you can use to get the timezone by city or longitude and latitude:

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

居里长安 2024-09-03 19:06:00

您可以从系统获取时区列表并将其与城市名称进行比较:

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

这是脆弱的。

或者,您可以使用一些网络服务,传递长/纬度,它会为您提供时区,但这意味着您受到第三方网络服务的束缚。

You can get a list of timezones from the system and compare it to the city name:

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

Which is fragile.

Alternatively there are a few web services out there you can use, passing it long/lat and it gives you timezone, but that means you're tethered to the third part web service.

客…行舟 2024-09-03 19:06:00

我确信这段代码可以简化,但这对我获取时区 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

I'm sure this code can be simplified, but this worked for me to get the TimeZone ID and Name.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文