Page 1 of 1

Does anyone understand the gb.Map component?

Posted: Tuesday 14th January 2020 8:02pm
by stevedee
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:-
Public Sub Form_Open()

  MapView1.Map.AddTile("openstreet", "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").Copyright = "OpenStreetMap Contributors"
  
  showMethod {1/2/3}
  
  MapView1.Map.Zoom = 15
 
End
Then I added some code based upon an example I found from a French guy, which I've called showMethod1:-
Public Sub showMethod1()

  MapView1.Map.AddShape("downtown")
  MapView1.Map!downtown.AddPoint("church", MapPoint(51.059, -0.33))
  MapView1.Map.Center = MapView1.Map!downtown!church.Points
  
End
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:-
Public Sub showMethod2()

  MapView1.Map.AddShape("downtown")
  MapView1.Map["downtown"].AddPoint("church", MapPoint(51.059, -0.33))
  MapView1.Map.Center = MapView1.Map["downtown"]["church"].Points
  
End
Again, this code doesn't look much like anything in the user documentation. I also can't see any manual reference to:-
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.
Public Sub showMethod3()
Dim myPoint As New MapPoint

  myPoint.Lat = 51.059
  myPoint.Lon = -0.33
  MapView1.Map.AddShape("downtown")
  MapView1.Map["downtown"].AddPoint("church", myPoint)
  MapView1.Map.Center = MapView1.Map["downtown"]["church"].Points
  
End
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:-
    MapView1.Map.Zoom = 15

Public Sub SetArea()
 Dim hPolyLine As New MapPoint[] 
  
  With MapView1.Map.AddShape("Poly")
    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)]
    .AddPolyLine("manor", hPolyLine)
  End With 

End

Public Sub DrawCircle()
 Dim hCircle As New MapPoint
  
  With MapView1.Map.AddShape("ring")
    hCircle = MapPoint(51.063, -0.326)
    .AddCircle("hoop", hCircle, 50, Color.red, 1, 1, Color.Blue)
  End With 
  
End

Re: Does anyone understand the gb.Map component?

Posted: Tuesday 14th January 2020 8:28pm
by sjsepan
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/Forum ... a-operator
https://stackoverflow.com/questions/684 ... ne-of-code

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 wrote: Tuesday 14th January 2020 8:02pm
Public Sub showMethod1()

  MapView1.Map.AddShape("downtown")
  MapView1.Map!downtown.AddPoint("church", MapPoint(51.059, -0.33))
  MapView1.Map.Center = MapView1.Map!downtown!church.Points
  
End
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:-
Public Sub showMethod2()

  MapView1.Map.AddShape("downtown")
  MapView1.Map["downtown"].AddPoint("church", MapPoint(51.059, -0.33))
  MapView1.Map.Center = MapView1.Map["downtown"]["church"].Points
  
End

Re: Does anyone understand the gb.Map component?

Posted: Wednesday 15th January 2020 9:55am
by stevedee
sjsepan wrote: Tuesday 14th January 2020 8:28pm ...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:-
        'use most recent (latest) times in me-tv schedule
        If (tvRecord!start_time + tvRecord!duration) > (lngStart + lngDur) Then
          lngStart = tvRecord!start_time
          lngDur = tvRecord!duration
          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.

Re: Does anyone understand the gb.Map component?

Posted: Thursday 16th January 2020 9:53am
by stevedee
sjsepan wrote: Tuesday 14th January 2020 8:28pm 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:-
Dim Cars As New Collection

  Cars["Ford"] = "black"
  Cars["Skoda"] = "blue"
  Cars["Jaguar"] = "red"
  
  Me.Text = "Skoda: " & Cars!"Skoda"
...or use this method:-
Me.Text = "Skoda: " & Cars["Skoda"]

Re: Does anyone understand the gb.Map component?

Posted: Thursday 16th January 2020 9:56pm
by stevedee
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:-
Dim myRoute As New MapPoint[]

	myRoute = [MapPoint(51.063, -0.326), MapPoint(51.058, -0.325), MapPoint(51.058, -0.333).....]
Or:-
	myRoute.Add(MapPoint(fLat1, fLon1))
	myRoute.Add(MapPoint(fLat2, fLon2))
We need to add a MapShape layer & give it a simple string name (a key):-
	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":-
Dim myPoint As New MapPoint

	myPoint.Lat = fLat
	myPoint.Lon = fLon
	MapView1.Map["outdoors"].AddPoint("starter", myPoint)
When the map is displayed, I'd like it centred on my starting point:-
	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"):-
	MapView1.Map["outdoors"].AddPolyLine("walkies", myRoute, Color.Red)
One final touch is to zoom in to get a more "local" view:-
	MapView1.Map.Zoom = 14


I will post an example project under Project Showcase for anyone interested.

Re: Does anyone understand the gb.Map component?

Posted: Thursday 16th January 2020 11:20pm
by vuott
Hello
...from Wiki of italian Gambas Forum:

https://www.gambas-it.org/wiki/index.ph ... enziazione

Re: Does anyone understand the gb.Map component?

Posted: Friday 17th January 2020 9:12am
by stevedee
vuott wrote: Thursday 16th January 2020 11:20pm ...from Wiki of italian Gambas Forum...
Many thanks vuott, that looks like a great resource.

This image shows an English translation of topics:-

Map_Italian.png
Map_Italian.png (74.95 KiB) Viewed 9481 times