B4A Class Open Street Map viewer - GPS

Hi,

This b4Xlib contains a custom view (cvMap) which can display Open Street Map.


screenshot.png
View attachment 109081



The tiles are retrieved from the internet and cached in a database. You can add shapes and images on the map.

UI :
- Lat/lng Center of the map
- Zoom Level
- Compass Direction with rotation
- Scale
- Button Menu
- Grid
- Center
- GPS position and bearing

Event :
- ready
- Lat/lng changed
- Zoom Level Changed
- Compass Direction Changed
- Shape Clicked
- Map Clicked
- Center Lat/lng clicked
- Button Menu clicked
- Scale clicked
- Compass Clicked
- GPS Clicked

Tile Server
2 new properties :
This 2 properties are available in the designer or with setter/getter from the cvmap : userAgent, tileServer

Dependencies :
- Core
- SQL
- OKHttpUtils2
- XUI
- XUI views

Other files in the b4xlib :
- coMapUtilities : code module with Types, functions, helpers
- clTileManager : standard class module to load tile from database and/or internet and save them into the database
- clMapShapeCirgle : standard class to draw circle on the map
- clMapShapeLine : standard class to draw line on the map
- clMapShapePolygon : standard class to draw polygon on the map
- clMapShapeImage : standard class to draw image on the map
- layout cvmap.bal and cvmap.bjl
- images for the compass and gps



How to use it :
- Just add the cvMap custom view with the designer
- create sub to handle events if necessary
- set options (lat/lng, zoom.....)

B4A example and B4J example included



spsp
 

Attachments

  • Screenshot_2021-02-21-11-49-10-222_map.b4A.com.jpg
    Screenshot_2021-02-21-11-49-10-222_map.b4A.com.jpg
    258.3 KB · Views: 867
  • B4XMap.b4xlib
    23.3 KB · Views: 271
  • B4Xmap.zip
    23 KB · Views: 299
Last edited:

Indy

Active Member
Licensed User
I have adapted a version for b4i. Obviously it would have been much better to recreate everything with B4XPages, but I don't have the time.

PS. I have not reported everything on B4XLib. If I can in the next few days
PS.2. (Hint) Maybe all panel/pane declarations should be changed in B4XView and created panels with XUI.CreatePanel.

I'd like to try this version but, in an Android project not B4XPages. Can this be adapted for Android as is or do I need to change things?

Thanks
 

Star-Dust

Expert
Licensed User
I'd like to try this version but, in an Android project not B4XPages. Can this be adapted for Android as is or do I need to change things?

Thanks
In the first post you find the b4a version not b4xpages.
 

oparra

Well-Known Member
Licensed User
i get error ResponseError. Reason: , Response: Access denied.

View attachment 109729



B4X:
'Get Tile from internet openstreemap website
public Sub getTileFromInternet(aZ As Int,aX As Int,aY As Int) As ResumableSub
    Dim j As HttpJob
    Dim bmp As B4XBitmap
    Try
        j.Initialize("", Me)
        j.Download($"https://a.tile.openstreetmap.org/${aZ}/${aX}/${aY}.png"$)
        j.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Try
                bmp=j.GetBitmap
            Catch
                Log(LastException.Message)
            End Try
        End If
    Catch
        Log(LastException.Message)
    End Try
    If j.IsInitialized Then
        j.Release
    End If
    Return bmp
End Sub

Change tile server??
 

oparra

Well-Known Member
Licensed User
changing user agent works fine

j.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
' j.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0")
 

Star-Dust

Expert
Licensed User

Carlos marin

Active Member
Licensed User


B4X:
'Get Tile from internet openstreemap website
public Sub getTileFromInternet(aZ As Int,aX As Int,aY As Int) As ResumableSub
    Dim j As HttpJob
    Dim bmp As B4XBitmap
    Try
        j.Initialize("", Me)
        j.Download($"https://a.tile.openstreetmap.org/${aZ}/${aX}/${aY}.png"$)
        j.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Try
                bmp=j.GetBitmap
            Catch
                Log(LastException.Message)
            End Try
        End If
    Catch
        Log(LastException.Message)
    End Try
    If j.IsInitialized Then
        j.Release
    End If
    Return bmp
End Sub

Change tile server??

Thanks for answering, I can't find this sub in the example, I don't know where to put it or where to call it, I don't get exactly where this error comes from
 

spsp

Member
Licensed User
Thanks for answering, I can't find this sub in the example, I don't know where to put it or where to call it, I don't get exactly where this error comes from
Hi,

The user-agent is in the file clMapTileManager.bas line 69 in the b4Xmap.b4Xlib (which is a zip file)

1615911143318.png


I don't know why it is OK in the B4A example and not in the B4J example.
 

spsp

Member
Licensed User
I'd like to try this version but, in an Android project not B4XPages. Can this be adapted for Android as is or do I need to change things?

Thanks

Hi,

an minimalist B4A example is attached in this post
 

Attachments

  • B4AOnlyMap.zip
    3.3 KB · Views: 91

oparra

Well-Known Member
Licensed User
Hi,

The user-agent is in the file clMapTileManager.bas line 69 in the b4Xmap.b4Xlib (which is a zip file)

View attachment 109806

I don't know why it is OK in the B4A example and not in the B4J example.
Read User-Agent;

Technical Usage Requirements
  • Valid HTTP User-Agent identifying application. Faking another app’s User-Agent WILL get you blocked.
  • If known, a valid HTTP Referer.
  • DO NOT send no-cache headers. (“Cache-Control: no-cache”, “Pragma: no-cache” etc.)
  • Cache Tile downloads locally according to HTTP Expiry Header, alternatively a minimum of 7 days.
  • Maximum of 2 download threads. (Unmodified web browsers’ download thread limits are acceptable.)
 

Indy

Active Member
Licensed User
Hi,

an minimalist B4A example is attached in this post
Hi

Thank you for taking the time to put together a minimal project for me, really appreciate it. Unfortunately, when I run this I get a
B4X:
ResponseError. Reason: , Response: Access denied.

I don't know what's raising that error. Is there something I need to do? I don't appear to get any permission prompt, which looking at your B4XPages version you had.

Thanks
 

spsp

Member
Licensed User
Hi

Thank you for taking the time to put together a minimal project for me, really appreciate it. Unfortunately, when I run this I get a
B4X:
ResponseError. Reason: , Response: Access denied.

I don't know what's raising that error. Is there something I need to do? I don't appear to get any permission prompt, which looking at your B4XPages version you had.

Thanks

Hi,

The permission is only for GPS, I don't use it in the minimal example.

Download again in the first post the B4XMap.b4Xlib. I've modified the user-agent. It should be OK

spsp
 

Indy

Active Member
Licensed User
Hi,

The permission is only for GPS, I don't use it in the minimal example.

Download again in the first post the B4XMap.b4Xlib. I've modified the user-agent. It should be OK

spsp

Just downloaded the lib again and retried the app - Worked perfectly! Thank you for your help. I should now be in a good position to convert my current app to use OSM instead of Google Maps.

Thanks again.
 

oparra

Well-Known Member
Licensed User
You could add the option to change the tile server (ServerTile) and user agent (UserAgent) by parameters.

Sample:
j.Download($"${ServerTile}${aZ}/${aX}/${aY}.png"$)

j.GetRequest.SetHeader("User-Agent",$"{UserAgent}"$)


to comply with the restrictions of tile servers.

User Agent: Email or App Name or Package Name.
Server Tile: use one the lista server or other personal : https://wiki.openstreetmap.org/wiki/Tile_servers
 

spsp

Member
Licensed User
You could add the option to change the tile server (ServerTile) and user agent (UserAgent) by parameters.

Sample:
j.Download($"${ServerTile}${aZ}/${aX}/${aY}.png"$)

j.GetRequest.SetHeader("User-Agent",$"{UserAgent}"$)


to comply with the restrictions of tile servers.

User Agent: Email or App Name or Package Name.
Server Tile: use one the lista server or other personal : https://wiki.openstreetmap.org/wiki/Tile_servers

Hi,

properties userAgent and tileServer added, see post #1

Thank you for your suggestion

spsp
 

Indy

Active Member
Licensed User
Hi,

Is there a way of reading back the current GPS position? I tried to see if there was a way of using the GPS_LocationChanged (aLocation As Location) event but that doesn't seem to do anything.

B4X:
Log(aLocation.Latitude)

I'm looking at incorporating the ability to be able to freely move the map (as it does now) and then click a button to put the map back to where I am now (current GPS pos).

Thanks
 

spsp

Member
Licensed User
Hi,

Is there a way of reading back the current GPS position? I tried to see if there was a way of using the GPS_LocationChanged (aLocation As Location) event but that doesn't seem to do anything.

B4X:
Log(aLocation.Latitude)

I'm looking at incorporating the ability to be able to freely move the map (as it does now) and then click a button to put the map back to where I am now (current GPS pos).

Thanks


Hi,

In cvMap , when the property followGPS is set to true, the map is centered and oriented according the GPS. when this property is set to false, you can move the map.

You can set to false/true this property with a button
 
Top