- [C#] Dependency Injection in Winform
- [SQLSERVER] Hướng dẫn tìm kiếm nâng cao trên sql
- [C#] Hướng dẫn sử dụng SetTimeOut trên Winform like Javascript
- [DATABASE] In cây thông noel bằng sqlserver
- [C#] Hướng dẫn fix lỗi hiển thị UTF-8 khi sử dụng WebClient Download String
- [DATABASE] Hướng dẫn mã hóa và giải mã sử dụng thuật toán AES 256 trên sqlserver
- [DATABASE] Base64 Encode and Decode trong Sqlserver
- [C#] Vì Mẹ anh bắt phải Fake địa chỉ MacAddress
- [C#] Hướng dẫn xuất dữ liệu từ DataGridview ra file Excel
- [C#] Hướng dẫn khởi động lại chương trình ứng dụng winform
- [C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace
- [C#] Hướng dẫn đọc file hình ảnh định dạng WEBP và chuyển đổi WebP sang JPG
- [C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính
- [C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
[VB.NET] Hướng dẫn add Marker trên google map sử dụng thư viện GMap
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách thêm marker lên trên bản đồ google map sử dụng thư viện GMap.
Trong lập trình ứng dụng, khi các bạn muốn thêm từng địa điểm của mình lên bản đồ trong lập trình vb.net.
Ví dụ: Một công ty có nhiều chi nhánh và mình muốn hiển thị tất cả các tọa độ của những chi nhánh đó lên trên bản đồ google map.
Dưới đây, là giao diện demo ứng dụng add marker trên google map:
Đầu tiên, các bạn nhớ cài đặt thư viện Gmap vào project nhé, các bạn có thể lấy thư viện ở source code của mình ở bên dưới
Source code Add marker on google map vb.net:
Imports GMap
Imports GMap.NET
Imports GMap.NET.MapProviders
Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Public Class Form1
Dim marker As GMarkerGoogle
Dim markerOverlay As GMapOverlay
Dim dt As DataTable
Dim selection As Integer = 0
Dim Lat As Double = 10.826479
Dim Lng As Double = 106.722285
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt = GetTable()
For Each dr As DataRow In dt.Rows
'Tạo marker position map
markerOverlay = New GMapOverlay(dr.Item("Name"))
marker = New GMarkerGoogle(New PointLatLng(dr.Item("Lat"), dr.Item("Lng")), GMarkerGoogleType.red_dot)
markerOverlay.Markers.Add(marker)
marker.ToolTipMode = MarkerTooltipMode.Always
marker.ToolTipText = String.Format("{0}" + vbNewLine + " Kinh độ: {1} " + vbNewLine + " Vĩ độ: {2}", dr.Item("Name"), dr.Item("Lat"), dr.Item("Lng"))
GMapControl1.Overlays.Add(markerOverlay)
Next
GMapControl1.DragButton = MouseButtons.Left
GMapControl1.CanDragMap = True
GMapControl1.MapProvider = GMapProviders.GoogleMap
GMapControl1.Position = New PointLatLng(Lat, Lng)
GMapControl1.MinZoom = 0
GMapControl1.MaxZoom = 24
GMapControl1.Zoom = 12
GMapControl1.AutoScroll = True
CreateCircle(Lat, Lng, 10000)
End Sub
Function GetTable() As DataTable
Dim table As New DataTable
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Lat", GetType(Double))
table.Columns.Add("Lng", GetType(Double))
table.Rows.Add("Đại học Văn Lang", 10.812598, 106.694913)
table.Rows.Add("Đại học Kinh Tế", 10.78351, 106.694706)
table.Rows.Add("Đại học Bách Khoa", 10.77268, 106.658814)
table.Rows.Add("Đại học Công Nghiệp", 10.831681, 106.68731)
table.Rows.Add("Đại học Sư phạm kỹ thuật", 10.851418, 106.77142)
Return table
End Function
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
ToolTip1.SetToolTip(TrackBar1, TrackBar1.Value.ToString())
GMapControl1.Zoom = TrackBar1.Value
End Sub
Private Sub GMapControl1_OnMapZoomChanged() Handles GMapControl1.OnMapZoomChanged
TrackBar1.Value = GMapControl1.Zoom
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GMapControl1.Position = New PointLatLng(Lat, Lng)
GMapControl1.Zoom = 12
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Application.Exit()
End Sub
'//////////////////////// Draw circle ///////////////////////////
Private Sub CreateCircle(ByVal lat As Double, ByVal lon As Double, ByVal radius As Double)
Dim point As PointLatLng = New PointLatLng(lat, lon)
Dim segments As Integer = 1000
Dim gpollist As List(Of PointLatLng) = New List(Of PointLatLng)()
For i As Integer = 0 To segments - 1
gpollist.Add(FindPointAtDistanceFrom(point, i, radius / 1000))
Next
Dim gpol As GMapPolygon = New GMapPolygon(gpollist, "Circle")
Dim polyOverlay As GMapOverlay = New GMapOverlay("Circle")
gpol.Stroke.DashStyle = Drawing2D.DashStyle.Custom
gpol.Fill = New SolidBrush(Color.FromArgb(50, Color.Red)) ' Thay màu ở đây
gpol.Stroke = New Pen(Color.FromArgb(30, Color.Red), 1) ' Thay màu ở đây
GMapControl1.Overlays.Add(polyOverlay)
polyOverlay.Polygons.Add(gpol)
End Sub
Public Shared Function FindPointAtDistanceFrom(ByVal startPoint As GMap.NET.PointLatLng, ByVal initialBearingRadians As Double, ByVal distanceKilometres As Double) As GMap.NET.PointLatLng
Const radiusEarthKilometres As Double = 6371.01
Dim distRatio = distanceKilometres / radiusEarthKilometres
Dim distRatioSine = Math.Sin(distRatio)
Dim distRatioCosine = Math.Cos(distRatio)
Dim startLatRad = DegreesToRadians(startPoint.Lat)
Dim startLonRad = DegreesToRadians(startPoint.Lng)
Dim startLatCos = Math.Cos(startLatRad)
Dim startLatSin = Math.Sin(startLatRad)
Dim endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)))
Dim endLonRads = startLonRad + Math.Atan2(Math.Sin(initialBearingRadians) * distRatioSine * startLatCos, distRatioCosine - startLatSin * Math.Sin(endLatRads))
Return New GMap.NET.PointLatLng(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads))
End Function
Public Shared Function DegreesToRadians(ByVal degrees As Double) As Double
Const degToRadFactor As Double = Math.PI / 180
Return degrees * degToRadFactor
End Function
Public Shared Function RadiansToDegrees(ByVal radians As Double) As Double
Const radToDegFactor As Double = 180 / Math.PI
Return radians * radToDegFactor
End Function
End Class
HAPPY CODING