Does anyone understand the gb.Map component?

Post

Posted
Rating:
#1 (In Topic #363)
Avatar
Regular
stevedee is in the usergroup ‘Regular’
I've spent a few hours this rainy afternoon taking my first look as this Map component.

I often struggle with Gambas documentation because I'm really thick, so I'm looking for advice. I started with a new project and just dragged a MapView onto the main form. The first bit of code is quite easy:-

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   MapView1.Map.AddTile("openstreet", "http://\{s}.tile.openstreetmap.org/\{z}/\{x}/\{y}.png").Copyright = "OpenStreetMap Contributors"
  4.  
  5.   showMethod \{1/2/3}
  6.  
  7.   MapView1.Map.Zoom = 15
  8.  

Then I added some code based upon an example I found from a French guy, which I've called showMethod1:-

Code (gambas)

  1. Public Sub showMethod1()
  2.  
  3.   MapView1.Map.AddShape("downtown")
  4.   MapView1.Map!downtown.AddPoint("church", MapPoint(51.059, -0.33))
  5.   MapView1.Map.Center = MapView1.Map!downtown!church.Points
  6.  

This code works, but I have 2 problems with it; I don't think I have ever seen the "!" used this way, and the autocomplete doesn't seem to recognise the "Points" method (…at least I assume its a method).

I then re-wrote the code as showMethod2, which also works:-

Code (gambas)

  1. Public Sub showMethod2()
  2.  
  3.   MapView1.Map.AddShape("downtown")
  4.   MapView1.Map["downtown"].AddPoint("church", MapPoint(51.059, -0.33))
  5.   MapView1.Map.Center = MapView1.Map["downtown"]["church"].Points
  6.  

Again, this code doesn't look much like anything in the user documentation. I also can't see any manual reference to:-

Code (gambas)

  1. MapPoint(51.059, -0.33)

…which seems to be the undocumented default Method to setup the Lat & Lon Properties. So for displayMethod3 I created a new instance of MapPoint and set these two properties individually.

Code (gambas)

  1. Public Sub showMethod3()
  2. Dim myPoint As New MapPoint
  3.  
  4.   myPoint.Lat = 51.059
  5.   myPoint.Lon = -0.33
  6.   MapView1.Map.AddShape("downtown")
  7.   MapView1.Map["downtown"].AddPoint("church", myPoint)
  8.   MapView1.Map.Center = MapView1.Map["downtown"]["church"].Points
  9.  

So your views & comments would be appreciated, even if it turns out I've missed something important in the documentation.


However, zooming the map and drawing polylines & circles is more straightforward:-

Code (gambas)

  1.     MapView1.Map.Zoom = 15
  2.  
  3. Public Sub SetArea()
  4.  Dim hPolyLine As New MapPoint[]
  5.  
  6.   With MapView1.Map.AddShape("Poly")
  7.     hPolyLine = [MapPoint(51.063, -0.326), MapPoint(51.058, -0.325), MapPoint(51.058, -0.333), MapPoint(51.063, -0.334), MapPoint(51.064, -0.330), MapPoint(51.063, -0.326)]
  8.     .AddPolyLine("manor", hPolyLine)
  9.   End With
  10.  
  11.  
  12. Public Sub DrawCircle()
  13.  Dim hCircle As New MapPoint
  14.  
  15.   With MapView1.Map.AddShape("ring")
  16.     hCircle = MapPoint(51.063, -0.326)
  17.     .AddCircle("hoop", hCircle, 50, Color.red, 1, 1, Color.Blue)
  18.   End With
  19.  

Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
SteveDee,
The exclamation point (! or 'bang') looks like a dictionary access operator that I used to see in VB/VBA.

https://social.msdn.microsoft.com/Forums/en-US/1393df69-929e-49c6-93b0-5cb0fa7a6430/what-is-a-operator
vb.net - What does this { ! } mean in the specific line of code? - Stack Overflow

I was never fond of it myself, and went out of my way to use the other syntax.
Steve S
PS – thanks for sharing, will be watching where you get with the map component…
_________________

stevedee said


Code (gambas)

  1. Public Sub showMethod1()
  2.  
  3.   MapView1.Map.AddShape("downtown")
  4.   MapView1.Map!downtown.AddPoint("church", MapPoint(51.059, -0.33))
  5.   MapView1.Map.Center = MapView1.Map!downtown!church.Points
  6.  

This code works, but I have 2 problems with it; I don't think I have ever seen the "!" used this way, and the autocomplete doesn't seem to recognise the "Points" method (…at least I assume its a method).

I then re-wrote the code as showMethod2, which also works:-

Code (gambas)

  1. Public Sub showMethod2()
  2.  
  3.   MapView1.Map.AddShape("downtown")
  4.   MapView1.Map["downtown"].AddPoint("church", MapPoint(51.059, -0.33))
  5.   MapView1.Map.Center = MapView1.Map["downtown"]["church"].Points
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’

sjsepan said

…The exclamation point (! or 'bang') looks like a dictionary access operator…

Thanks Steve, that's another clue.

I'm familiar with "!" being using to access fields in a database, like this extract from one of my old Gambas projects:-

Code (gambas)

  1.         'use most recent (latest) times in me-tv schedule
  2.         If (tvRecord!start_time + tvRecord!duration) > (lngStart + lngDur) Then
  3.           lngStart = tvRecord!start_time
  4.           lngDur = tvRecord!duration
  5.           strChID = tvRecord!channel_id

… but I don't think I've ever seen it used in Gambas beyond that. Maybe "!" can be used to access the elements in a collection or an array? I cant see anything about this in the documentation, so I'll have to experiment.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’

sjsepan said

The exclamation point (! or 'bang') looks like a dictionary access operator…

You are spot-on Steve.

Generally, there are 3 collection types in computer science; array, set & dictionary

In Gambas (& VB) a Collection is basically a Dictionary, and we can use the "!" as follows:-

Code (gambas)

  1.  
  2.   Cars["Ford"] = "black"
  3.   Cars["Skoda"] = "blue"
  4.   Cars["Jaguar"] = "red"
  5.  
  6.   Me.Text = "Skoda: " & Cars!"Skoda"

…or use this method:-

Code (gambas)

  1. Me.Text = "Skoda: " & Cars["Skoda"]
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Ok, I now have a much better idea of how to use the Map component. Here is a simplified example, which uses the MapView graphical component as a drawing area.

The first step is to select a map. I'm using a free "Hike & Bike" OpenStreetMap, and the terms & conditions require me to display a copyright message:-

   MapView1.Map.AddTile("openstreet", "https://tiles.wmflabs.org/hikebike/\{z}/{x}/{y}.png").Copyright = "© OpenStreetMap Contributors"

This one line of code is all you need to display a world map which can be zoomed using the display controls.

To plot data (such as a route recorded on a gps device) the data needs to be prepared as a series of MapPoints, where each point consists of a Latitude and a Longitude which must be added to a Gambas collection:-

Code (gambas)

  1. Dim myRoute As New MapPoint[]
  2.  
  3.         myRoute = [MapPoint(51.063, -0.326), MapPoint(51.058, -0.325), MapPoint(51.058, -0.333).....]
Or:-   

Code (gambas)

  1.         myRoute.Add(MapPoint(fLat1, fLon1))
  2.         myRoute.Add(MapPoint(fLat2, fLon2))

We need to add a MapShape layer & give it a simple string name (a key):-

Code (gambas)

  1.         MapView1.Map.AddShape("outdoors")

It would be nice to have a balloon shaped flag at the start of route. This also needs a key/name, so lets call it "starter":-

Code (gambas)

  1. Dim myPoint As New MapPoint
  2.  
  3.         myPoint.Lat = fLat
  4.         myPoint.Lon = fLon
  5.         MapView1.Map["outdoors"].AddPoint("starter", myPoint)

When the map is displayed, I'd like it centred on my starting point:-

Code (gambas)

  1.         MapView1.Map.Center = MapView1.Map["outdoors"]["starter"].Points

…which simply centres the map at "starter" on the "outdoor" MapShape layer.

Now we can plot the route data in myRoute as a series of linked red lines, giving it a key/name ("walkies"):-

Code (gambas)

  1.         MapView1.Map["outdoors"].AddPolyLine("walkies", myRoute, Color.Red)

One final touch is to zoom in to get a more "local" view:-

Code (gambas)

  1.         MapView1.Map.Zoom = 14



I will post an example project under Project Showcase for anyone interested.
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’
Hello
…from Wiki of italian Gambas Forum:

Guide della comunità - Gambas-it.org - Wikipedia

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
stevedee is in the usergroup ‘Regular’

vuott said

…from Wiki of italian Gambas Forum…

Many thanks vuott, that looks like a great resource.

This image shows an English translation of topics:-


Image

(Click to enlarge)

Online now: No Back to the top
1 guest and 0 members have just viewed this.