Re: OpenStreetMap embarqué dans votre application
Publié : ven. 13/juin/2014 23:07
C'est ce que j'ai fait mais je ne voie rien de particulier. Merci de ton aide djes. Si quelqu'un d'autre peut tester sur Mac OS X...
Forums PureBasic - Français
http://forums.purebasic.com/french/
Code : Tout sélectionner
Enumeration
#Window_0
#Listview_0
#Button_0
#Button_1
#Button_2
#Button_3
#Button_4
#Image_0
#Text_0
#Text_1
#Text_2
#Text_4
#Text_5
#Text_6
#Text_7
#Text_8
#Text_9
#Text_10
#Text_11
#Text_12
#String_0
#String_1
#String_2
#String_3
#String_4
#String_5
#String_6
#String_7
#String_8
#String_9
#String_10
EndEnumeration
Global glDBSQLite.l
Global glListviewItemSelected.l = -1
Global glImageMemory.l
UseJPEG2000ImageDecoder()
UseJPEG2000ImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
Procedure Main_WindowOpen()
If OpenWindow(#Window_0, 200, 200, 800, 390, "DVP - Gestion de Contacts", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
ListViewGadget(#Listview_0, 10, 10, 300, 370)
ButtonGadget(#Button_0, 320, 350, 080, 30, "Reset")
ButtonGadget(#Button_1, 400, 350, 100, 30, "Ajouter")
ButtonGadget(#Button_2, 500, 350, 100, 30, "Mettre à jour")
ButtonGadget(#Button_3, 600, 350, 100, 30, "Supprimer")
ButtonGadget(#Button_4, 700, 350, 100, 30, "Parcourir")
TextGadget(#Text_0, 320, 20, 110, 20, "Nom :")
TextGadget(#Text_1, 320, 50, 110, 20, "Prénom : ")
TextGadget(#Text_2, 320, 80, 110, 20, "Profession : ")
TextGadget(#Text_4, 320, 110, 110, 20, "Société :")
TextGadget(#Text_5, 320, 140, 110, 20, "Adresse :")
TextGadget(#Text_6, 320, 170, 110, 20, "Code Postal :")
TextGadget(#Text_7, 320, 200, 110, 20, "Ville :")
TextGadget(#Text_8, 320, 230, 110, 20, "Pays :")
TextGadget(#Text_9, 320, 260, 110, 20, "Téléphone :")
TextGadget(#Text_10, 320, 290, 110, 20, "Fax : ")
TextGadget(#Text_11, 320, 320, 110, 20, "Mail :")
TextGadget(#Text_12, 620, 20, 110, 20, "Photo :")
StringGadget(#String_0, 450, 20, 150, 20, "")
StringGadget(#String_1, 450, 50, 150, 20, "")
StringGadget(#String_2, 450, 80, 150, 20, "")
StringGadget(#String_3, 450, 110, 150, 20, "")
StringGadget(#String_4, 450, 140, 150, 20, "")
StringGadget(#String_5, 450, 170, 150, 20, "")
StringGadget(#String_6, 450, 200, 150, 20, "")
StringGadget(#String_7, 450, 230, 150, 20, "")
StringGadget(#String_8, 450, 260, 150, 20, "")
StringGadget(#String_9, 450, 290, 150, 20, "")
StringGadget(#String_10, 450, 320, 150, 20, "")
ImageGadget(#Image_0, 620, 50, 160, 240, 0, #PB_Image_Border)
EndIf
EndProcedure
Procedure DB_Init()
Protected plFile.l
Protected psSQLRequest.s
UseSQLiteDatabase()
If FileSize(#PB_Compiler_FilePath+"GestionContacts.sqlite") < 0
plFile = CreateFile(#PB_Any, #PB_Compiler_FilePath+"GestionContacts.sqlite")
If plFile
CloseFile(plFile)
EndIf
EndIf
glDBSQLite = OpenDatabase(#PB_Any, #PB_Compiler_FilePath+"GestionContacts.sqlite", "", "", #PB_Database_SQLite)
If glDBSQLite = 0
MessageRequester("DVP - Gestion de Contacts", DatabaseError())
End
ElseIf plFile
psSQLRequest = "CREATE TABLE IF NOT EXISTS contacts ("
psSQLRequest + "id_contact INTEGER PRIMARY KEY AUTOINCREMENT Not NULL, "
psSQLRequest + "contact_nom TEXT Not NULL, "
psSQLRequest + "contact_prenom TEXT Not NULL, "
psSQLRequest + "contact_job TEXT, "
psSQLRequest + "contact_company TEXT, "
psSQLRequest + "contact_address TEXT, "
psSQLRequest + "contact_postalcode TEXT, "
psSQLRequest + "contact_city TEXT, "
psSQLRequest + "contact_country TEXT, "
psSQLRequest + "contact_phone TEXT, "
psSQLRequest + "contact_fax TEXT, "
psSQLRequest + "contact_mail TEXT, "
psSQLRequest + "contact_photo BLOB, "
psSQLRequest + "contact_photo_size INTEGER"
psSQLRequest + ")"
; Debug psSQLRequest
If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0
MessageRequester("DVP - Gestion de Contacts - DB_Init()", DatabaseError())
EndIf
EndIf
If DatabaseQuery(glDBSQLite, "SELECT * FROM contacts") <> 0
While NextDatabaseRow(glDBSQLite)
AddGadgetItem(#Listview_0, CountGadgetItems(#Listview_0), GetDatabaseString(glDBSQLite, 1) + " " + GetDatabaseString(glDBSQLite, 2))
SetGadgetItemData(#Listview_0, CountGadgetItems(#Listview_0)-1, GetDatabaseLong(glDBSQLite, 0))
Wend
FinishDatabaseQuery(glDBSQLite)
EndIf
EndProcedure
Procedure DB_InsertContact()
Protected psSQLRequest.s
psSQLRequest = "INSERT INTO contacts"
psSQLRequest + "(contact_nom, contact_prenom, contact_job, contact_company, contact_address, "
psSQLRequest + "contact_postalcode, contact_city, contact_country, contact_phone, contact_fax, contact_mail, contact_photo, contact_photo_size)"
psSQLRequest + "VALUES ("
psSQLRequest + "'"+GetGadgetText(#String_0)+"',"
psSQLRequest + "'"+GetGadgetText(#String_1)+"',"
psSQLRequest + "'"+GetGadgetText(#String_2)+"',"
psSQLRequest + "'"+GetGadgetText(#String_3)+"',"
psSQLRequest + "'"+GetGadgetText(#String_4)+"',"
psSQLRequest + "'"+GetGadgetText(#String_5)+"',"
psSQLRequest + "'"+GetGadgetText(#String_6)+"',"
psSQLRequest + "'"+GetGadgetText(#String_7)+"',"
psSQLRequest + "'"+GetGadgetText(#String_8)+"',"
psSQLRequest + "'"+GetGadgetText(#String_9)+"',"
psSQLRequest + "'"+GetGadgetText(#String_10)+"',"
psSQLRequest + "?,"
If glImageMemory
psSQLRequest + Str(MemorySize(glImageMemory))
Else
psSQLRequest + "0"
EndIf
psSQLRequest + ")"
;Debug psSQLRequest
If glImageMemory
SetDatabaseBlob(glDBSQLite, 0, glImageMemory, MemorySize(glImageMemory))
Else
ReplaceString(psSQLRequest, "?", "'0'")
EndIf
If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0
MessageRequester("DVP - Gestion de Contacts - DB_InsertContact()", DatabaseError())
EndIf
; Ajoute le dernier element ajouté à la base
If DatabaseQuery(glDBSQLite, "SELECT max(id_contact), contact_nom, contact_prenom FROM contacts") <> 0
While NextDatabaseRow(glDBSQLite)
AddGadgetItem(#Listview_0, CountGadgetItems(#Listview_0), GetDatabaseString(glDBSQLite, 1) + " " + GetDatabaseString(glDBSQLite, 2))
SetGadgetItemData(#Listview_0, CountGadgetItems(#Listview_0)-1, GetDatabaseLong(glDBSQLite, 0))
Wend
FinishDatabaseQuery(glDBSQLite)
Else
MessageRequester("DVP - Gestion de Contacts - DB_InsertContact", DatabaseError())
EndIf
EndProcedure
Procedure DB_SelectContact(GadgetItem.l)
Protected psSQLRequest.s
Protected plFileImageSize.l
Protected plFileImage.l
psSQLRequest = "SELECT * FROM contacts WHERE id_contact="+Str(GetGadgetItemData(#Listview_0, GadgetItem))
;Debug psSQLRequest
If DatabaseQuery(glDBSQLite, psSQLRequest) <> 0
While NextDatabaseRow(glDBSQLite)
SetGadgetText(#String_0, GetDatabaseString(glDBSQLite, 1))
SetGadgetText(#String_1, GetDatabaseString(glDBSQLite, 2))
SetGadgetText(#String_2, GetDatabaseString(glDBSQLite, 3))
SetGadgetText(#String_3, GetDatabaseString(glDBSQLite, 4))
SetGadgetText(#String_4, GetDatabaseString(glDBSQLite, 5))
SetGadgetText(#String_5, GetDatabaseString(glDBSQLite, 6))
SetGadgetText(#String_6, GetDatabaseString(glDBSQLite, 7))
SetGadgetText(#String_7, GetDatabaseString(glDBSQLite, 8))
SetGadgetText(#String_8, GetDatabaseString(glDBSQLite, 9))
SetGadgetText(#String_9, GetDatabaseString(glDBSQLite, 10))
SetGadgetText(#String_10, GetDatabaseString(glDBSQLite, 11))
plFileImageSize = GetDatabaseLong(glDBSQLite, 13)
If plFileImageSize > 0
glImageMemory = AllocateMemory(plFileImageSize)
GetDatabaseBlob(glDBSQLite, 12, glImageMemory, plFileImageSize)
plFileImage = CatchImage(#PB_Any, glImageMemory, plFileImageSize)
If plFileImage
ResizeImage(plFileImage, 160, 240)
SetGadgetState(#Image_0, ImageID(plFileImage))
FreeImage(plFileImage)
EndIf
Else
SetGadgetState(#Image_0, 0)
EndIf
Wend
FinishDatabaseQuery(glDBSQLite)
Else
MessageRequester("DVP - Gestion de Contacts - DB_SelectContact", DatabaseError())
EndIf
EndProcedure
Procedure DB_UpdateContact(GadgetItem.l)
Protected psSQLRequest.s
psSQLRequest = "UPDATE contacts SET "
psSQLRequest + "contact_nom='"+GetGadgetText(#String_0)+"', "
psSQLRequest + "contact_prenom='"+GetGadgetText(#String_1)+"', "
psSQLRequest + "contact_job='"+GetGadgetText(#String_2)+"', "
psSQLRequest + "contact_company='"+GetGadgetText(#String_3)+"', "
psSQLRequest + "contact_address='"+GetGadgetText(#String_4)+"', "
psSQLRequest + "contact_postalcode='"+GetGadgetText(#String_5)+"', "
psSQLRequest + "contact_city='"+GetGadgetText(#String_6)+"', "
psSQLRequest + "contact_country='"+GetGadgetText(#String_7)+"', "
psSQLRequest + "contact_phone='"+GetGadgetText(#String_8)+"', "
psSQLRequest + "contact_fax='"+GetGadgetText(#String_9)+"', "
psSQLRequest + "contact_mail='"+GetGadgetText(#String_10)+"', "
psSQLRequest + "contact_photo = ?,"
If glImageMemory
psSQLRequest + "contact_photo_size = " + Str(MemorySize(glImageMemory))
Else
psSQLRequest + "contact_photo_size = 0"
EndIf
psSQLRequest + " WHERE id_contact="+Str(GetGadgetItemData(#Listview_0, GadgetItem))
;Debug psSQLRequest
If glImageMemory
SetDatabaseBlob(glDBSQLite, 0, glImageMemory, MemorySize(glImageMemory))
Else
ReplaceString(psSQLRequest, "?", "'0'")
EndIf
If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0
MessageRequester("DVP - Gestion de Contacts - DB_UpdateContact()", DatabaseError())
Else
SetGadgetItemText(#Listview_0, GadgetItem, GetGadgetText(#String_0)+" "+GetGadgetText(#String_1))
EndIf
EndProcedure
Procedure DB_DeleteContact(GadgetItem.l)
Protected psSQLRequest.s
psSQLRequest = "DELETE FROM contacts WHERE id_contact="+Str(GetGadgetItemData(#Listview_0, GadgetItem))
;Debug psSQLRequest
If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0
MessageRequester("DVP - Gestion de Contacts - DB_DeleteContact()", DatabaseError())
Else
RemoveGadgetItem(#Listview_0, GadgetItem)
EndIf
EndProcedure
Main_WindowOpen()
DB_Init()
Repeat
glEvent = WaitWindowEvent()
glGadget= EventGadget()
glType = EventType()
If glEvent = #PB_Event_Gadget
Select glGadget
Case #Listview_0 ;{
If GetGadgetState(#Listview_0) >= 0 And GetGadgetState(#Listview_0) <= CountGadgetItems(#Listview_0)
If GetGadgetState(#Listview_0) <> glListviewItemSelected
glListviewItemSelected = GetGadgetState(#Listview_0)
DB_SelectContact(GetGadgetState(#Listview_0))
EndIf
EndIf
;}
Case #Button_0 ;{ Reset
If glType = #PB_EventType_LeftClick
Define.l dlInc
For dlInc = #String_0 To #String_10
SetGadgetText(dlInc, "")
Next
SetGadgetState(#Image_0, 0)
If glImageMemory
FreeMemory(glImageMemory)
glImageMemory = 0
EndIf
glListviewItemSelected = -1
EndIf
;}
Case #Button_1 ;{ Ajouter
If GetGadgetText(#String_0) <> "" And GetGadgetText(#String_1) <> ""
If glType = #PB_EventType_LeftClick
DB_InsertContact()
EndIf
EndIf
;}
Case #Button_2 ;{ Mettre à jour
If GetGadgetState(#Listview_0) >= 0 And GetGadgetState(#Listview_0) <= CountGadgetItems(#Listview_0)-1
If glType = #PB_EventType_LeftClick
DB_UpdateContact(GetGadgetState(#Listview_0))
EndIf
EndIf
;}
Case #Button_3 ;{ Supprimer
If GetGadgetState(#Listview_0) >= 0 And GetGadgetState(#Listview_0) <= CountGadgetItems(#Listview_0)-1
If glType = #PB_EventType_LeftClick
If MessageRequester("DVP - Gestion de Contacts", "Voulez vous vraiment supprimer le contact courant "+Chr(34)+GetGadgetItemText(#Listview_0, GetGadgetState(#Listview_0))+Chr(34)+" ?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
DB_DeleteContact(GetGadgetState(#Listview_0))
EndIf
EndIf
EndIf
;}
Case #Button_4 ;{ Parcourir
Define.s psFileImage
Define.l plFileImage
; Ouvre une boîte de dialogue standard pour choisir une image
psFileImage = OpenFileRequester("Choisissez une image à charger", "", "Fichiers Images|*.png;*.bmp;*.jpg;*.jpeg|Tous les fichiers (*.*)|*.*", 0)
If psFileImage
; Charge une image
plFileImage = LoadImage(#PB_Any, psFileImage)
If plFileImage
; Redimensionne l'image à la taille de l'ImageGadget
ResizeImage(plFileImage, 160, 240)
; Change l'image affichée par l' ImageGadget
SetGadgetState(#Image_0, ImageID(plFileImage))
; Libère l'image
FreeImage(plFileImage)
; Charge l'image dans la zone mémoire globale dédiée 'glImageMemory'
plFileImage = OpenFile(#PB_Any, psFileImage)
If plFileImage
glImageMemory = AllocateMemory(Lof(plFileImage))
If glImageMemory
ReadData(plFileImage, glImageMemory, Lof(plFileImage))
EndIf
CloseFile(plFileImage)
EndIf
EndIf
EndIf
;}
EndSelect
EndIf
Until glEvent = #PB_Event_CloseWindow
End
Code : Tout sélectionner
*LibCurl_NewMemory = AllocateMemory(MemorySize(*LibCurl_SharedMem))
Code : Tout sélectionner
;@author Progi1984
;@changelog 1.0 24/08/09 Initial release (cache, zoom, moving, layers)
;@changelog 1.1 31/08/09 ADDED : Mimize Window
;@+ ADDED : Zoom with click on the ImageGadget
;@thanks akj (http://www.purebasic.fr/english/profile.php?mode=viewprofile&u=292) for ideas and bug reporting
;@thanks netmaestro (http://www.purebasic.fr/english/profile.php?mode=viewprofile&u=1912) for tests (Vista) and bug reporting
;@misc : Prerequisites : For Windows, http://www.microsoft.com/downloads/details.aspx?FamilyID=a5c84275-3b97-4ab7-a40d-3802b2af5fc2&displaylang=en
;@changelog by thyphoon remove curl dependencies and add latitude/longitude gadget edit
InitNetwork()
IncludePath ""
UsePNGImageDecoder()
UseSQLiteDatabase()
EnableExplicit
Enumeration
#Window_0
#Image_0
#Image_1
#Image_2
#Image_3
#Button_0
#Button_1
#Button_2
#Button_3
#Button_4
#Button_5
#Combo_0
#Text_0
#Text_1
#Text_2
#Text_3
#Text_4
#String_0
#String_1
EndEnumeration
#_DQ_ = Chr(34)
Global gfLatitude.d = 49.0422777
Global gfLongitude.d = 2.035566
Global gfxTile.f
Global gfyTile.f
Global glZoom.l = 17
Global glLayerCur.l = 0
Global glSQLiteDB.l
Global gsSQLiteDBPath.s = ""
Global glGadget.l
Global glEvent.l
Global glQuit.l
Structure S_OSM_Layers
sName.s
sURL.s
lZoomMin.l
lZoomMax.l
EndStructure
#OSM_Layers_Num = 5
Global Dim OSM_Layers_Dim.S_OSM_Layers(#OSM_Layers_Num)
OSM_Layers_Dim(0)\sName = "OSM Mapnik"
OSM_Layers_Dim(0)\sURL = "http://tile.openstreetmap.org/"
OSM_Layers_Dim(0)\lZoomMin = 0
OSM_Layers_Dim(0)\lZoomMax = 18
OSM_Layers_Dim(1)\sName = "OSM Osmarender/Tiles@Home"
OSM_Layers_Dim(1)\sURL = "http://tah.openstreetmap.org/Tiles/tile/"
OSM_Layers_Dim(1)\lZoomMin = 0
OSM_Layers_Dim(1)\lZoomMax = 17
OSM_Layers_Dim(2)\sName = "OSM Cycle Map"
OSM_Layers_Dim(2)\sURL = "http://andy.sandbox.cloudmade.com/tiles/cycle/"
OSM_Layers_Dim(2)\lZoomMin = 0
OSM_Layers_Dim(2)\lZoomMax = 18
OSM_Layers_Dim(3)\sName = "OSM Maplint"
OSM_Layers_Dim(3)\sURL = "http://tah.openstreetmap.org/Tiles/maplint/"
OSM_Layers_Dim(3)\lZoomMin = 12
OSM_Layers_Dim(3)\lZoomMax = 16
OSM_Layers_Dim(4)\sName = "NPE out-of-copyright map "
OSM_Layers_Dim(4)\sURL = "http://richard.dev.openstreetmap.org/npe/"
OSM_Layers_Dim(4)\lZoomMin = 13
OSM_Layers_Dim(4)\lZoomMax = 15
Define.f pfValue
;@desc Open the main window
;@author Progi1984
Procedure WindowMain_Open()
If OpenWindow(#Window_0, 260, 225, 700, 571, "OpenStreetMap", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
LoadFont(0,"Wingdings",12)
LoadFont(1,"Arial",12,#PB_Font_Bold)
ImageGadget(#Image_0, 10, 10, 256, 256, 0)
ImageGadget(#Image_1, 266, 10, 256, 256, 0)
ImageGadget(#Image_2, 10, 266, 256, 256, 0)
ImageGadget(#Image_3, 266, 266, 256, 256, 0)
TextGadget(#Text_0, 530, 10, 60, 15, "Layers :")
ComboBoxGadget(#Combo_0, 550, 30, 130, 20)
AddGadgetItem(#Combo_0, 0, OSM_Layers_Dim(0)\sName)
AddGadgetItem(#Combo_0, 1, OSM_Layers_Dim(1)\sName)
AddGadgetItem(#Combo_0, 2, OSM_Layers_Dim(2)\sName)
AddGadgetItem(#Combo_0, 3, OSM_Layers_Dim(3)\sName)
AddGadgetItem(#Combo_0, 4, OSM_Layers_Dim(4)\sName)
SetGadgetState(#Combo_0, 0)
TextGadget(#Text_1, 530, 50, 60, 15, "Movements :")
ButtonGadget(#Button_0, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Button_0, FontID(0))
ButtonGadget(#Button_1, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Button_1, FontID(0))
ButtonGadget(#Button_2, 580, 070, 30, 30, Chr($E9)) : SetGadgetFont(#Button_2, FontID(0))
ButtonGadget(#Button_3, 580, 130, 30, 30, Chr($EA)) : SetGadgetFont(#Button_3, FontID(0))
TextGadget(#Text_2, 530, 160, 60, 15, "Zoom :")
ButtonGadget(#Button_4, 550, 180, 50, 30, "+") : SetGadgetFont(#Button_4, FontID(1))
ButtonGadget(#Button_5, 600, 180, 50, 30, "-") : SetGadgetFont(#Button_5, FontID(1))
TextGadget(#Text_3, 530, 230, 60, 15, "Latitude :")
StringGadget(#String_0, 600, 230, 90, 20, StrD(gfLatitude))
TextGadget(#Text_4, 530, 250, 60, 15, "Longitude :")
StringGadget(#String_1, 600, 250, 90, 20, StrD(gfLongitude))
EndIf
EndProcedure
;@desc Permits to translate XY Coordinates to Lon/Lat Coordinates
;@author Progi1984
Procedure OSM_LatLon2XY()
Protected n.l = Pow(2, glZoom)
gfxTile = Int(((gfLongitude + 180) / 360) * n)
gfyTile = Int((1-(Log(Tan(gfLatitude * #PI / 180)+(1/Cos(gfLatitude*#PI/180)))/#PI))/2*n)
EndProcedure
;@desc Permits to translate Lon/Lat Coordinates to XY Coordinates
;@author Progi1984
Procedure OSM_XY2LatLon()
Protected n.l = Pow(2, glZoom)
Protected pfLatitudeRad.f
gfLongitude = gfxTile / n * 360.0 - 180.0
pfLatitudeRad = ATan(SinH(#PI * (1 - 2 * gfyTile / n)))
gfLatitude = pfLatitudeRad * 180.0 / #PI
SetGadgetText(#String_0,StrD(gfLatitude))
SetGadgetText(#String_1,StrD(gfLongitude))
EndProcedure
;@desc Draws 4 subtiles for the XY Coordinates
;@author Progi1984
Macro OSM_GetSquareTile()
Debug "======"
OSM_GetImage(0, glZoom+1,2*gfxTile ,2*gfyTile)
OSM_GetImage(1, glZoom+1,2*gfxTile+1,2*gfyTile)
OSM_GetImage(2, glZoom+1,2*gfxTile ,2*gfyTile+1)
OSM_GetImage(3, glZoom+1,2*gfxTile+1,2*gfyTile+1)
Debug "======"
EndMacro
Macro OSM_TestXYCoordinates(_CoordTile_, _Inc_, _Test_)
pfValue = _CoordTile_ _Inc_ 0.5
If pfValue _Test_
_CoordTile_ = Pow(2, glZoom) - 2
Else
_CoordTile_ = pfValue
EndIf
EndMacro
Procedure OSM_GetImage(Image.l, Zoom.l, XTile.l, YTile.l)
Protected plMemory.l
Protected psURL.s = OSM_Layers_Dim(glLayerCur)\sURL+Str(Zoom)+"/"+Str(XTile)+"/"+Str(YTile)+".png"
Protected plImageSize.l
Protected plImageMem.l
Protected psSQLRequest.s
Protected pbImageOK.b
Protected plRes.l
; Test if in cache else download it
DatabaseQuery(glSQLiteDB, "SELECT * FROM cache WHERE cache_layer="+#_DQ_+Str(glLayerCur)+#_DQ_+" AND cache_zoom="+#_DQ_+Str(Zoom)+#_DQ_+" AND cache_x="+#_DQ_+Str(XTile)+#_DQ_+" AND cache_y="+#_DQ_+Str(YTile)+#_DQ_+"")
If FirstDatabaseRow(glSQLiteDB)
Debug "CACHE : "+Str(glLayerCur)+"\"+Str(Zoom)+"\"+Str(XTile)+"\"+Str(Ytile)
plImageSize = GetDatabaseLong(glSQLiteDB,6)
If plImageSize
plImageMem = AllocateMemory(plImageSize)
GetDatabaseBlob(glSQLiteDB, 5, plImageMem, plImageSize)
FinishDatabaseQuery(glSQLiteDB)
If IsImage(Image)
FreeImage(Image)
EndIf
If CatchImage(Image, plImageMem)
SetGadgetState(#Image_0+Image, ImageID(Image))
FreeMemory(plImageMem)
EndIf
pbImageOK = #True
EndIf
Else
Debug DatabaseError()
EndIf
If pbImageOK = #False
Debug "DOWNLOAD : "+Str(glLayerCur)+"\"+Str(Zoom)+"\"+Str(XTile)+"\"+Str(Ytile)
plMemory = ReceiveHTTPMemory(psURL)
If plMemory
psSQLRequest = "INSERT INTO cache(cache_layer, cache_zoom, cache_x, cache_y, cache_image, cache_imagesize) VALUES ("
psSQLRequest + Str(glLayerCur)+","
psSQLRequest + Str(Zoom)+","
psSQLRequest + Str(XTile)+","
psSQLRequest + Str(YTile)+","
psSQLRequest + "?,"
psSQLRequest + Str(MemorySize(plMemory))+")"
SetDatabaseBlob(glSQLiteDB, 0, plMemory, MemorySize(plMemory))
plRes = DatabaseUpdate(glSQLiteDB, psSQLRequest)
If plRes = 0
Debug psSQLRequest
Debug DatabaseError()
EndIf
If IsImage(Image)
FreeImage(Image)
EndIf
If CatchImage(Image, plMemory, MemorySize(plMemory))
SetGadgetState(#Image_0+Image, ImageID(Image))
FreeMemory(plMemory)
EndIf
EndIf
EndIf
EndProcedure
Procedure OSM_CacheInit()
Protected psSQLRequest.s
If gsSQLiteDBPath = ""
gsSQLiteDBPath = GetCurrentDirectory()+"OSMCache.sqlite"
EndIf
If FileSize(gsSQLiteDBPath) <= 0
If CreateFile(0, gsSQLiteDBPath)
CloseFile(0)
EndIf
glSQLiteDB = OpenDatabase(#PB_Any, gsSQLiteDBPath, "", "")
If glSQLiteDB
psSQLRequest = "CREATE TABLE "+#_DQ_+"main"+#_DQ_+"."+#_DQ_+"cache"+#_DQ_+" ("
psSQLRequest + #_DQ_+"id_cache"+#_DQ_+" INTEGER PRIMARY KEY AUTOINCREMENT Not NULL , "
psSQLRequest + #_DQ_+"cache_layer"+#_DQ_+" INTEGER Not NULL Default 0, "
psSQLRequest + #_DQ_+"cache_zoom"+#_DQ_+" INTEGER Not NULL , "
psSQLRequest + #_DQ_+"cache_x"+#_DQ_+" INTEGER Not NULL , "
psSQLRequest + #_DQ_+"cache_y"+#_DQ_+" INTEGER Not NULL , "
psSQLRequest + #_DQ_+"cache_image"+#_DQ_+" BLOB, "
psSQLRequest + #_DQ_+"cache_imagesize"+#_DQ_+" INTEGER Not NULL"
psSQLRequest + ")"
DatabaseUpdate(glSQLiteDB, psSQLRequest)
EndIf
Else
glSQLiteDB = OpenDatabase(#PB_Any, gsSQLiteDBPath, "", "")
EndIf
EndProcedure
WindowMain_Open()
; Init DB
OSM_CacheInit()
; Init Map
OSM_LatLon2XY()
OSM_GetSquareTile()
Repeat
glEvent = WaitWindowEvent()
Select glEvent
Case #PB_Event_CloseWindow : glQuit = 1
Case #PB_Event_Gadget ;{
glGadget = EventGadget()
Select glGadget
Case #Button_0 ;{ To left
OSM_TestXYCoordinates(gfxTile, -, < 0)
OSM_GetSquareTile()
OSM_XY2LatLon()
SetGadgetText(#String_1, StrF(gfLongitude))
;}
Case #Button_1 ;{ To Right
OSM_TestXYCoordinates(gfxTile, +,> Pow(2, glZoom) - 1)
OSM_GetSquareTile()
OSM_XY2LatLon()
SetGadgetText(#String_1, StrF(gfLongitude))
;}
Case #Button_2 ;{ To Top
OSM_TestXYCoordinates(gfyTile, -, < 0)
OSM_GetSquareTile()
OSM_XY2LatLon()
SetGadgetText(#String_0, StrF(gfLatitude))
;}
Case #Button_3 ;{ To Bottom
OSM_TestXYCoordinates(gfyTile, +, > Pow(2, glZoom) - 1)
OSM_GetSquareTile()
OSM_XY2LatLon()
SetGadgetText(#String_0, StrF(gfLatitude))
;}
Case #Button_4 ;{ Zoom +
If glZoom < OSM_Layers_Dim(glLayerCur)\lZoomMax
glZoom + 1
OSM_LatLon2XY()
OSM_GetSquareTile()
EndIf
;}
Case #Button_5 ;{ Zoom -
If glZoom > OSM_Layers_Dim(glLayerCur)\lZoomMin
glZoom - 1
OSM_LatLon2XY()
OSM_GetSquareTile()
EndIf
;}
Case #Combo_0 ;{ Layers
pfValue = GetGadgetState(#Combo_0)
If pfValue >= 0 And pfValue < #OSM_Layers_Num And pfValue <> glLayerCur
; Current Layer
glLayerCur = pfValue
; Zoom
If glZoom < OSM_Layers_Dim(glLayerCur)\lZoomMin
glZoom = OSM_Layers_Dim(glLayerCur)\lZoomMin +1
EndIf
If glZoom > OSM_Layers_Dim(glLayerCur)\lZoomMax
glZoom = OSM_Layers_Dim(glLayerCur)\lZoomMax
EndIf
OSM_LatLon2XY()
OSM_GetSquareTile()
EndIf
;}
Case #Image_0 ;{
gfxTile = 2*gfxTile
gfyTile = 2*gfyTile
glZoom + 1
OSM_XY2LatLon()
OSM_GetSquareTile()
;}
Case #Image_1 ;{
gfxTile = 2*gfxTile +1
gfyTile = 2*gfyTile
glZoom + 1
OSM_XY2LatLon()
OSM_GetSquareTile()
;}
Case #Image_2 ;{
gfxTile = 2*gfxTile
gfyTile = 2*gfyTile+1
glZoom + 1
OSM_XY2LatLon()
OSM_GetSquareTile()
;}
Case #Image_3 ;{
gfxTile = 2*gfxTile +1
gfyTile = 2*gfyTile+1
glZoom + 1
OSM_XY2LatLon()
OSM_GetSquareTile()
;}
Case #String_0,#String_1
gfLatitude=ValD(GetGadgetText(#String_0))
gfLongitude=ValD(GetGadgetText(#String_1))
OSM_LatLon2XY()
OSM_GetSquareTile()
EndSelect
;}
EndSelect
Until glQuit = 1
j'ai ouvert un autre sujet a propos de OpenStreetMap car j'essaye aussi de mon côté de faire un module utilisable de partoutMetalOS a écrit :Je test dès que je trouve du temps de libre. Merci