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 的值將設定為緯度和經度。
請注意,這通常不是瞬時的,第一次嘗試可能會失敗