Differenze tra le versioni di "Utilizzare una DrawingArea con gb.map anziché una MapView"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
Mostreremo di seguito un semplice codice esemplificativo, nel quale si farà uso di una ''DrawingArea'' anziché dell'oggetto ''MapView''.
+
Mostreremo di seguito un semplice codice esemplificativo, nel quale si farà uso di una ''DrawingArea'' <B><SUP>&#091;[[#Note|Nota 1]]&#093;</sup></b> anziché dell'oggetto ''MapView''.
  
Bisognerà ovviamente attivare il Componente ''gb.map'' . <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
+
Bisognerà ovviamente attivare il Componente ''gb.map'' . <SUP>&#091;[[#Note|Nota 2]]&#093;</sup>
 
  Private DrawingArea1 As DrawingArea
 
  Private DrawingArea1 As DrawingArea
 
  Private hMap As New Map     
 
  Private hMap As New Map     
Riga 54: Riga 54:
 
    
 
    
 
   Dim mx, my As Short
 
   Dim mx, my As Short
 +
<FONT Color=gray>' ''Si impostano le seguenti costanti, per consentire uno spostamento morbido della mappa per ciascun livello di zoom:''</font>
 
   Dim s As Single[] = [0.1, 0.1, 0.028, 0.04, 0.064, 0.106, 0.182, 0.32, 0.56, 1.05, 1.9, 3.4, 6.3, 11.6, 21.6, 40.5, 76, 144]
 
   Dim s As Single[] = [0.1, 0.1, 0.028, 0.04, 0.064, 0.106, 0.182, 0.32, 0.56, 1.05, 1.9, 3.4, 6.3, 11.6, 21.6, 40.5, 76, 144]
 
    
 
    
Riga 80: Riga 81:
 
   x = pt.X   
 
   x = pt.X   
 
   y = pt.Y
 
   y = pt.Y
 +
 
 +
<FONT Color=gray>' ''Se si clicca sulla mappa con il tasto centrale, viene creato un file immagine della mappa corrente mostrata:''</font> ' <SUP>&#091;[[#Note|Nota 3]]&#093;</sup>
 +
  If Mouse.Middle Then
 +
    Dim im As New Image(mappa.Width, mappa.Height, Color.Transparent, Image.Standard)
 +
    With Paint
 +
      .Begin(im)
 +
      mappa.Draw()
 +
      .End
 +
    End With
 +
    im.Save("/tmp/immagine.png", 100)
 +
  Endif
 
    
 
    
 
  '''End'''
 
  '''End'''
Riga 105: Riga 117:
 
----
 
----
 
=Note=
 
=Note=
[1] Qualora la mappa non appaia, verificare la versione GoogleMap, ed eventualmente modificarla in questo punto: ["version": "900"]
+
[1] E' possibile utilizzare anche altre superfici, come ad esempio una ''ImageView'':
 +
Private mappa As New Map
 +
Private mp As New MapPoint(41.9, 12.5)
 +
Private pt As New Point
 +
Private x As Short
 +
Private y As Short
 +
 +
Public Sub _New()
 +
 
 +
  With mappa
 +
    .AddTile("Opentopomap", "https&#058;//khms{s}.google.it/kh/v={version}&src=app&x={x}&y={y}&z={z}&s=Galile", ["version": "900"]).SubDomains = ["0", "1", "2"]
 +
    .Center = mp
 +
    .Zoom = 15
 +
  End With
 +
  With Me
 +
    .W = Screen.AvailableWidth
 +
    .H = Screen.AvailableHeight
 +
    .Arrangement = Arrange.Fill
 +
    .Text = "Zoom: 15"
 +
  End With
 +
 
 +
End
 +
 +
Public Sub ImageView1_Draw(View As Image)
 +
 
 +
  With mappa
 +
    .Width = Paint.W
 +
    .Height = Paint.H
 +
    .Center = mp
 +
    .Draw()
 +
  End With
 +
 
 +
  ImageView1.Refresh()
 +
 
 +
End
 +
 +
Public Sub ImageView1_MouseWheel()
 +
 
 +
  pt = Point(Mouse.X, Mouse.Y)
 +
  mp.Lat = mappa.PixelToMapPointRel(pt).Lat
 +
  mp.Lon = mappa.PixelToMapPointRel(pt).Lon
 +
 +
  Select Case Mouse.Delta
 +
    Case 1
 +
      mappa.Zoom += 1
 +
    Case -1
 +
      mappa.Zoom -= 1
 +
  End Select
 +
 
 +
  Me.Text = "Zoom: " & CStr(mappa.Zoom)
 +
 
 +
End
 +
 +
Public Sub ImageView1_MouseMove()
 +
 
 +
  Dim mx, my As Short
 +
  Dim s As Single[] = [0.1, 0.1, 0.028, 0.04, 0.064, 0.106, 0.182, 0.32, 0.56, 1.05, 1.9, 3.4, 6.3, 11.6, 21.6, 40.5, 76, 144]
 +
 
 +
  mx = (x - Mouse.X)
 +
  my = (y - Mouse.Y)
 +
 
 +
  mappa.Center.Lat -= ((my ^ 0) * my) / (98 * mappa.Zoom * s[mappa.Zoom - 1])
 +
  mappa.Center.Lon += ((mx ^ 0) * mx) / (72 * mappa.Zoom * s[mappa.Zoom - 1])
 +
 
 +
  x = Mouse.X 
 +
  y = Mouse.Y
 +
 
 +
End
 +
 +
Public Sub ImageView1_MouseDown() 
 +
 +
  pt = Point(Mouse.X, Mouse.Y)
 +
 +
  Me.Text = "Lat. " & Format(mappa.PixelToMapPointRel(pt).Lat, "0.000000") &
 +
            "  Lon. " & Format(mappa.PixelToMapPointRel(pt).Lon, "0.000000") &
 +
            "  -  Zoom: " & CStr(mappa.Zoom)
 +
 +
  x = pt.X 
 +
  y = pt.Y
 +
 
 +
<FONT Color=gray>' ''Se si clicca sulla mappa con il tasto centrale, viene creato un file immagine della mappa corrente mostrata:''</font>
 +
  If Mouse.Middle Then
 +
    Dim im As New Image(mappa.Width, mappa.Height, Color.Transparent, Image.Standard)
 +
    With Paint
 +
      .Begin(im)
 +
      mappa.Draw()
 +
      .End
 +
    End With
 +
    im.Save("/tmp/immagine.png", 100)
 +
  Endif
 +
 +
End
 +
 
 +
 
 +
[2] Qualora la mappa non appaia, verificare la versione GoogleMap, ed eventualmente modificarla in questo punto: ["version": "900"]
 
<BR>Al riguardo vedere: [[Conoscere_la_versione_più_recente_di_Google_Maps_satellite|Conoscere la versione più recente di Google Maps satellite]]
 
<BR>Al riguardo vedere: [[Conoscere_la_versione_più_recente_di_Google_Maps_satellite|Conoscere la versione più recente di Google Maps satellite]]
 +
 +
[3] Vedere anche: [[Ottenere_un%27Image_dalla_mappa_mostrata_da_una_MapView|Ottenere un'Image dalla mappa mostrata da una MapView]]

Versione delle 07:46, 25 apr 2021

Mostreremo di seguito un semplice codice esemplificativo, nel quale si farà uso di una DrawingArea [Nota 1] anziché dell'oggetto MapView.

Bisognerà ovviamente attivare il Componente gb.map . [Nota 2]

Private DrawingArea1 As DrawingArea
Private hMap As New Map    
Private x As Integer
Private y As Integer
Private mp As New MapPoint(35.864483, 12.867534)
Private pt As New Point
 
 
Public Sub _New()    
 
 With hMap
   .AddTile("GoogleMaps", "https://khms{s}.google.it/kh/v={version}&src=app&x={x}&y={y}&z={z}&s=Galile", ["version": "900"]).SubDomains = ["0", "1", "2"]
   .Center = mp
 End With
 With Me  
   .W = Desktop.W
   .H = Desktop.H
   .Arrangement = Arrange.Fill
 End With
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .X = 0
   .Y = 0
 End With
 
End
 
 
Public Sub Form_Open()
 
 hMap.Zoom = 6  
 Me.Text = "Zoom: " & CStr(6)
  
End
 
 
Public Sub DrawingArea1_Draw()
 
 With hMap
   .Width = Paint.W
   .Height = Paint.H
   .Center = mp
   .Draw()
 End With
 
 DrawingArea1.Refresh()
 
End
 
 
Public Sub DrawingArea1_MouseMove()  
 
 Dim mx, my As Short
' Si impostano le seguenti costanti, per consentire uno spostamento morbido della mappa per ciascun livello di zoom:
 Dim s As Single[] = [0.1, 0.1, 0.028, 0.04, 0.064, 0.106, 0.182, 0.32, 0.56, 1.05, 1.9, 3.4, 6.3, 11.6, 21.6, 40.5, 76, 144]
 
 mx = (x - Mouse.X)
 my = (y - Mouse.Y)
 
 With hMap
   .Center.Lat -= ((my ^ 0) * my) / (98 * hMap.Zoom * s[hMap.Zoom - 1])
   .Center.Lon += ((mx ^ 0) * mx) / (72 * hMap.Zoom * s[hMap.Zoom - 1])
 End With
 
 x = Mouse.X  
 y = Mouse.Y  
 
End
 
 
Public Sub DrawingArea1_MouseDown()  
 
 pt = Point(Mouse.X, Mouse.Y)
 
 Me.Text = "Lat. " & Format(hMap.PixelToMapPointRel(pt).Lat, "0.000000") & 
           "   Lon. " & Format(hMap.PixelToMapPointRel(pt).Lon, "0.000000") &
           "  -  Zoom: " & CStr(hMap.Zoom)
 
 x = pt.X  
 y = pt.Y
 
' Se si clicca sulla mappa con il tasto centrale, viene creato un file immagine della mappa corrente mostrata: ' [Nota 3]
 If Mouse.Middle Then
   Dim im As New Image(mappa.Width, mappa.Height, Color.Transparent, Image.Standard)
   With Paint
     .Begin(im)
     mappa.Draw()
     .End
   End With
   im.Save("/tmp/immagine.png", 100)
 Endif
 
End
 

Public Sub DrawingArea1_MouseWheel()
 
' Pone al centro della mappa il punto ove si è ruotata la rotellina: 
 pt = Point(Mouse.X, Mouse.Y)
 mp.Lat = hMap.PixelToMapPointRel(pt).Lat
 mp.Lon = hMap.PixelToMapPointRel(pt).Lon
 
' Valuta il verso della rotazione della rotellina: 
 If Mouse.Delta > 0 Then 
   hMap.Zoom += 1
 Else 
   hMap.Zoom -= 1
 Endif
 Me.Text = "Zoom: " & CStr(hMap.Zoom)
 
End



Note

[1] E' possibile utilizzare anche altre superfici, come ad esempio una ImageView:

Private mappa As New Map
Private mp As New MapPoint(41.9, 12.5)
Private pt As New Point
Private x As Short
Private y As Short

Public Sub _New()
 
 With mappa
   .AddTile("Opentopomap", "https://khms{s}.google.it/kh/v={version}&src=app&x={x}&y={y}&z={z}&s=Galile", ["version": "900"]).SubDomains = ["0", "1", "2"]
   .Center = mp
   .Zoom = 15
 End With
 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
   .Text = "Zoom: 15"
 End With
 
End

Public Sub ImageView1_Draw(View As Image)
 
 With mappa
   .Width = Paint.W
   .Height = Paint.H
   .Center = mp
   .Draw()
 End With
 
 ImageView1.Refresh()
  
End

Public Sub ImageView1_MouseWheel()
 
 pt = Point(Mouse.X, Mouse.Y)
 mp.Lat = mappa.PixelToMapPointRel(pt).Lat
 mp.Lon = mappa.PixelToMapPointRel(pt).Lon

 Select Case Mouse.Delta
   Case 1
     mappa.Zoom += 1
   Case -1
     mappa.Zoom -= 1
 End Select
 
 Me.Text = "Zoom: " & CStr(mappa.Zoom)
 
End

Public Sub ImageView1_MouseMove()
 
 Dim mx, my As Short
 Dim s As Single[] = [0.1, 0.1, 0.028, 0.04, 0.064, 0.106, 0.182, 0.32, 0.56, 1.05, 1.9, 3.4, 6.3, 11.6, 21.6, 40.5, 76, 144]
 
 mx = (x - Mouse.X)
 my = (y - Mouse.Y)
 
 mappa.Center.Lat -= ((my ^ 0) * my) / (98 * mappa.Zoom * s[mappa.Zoom - 1])
 mappa.Center.Lon += ((mx ^ 0) * mx) / (72 * mappa.Zoom * s[mappa.Zoom - 1])
 
 x = Mouse.X  
 y = Mouse.Y
 
End

Public Sub ImageView1_MouseDown()  

 pt = Point(Mouse.X, Mouse.Y)

 Me.Text = "Lat. " & Format(mappa.PixelToMapPointRel(pt).Lat, "0.000000") & 
           "   Lon. " & Format(mappa.PixelToMapPointRel(pt).Lon, "0.000000") &
           "  -  Zoom: " & CStr(mappa.Zoom)

 x = pt.X  
 y = pt.Y
 
' Se si clicca sulla mappa con il tasto centrale, viene creato un file immagine della mappa corrente mostrata:
 If Mouse.Middle Then
   Dim im As New Image(mappa.Width, mappa.Height, Color.Transparent, Image.Standard)
   With Paint
     .Begin(im)
     mappa.Draw()
     .End
   End With
   im.Save("/tmp/immagine.png", 100)
 Endif

End


[2] Qualora la mappa non appaia, verificare la versione GoogleMap, ed eventualmente modificarla in questo punto: ["version": "900"]
Al riguardo vedere: Conoscere la versione più recente di Google Maps satellite

[3] Vedere anche: Ottenere un'Image dalla mappa mostrata da una MapView