Google Static Maps

You can post your code snippets here for others to use and learn from
Post Reply
User avatar
Redlion
Posts: 94
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Google Static Maps

Post by Redlion »

Hello all,

This is really to big to be a snippet but I was not sure where to put it.
I have had a lot of fun with this and I thought that others might like to see what can be done with Hollywood.

This uses Royale but it could easily be made to be cross platform.

Check out developers.google.com/maps/documentation/staticmaps/ for more info.

Here we go.

Code: Select all

@VERSION 6,0
@REQUIRE "MUIRoyale"
@APPTITLE "Mapper"
@APPVERSION "$VER: 1.0 (01.05.15)"
@APPCOPYRIGHT "©2015, George Leo den Hollander"
@APPAUTHOR "Leo den Hollander"
@APPDESCRIPTION "GOOGLE MAPPING PROGRAM"

; Check out developers.google.com/maps/documentation/staticmaps/

@DISPLAY {Hidden = True}

DTW = getattribute(#Display,0,#AttrMaxWidth)
DTH = getattribute(#Display,0,#AttrMaxHeight)

xml$ =  ReplaceStr(FileToString("Mapper.xml"), "%DTW", DTW)
xml$ =  ReplaceStr(xml$, "%DTH", DTH)

if DTW > 1024
  IWsize = 1024
endif
if DTH > 950
  IHsize = 900
endif
createbrush(1,IWsize,IHsize)
Lat$ = "-29.30001"
Long$ = "134.00001"
MapAddress$ = Lat$..","..Long$
Zoom = 4
Global Markers$ = "markers=size:tiny"
MapType = 2

Dim LLdata[101]

Function GetMap(Mapaddress$,Zoom,Maptype,Markers$)

mui.set("Editor","contents","")    
openconnection(0,"maps.google.com",80)

    Request2$="/maps/api/staticmap?center="..Mapaddress$.."&"..Markers$.."&zoom="..strstr(zoom).."&size=512x475&scale=2"

    if MapType = 0
        Type$ = "roadmap"
    elseif MapType = 1
        Type$ = "satellite"
    elseif MapType = 2
        Type$ = "terrain"
    elseif MapType = 3
        Type$ = "hybrid"
    endif

    Request2$ = Request2$ .. "&maptype="..Type$

    Request2$ = Request2$ .. "&sensor=false"

    Request$ ="GET " .. Request2$ .. " HTTP/1.0" .. "\r\n"

    Request$ = Request$ .. "Content-Type: application/x-www-form-urlencoded; charset=iso-8859-1" .. "\r\n"

    Request$ = Request$ .. "Host: maps.google.com" .. "\r\n" .. "\r\n"

    NLA = val(Lat$)
    Lat$=strstr(NLA,4)
    mui.set("latitude","contents",Lat$)
    NLO = val(Long$)
    Long$=strstr(NLO,4)
    mui.set("longitude","contents",Long$)
    mui.set("Output","contents","")
    sendcount = SendData(0,Request$)
    mui.set("Output","contents",Request$)

    InData$, dwBytes, done = receiveData(0,#RECEIVEALL)

CloseConnection(0)

st = findstr(InData$,"\r\n\r\n")
words = leftstr(indata$,st)
mui.set("Editor","contents",words)
pic = midstr(indata$,st+4,dwBytes-st+4)
stringtofile(pic,"RAM:Google.png")
loadbrush(1,"RAM:Google.png",{width=1024,height=900})

mui.set("normal","Brush",1)
EndFunction

Function LoadinData()
    Datafile$ = ""
    Datafile$ = FileRequest("Open a File","",#REQ_NORMAL,"","")
    If Datafile$ <> ""
         x= 1
         Openfile(2,Datafile$,#MODE_READ)
             mui.set("File","contents",DataFile$)
             for d$ in FileLines(DataFile$)
                 LLdata[x] = d$
                 x=x+1
             next
             closefile(2) 
             x=x-1
             Convert$= ""
             for t = 1 to x step 4
                 if t > 3 and LLdata[t] = "C2" and LLdata[t-4] = "C1"
                     MC$ = "&markers=size:mid|color:blue|"
                 elseif LLdata[t] = "C3" and LLdata[t-4] = "C2"
                     MC$ = "&markers=size:mid|color:green|"
                 elseif LLdata[t] = "C4" and LLdata[t-4] = "C3"
                     MC$ = "&markers=size:mid|color:purple|"
                 elseif LLdata[t] = "C5" and LLdata[t-4] = "C4"
                     MC$ = "&markers=size:mid|color:white|"
                 else
                     MC$ = ""
                 endif
                 LB$ = ""
                 if LLdata[t+1]<>""
                    LB$ = "label:"..LLdata[t+1].."|"
                 endif
                 Convert$ = Convert$..MC$..LB$..LLdata[t+2]..","..LLdata[t+3].."|"
            next
            Markers$ = "&markers=size:mid|color:red|"..Convert$
            ; valid colours red blue green purple yellow orange gray black brown white
            ; valid sizes tiny mid small
            
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)
            
        Endif
EndFunction

Function p_MUIEvent(id) ; * Joint event handler for MUI buttons and menus ***********************************
    Switch id
        Case "Home":
            Lat$ = "-29.30001"
            Long$ = "134.00001"
            MapAddress$ = Lat$..","..Long$
            Zoom = 4
            Markers$ = "markers=size:tiny"
            MapType = 0
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)

        Case "down":
            Latval = val(lat$)
            d=80
            for z = 1 to zoom
              d=d/2
            next 
            Lat$= strstr(Latval-d,4)
            mui.set("file","contents",Lat$)
            MapAddress$ = Lat$..","..Long$
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)

        Case "up":
            Latval = val(lat$)
            u=80
            for z = 1 to zoom
              u=u/2
            next  
            Lat$= strstr(Latval+u,4)
            MapAddress$ = Lat$..","..Long$
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)

        Case "left":
            Longval = val(Long$)
            l=80
            for z = 1 to zoom
              l=l/2
            next  
            Long$= strstr(Longval-l,4)
            MapAddress$ = Lat$..","..Long$
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)

        Case "right":
            Longval = val(Long$)
            r=80
            for z = 1 to zoom
              r=r/2
            next  
            Long$= strstr(Longval+r,4)
            MapAddress$ = Lat$..","..Long$
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)
        
        Case "Set":
            Lat$ = mui.get("CenterLat","Contents")
            Long$ = mui.get("Centerlong","Contents")
            MapAddress$ = Lat$..","..Long$
            GetMap(Mapaddress$,Zoom,Maptype,Markers$)

        Case "Load":
            LoadinData()

        Case "Quit":
            End
    EndSwitch
EndFunction

Function p_EventFunc(msg)
    Switch msg.action
        Case "MUIRoyale":
            Switch msg.attribute
                Case "CloseRequest":
                     End
        
                Case "Pressed":
                    p_MUIEvent(msg.id)
            
                Case "Active":
                    Switch msg.id
                        Case "Type":
                            MapType = mui.Get("Type", "active", msg.triggervalue)
                            GetMap(Mapaddress$,Zoom,Maptype,Markers$)
                    endswitch

                Case "Level"    
                    Switch msg.id
                        Case "zoom":
                            zoom = msg.triggervalue
                            GetMap(Mapaddress$,Zoom,Maptype,Markers$)
                    EndSwitch
            Endswitch

        Case "HideWindow":
            MUI.Set("app", "iconified", True)

        Case "ShowWindow":
            MUI.Set("app", "iconified", False)

    EndSwitch
EndFunction

mui.CreateGui([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app" base="Map">
]] .. xml$ .. "</application>")

; listen to these events!
InstallEventHandler({MUIRoyale = p_EventFunc, HideWindow = p_EventFunc, ShowWindow = p_EventFunc})

EscapeQuit(True)

GetMap(Mapaddress$,Zoom,Maptype,Markers$)

; main loop!
Repeat
    WaitEvent
Forever
The XML File should be saved as Mapper.xml

Code: Select all

<window title=" Google Mapper" id="window" width="%DTW" height="%DTH" notify="closerequest" >
    <vgroup background="pre_ShadowFill">
        <vgroup>
            <hgroup>
                <image id="normal" source="Brush: 1"/>
                <vgroup frame="group">
                    <label centered="1">\33bGOOGLE MAPPER</label>
                    <rectangle maxheight="5"/>
                    <button id="Home" notify="pressed">HOME</button>
                    <rectangle maxheight="20"/>
                    <label leftaligned="true">LATITUDE</label>
                    <string id="Latitude" contents=""/>
                    <label leftaligned="true">LONGATUDE</label>
                    <string id="Longitude" contents=""/>
                    <label leftaligned="true">ZOOM</label>
                    <slider id="zoom" notify="level" level="4" min="1" max="20"/>
                    <rectangle maxheight="10"/>
                    <button id="up" notify="pressed">Move Map Up</button>
                    <button id="down" notify="pressed">Move Map Down</button>
                    <button id="left" notify="pressed">Move MapLeft</button>
                    <button id="right" notify="pressed">Move Map Right</button>
                    <rectangle maxheight="20"/>
                    <label leftaligned="true"> Manual Set- Center of Map</label>
                    <hgroup>
                        <label leftaligned="true"> Lat </label>
                        <string id="CenterLat" contents=""/>
                        <label leftaligned="true"> Long </label>
                        <string id="CenterLong" contents=""/>
                    </hgroup>
                    <button id="Set" notify="pressed">Set Lat Long</button>
                    <rectangle/>
                    <label leftaligned="true">MAP TYPE</label>
                    <cycle id="Type" notify="Active">
                        <item>Road Map</item>
                        <item>Satalite</item>
                        <item>Terrain</item>
                        <item>Hybrid</item>
                    </cycle>
                    <rectangle/>
                    <texteditor Id="Editor" Contents="" rows="15" scrollbar="true"/>
                    <rectangle maxheight="20"/>
                    <button id="Load" notify="pressed">Load Data File</button>
                    <string id="File" contents=""/>
                </vgroup>
            </hgroup>
            <string id="Output" maxlen="2000" contents=""/>   
        </vgroup>  
    </vgroup>
</window>
and here is a copy of simple data file. (make sure that there are no extra spaces) - max data points about 100

Code: Select all

C1
A
-32.95853361
116.3187218
C2
B
-35.65263456
118.32189876
C3
C
-34.35855368
116.3189344
C4
1
-34.05853361
117.3189344
This is very basic at the moment,but I would like to make it a bit more user friendly when I get time.

Basic Instruction to follow in next post.

No Promises, No Warranties.

Cheers
Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
User avatar
Redlion
Posts: 94
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: Google Static Maps

Post by Redlion »

Google Mapper Basic Instructions.

Home Button gets you back to the Start Map (Australia) the best place on earth.

Latitude & Longitide - Show the current Lat and Long of the center of the Map.

Zoom - slider to adjust the zoom level ( Note not all places have maps that can go down to 19 (21)- Cities seem to be quite good but country side is a little less detailed.

Move Map Up - move the center of the map in upward direction.
Move Map Down - move the center of the map in downward direction.
Move Map Left - move the center of the map in left direction.
Move Map Right - move the center of the map in right direction.

the amount that the map moves is depenant on the zoom value. (see calculation)

Manual Set Center of Map enter the Latitude and Longitude values for the Center of the Map. Press Set Lat Long button to set the selected center point.

Map Type - cycle button to select type of Map background.
Types are:
Road Map
Satellite
Terrain
Hybrid

Editor window - used to display returning Text.

Load Data File - opens requester to allow selection of data files.

Command String - Google request message.

How it works

By Magic - no not really. You send google a pile of instructions along with your data, and it returns a long string. The first part of the string is what you see in the Editor window, the second part is a string representing a PNG file,
All I do is get rid of the first bit of the string and save the second part as a bnary file called Google.png in the RAM: drive then load it back as brush 1.

There is quite a lot of info on the google website mentioned in the first post.

There are some limitations as to the size of the request you can send to google ( about 2000 charactors long ) also note if you start playing around with the code and you send a request to Google that is not quite right it can lockup and lock you out for a while ( about an hour or so if I remember correctly )

Anyway copy the code and give it a try.
Let me know what you think.

Cheers
Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
Post Reply