ipLocnTest
1.0.0
在 .NET 中获取功能齐全的地图,无需任何实际的地图 API 参与!
Imports System.Device.Location
将以下代码放入主类中
DIm n = 0
Private Sub Watcher_StatusChanged(ByVal sender As Object, ByVal e As GeoPositionStatusChangedEventArgs)
n += 1
If e.Status = GeoPositionStatus.Ready Then
If Watcher.Position.Location.IsUnknown Then
txtlat = "Cannot find location data"
Else
txtlat = Watcher.Position.Location.Latitude.ToString() '.Substring(0, 7)
txtlong = Watcher.Position.Location.Longitude.ToString() '.Substring(0, 7)
End If
End If
If n < 10 Then
If txtlat <> "Cannot find location data" And Math.Abs(Val(txtlat)) > 0.0000001 Then
Watcher.Stop()
'the value of txtlat and txtlong has now been set to your location
End If
Else
Watcher.Stop()
'give user some indication of error/ handle the error (thrown when cannot find location)
End If
End Sub
Private Sub LocateMe()
n = 0
Watcher = New GeoCoordinateWatcher()
AddHandler Watcher.StatusChanged, AddressOf Watcher_StatusChanged
Watcher.Start()
End Sub
当您需要位置时,调用 LocateMe() 子函数。首次使用时,将弹出一个消息框,请求访问位置的权限。 txtlat 和 txtlong 的值将设置为纬度和经度。
请注意,这通常不是瞬时的,第一次尝试可能会失败