Results 1 to 8 of 8

Thread: Static Google Map Class(es)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    40

    Static Google Map Class(es)

    This has been developed with framework 3.5 in Visual Studio 2008, however it's easily transferrable to 2.0 / 3.0 / 4.0.

    The following code will allow you to generate static google maps as either a string or as an image. The code utilizes version 2 of the static maps API (http://code.google.com/apis/maps/doc...on/staticmaps/).

    As part of several projects I've been completing simply building the URL's each time became monotonous as I'd have to keep going back to the google reference so I've created a library to do it for me.

    Class 1 - Static Map Class

    Code:
    Public Class StaticGoogleMap
    
        Public Const GOOGLE_STATIC_MAP_URL As String = "http://maps.googleapis.com/maps/api/staticmap?"
    
        Private _Markers As New MarkerList
    
        Public Property Markers() As MarkerList
            Get
                Return _Markers
            End Get
            Set(ByVal value As MarkerList)
                _Markers = value
            End Set
        End Property
    
        Public Function GenerateMarkers() As String
            ' If we have markers, process.
            If _Markers.Count > 0 Then
                Dim strMarkerText As String = ""
                For Each objMarker As Marker In _Markers
                    strMarkerText += "&" + objMarker.GenerateMarkerURLText
                Next
                Return strMarkerText
            Else
                ' If there are no markers added return an empty string.
                Return ""
            End If
        End Function
    
        Private _Sensor As Boolean = False
        Public Property Sensor() As Boolean
            Get
                Return _Sensor
            End Get
            Set(ByVal value As Boolean)
                _Sensor = value
            End Set
        End Property
    
        Private _LatLng As String = Nothing
        Public Property LatLng() As String
            Get
                Return _LatLng
            End Get
            Set(ByVal value As String)
                _LatLng = value
            End Set
        End Property
    
        Public Function SetUKPosition() As Boolean
            Try
                SetLatLng("53.7617", "-3.054")
                Zoom = 6
                ImageWidth = 640
                ImageHeight = 640
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function
    
        Public Sub SetLatLng(ByVal gLat As String, ByVal gLng As String)
            _LatLng = gLat + "," + gLng
        End Sub
    
        Private _Address As String = Nothing
        Public Property Address() As String
            Get
                Return _Address
            End Get
            Set(ByVal value As String)
                _Address = value
            End Set
        End Property
    
        Private _Zoom As Integer = 6
        Public Property Zoom() As Integer
            Get
                Return _Zoom
            End Get
            Set(ByVal value As Integer)
                _Zoom = value
            End Set
        End Property
    
        Private _ImageHeight As Integer = 320
        Private _ImageWidth As Integer = 320
    
        Public Property ImageHeight() As Integer
            Get
                Return _ImageHeight
            End Get
            Set(ByVal value As Integer)
                _ImageHeight = value
            End Set
        End Property
    
        Public Property ImageWidth() As Integer
            Get
                Return _ImageWidth
            End Get
            Set(ByVal value As Integer)
                _ImageWidth = value
            End Set
        End Property
    
        Public Enum ImageFormats
            PNG8 = 0
            PNG32 = 1
            GIF = 2
            JPG = 3
            JPG_BASELINE = 4
        End Enum
    
        Private _ImageFormat As ImageFormats = ImageFormats.PNG8
        Public Property ImageFormat() As ImageFormats
            Get
                Return _ImageFormat
            End Get
            Set(ByVal value As ImageFormats)
                _ImageFormat = value
            End Set
        End Property
    
        Public Enum MapTypes
            ROADMAP = 0
            SATELLITE = 1
            TERRAIN = 2
            HYBRID = 3
        End Enum
    
        Private _MapType As MapTypes = MapTypes.ROADMAP
        ''' <summary>
        ''' Specifies the type of map you would like to return. Default is road map.
        ''' </summary>
        ''' <value>MapType</value>
        ''' <returns>MapType</returns>
        ''' <remarks></remarks>
        Public Property MapType() As MapTypes
            Get
                Return _MapType
            End Get
            Set(ByVal value As MapTypes)
                _MapType = value
            End Set
        End Property
    
        ''' <summary>
        ''' Generates a static map URL
        ''' </summary>
        ''' <returns>String</returns>
        ''' <remarks></remarks>
        Public Function GenerateMap() As String
            ' Create a parameters string
            Dim strParams As String = ""
            ' Calculate whether we need a centre specifying.
            If Not _LatLng Is Nothing Or Not _Address Is Nothing Then
                strParams += IIf(strParams.Trim <> "", "&", "") + "center=" + IIf(_LatLng Is Nothing, _Address, _LatLng)
            End If
            ' Calculate the zoom
            If Not _Zoom = Nothing Then
                strParams += IIf(strParams.Trim <> "", "&", "") + "zoom=" + _Zoom.ToString
            End If
            ' Set the sensor
            strParams += IIf(strParams.Trim <> "", "&", "") + "sensor=" + _Sensor.ToString.ToLower
            ' Set the height / width of the image
            strParams += IIf(strParams.Trim <> "", "&", "") + "size=" + _ImageWidth.ToString + "x" + _ImageHeight.ToString
            ' Set the image type
            Select Case _ImageFormat
                Case ImageFormats.GIF
                    strParams += IIf(strParams.Trim <> "", "&", "") + "format=gif"
                Case ImageFormats.JPG
                    strParams += IIf(strParams.Trim <> "", "&", "") + "format=jpg"
                Case ImageFormats.JPG_BASELINE
                    strParams += IIf(strParams.Trim <> "", "&", "") + "format=jpg-baseline"
                Case ImageFormats.PNG32
                    strParams += IIf(strParams.Trim <> "", "&", "") + "format=png32"
            End Select
            ' Set the map type
            Select Case _MapType
                Case MapTypes.HYBRID
                    strParams += IIf(strParams.Trim <> "", "&", "") + "maptype=hybrid"
                Case MapTypes.SATELLITE
                    strParams += IIf(strParams.Trim <> "", "&", "") + "maptype=satellite"
                Case MapTypes.TERRAIN
                    strParams += IIf(strParams.Trim <> "", "&", "") + "maptype=terrain"
            End Select
    
            Return GOOGLE_STATIC_MAP_URL + strParams + GenerateMarkers()
    
        End Function
    
        Public Function GenerateMapAsImage() As Image
            Dim strMapURL As String = GenerateMap()
            Dim objImg As Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(strMapURL)))
            Return objImg
        End Function
    
    End Class
    Class 2 - MarkerList Class

    Code:
    Public Class MarkerList
        Inherits List(Of Marker)
    End Class
    Last edited by bootcom; Aug 1st, 2011 at 06:40 AM. Reason: Including the version information

  2. #2

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: Static Google Map Class(es)

    Class 3 - Marker

    Code:
    Public Class Marker
    
        Public Enum MarkerSize
            TINY = 0
            SMALL = 1
            MID = 2
            NORMAL = 3
        End Enum
    
        Public Enum MarkerType
            [DEFAULT] = 0
            CUSTOM = 1
        End Enum
    
        Public Enum MarkerColour
            CUSTOM = 0
            BLACK = 1
            BROWN = 2
            GREEN = 3
            PURPLE = 4
            YELLOW = 5
            BLUE = 6
            GRAY = 7
            ORANGE = 8
            RED = 9
            WHITE = 10
            NONE = 11
        End Enum
    
        Public Enum MarkerLabel
            A = 1
            B = 2
            C = 3
            D = 4
            E = 5
            F = 6
            G = 7
            H = 8
            I = 9
            J = 10
            K = 11
            L = 12
            M = 13
            N = 14
            O = 15
            P = 16
            Q = 17
            R = 18
            S = 19
            T = 20
            U = 21
            V = 22
            W = 23
            X = 24
            Y = 25
            Z = 26
            NUM_0 = 27
            NUM_1 = 28
            NUM_2 = 29
            NUM_3 = 30
            NUM_4 = 31
            NUM_5 = 32
            NUM_6 = 33
            NUM_7 = 34
            NUM_8 = 35
            NUM_9 = 36
        End Enum
    
        Private _Size As MarkerSize = MarkerSize.NORMAL
        Public Property Size() As MarkerSize
            Get
                Return _Size
            End Get
            Set(ByVal value As MarkerSize)
                _Size = value
            End Set
        End Property
    
        Private _Colour As MarkerColour = MarkerColour.BLUE
        Public Property Colour() As MarkerColour
            Get
                Return _Colour
            End Get
            Set(ByVal value As MarkerColour)
                _Colour = value
            End Set
        End Property
    
        Private _CustomColor As String
        ''' <summary>
        ''' 24 bit colour (example: color=0xFFFFCC)
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property CustomColor() As String
            Get
                Return _CustomColor
            End Get
            Set(ByVal value As String)
                _CustomColor = value
            End Set
        End Property
    
        Private _Label As MarkerLabel = Nothing
        Public Property Label() As MarkerLabel
            Get
                Return _Label
            End Get
            Set(ByVal value As MarkerLabel)
                _Label = value
            End Set
        End Property
    
        Private _MarkerURL As String = ""
        Public Property MarkerURL() As String
            Get
                Return _MarkerURL
            End Get
            Set(ByVal value As String)
                _MarkerURL = value
            End Set
        End Property
    
        Private _MarkerShadow As Boolean = True
        Public Property MarkerShadow() As Boolean
            Get
                Return _MarkerShadow
            End Get
            Set(ByVal value As Boolean)
                _MarkerShadow = value
            End Set
        End Property
    
        Private _Type As MarkerType = MarkerType.DEFAULT
        Public Property Type() As MarkerType
            Get
                Return _Type
            End Get
            Set(ByVal value As MarkerType)
                _Type = value
            End Set
        End Property
    
        Private _MarkerLocation As String
        Public Property MarkerLocation() As String
            Get
                Return _MarkerLocation
            End Get
            Set(ByVal value As String)
                _MarkerLocation = value
            End Set
        End Property
    
        Private Function GetMarkerImageType() As String
            ' Locate the type of icon we're trying to generate first.
            Select Case _Type
                Case MarkerType.CUSTOM
                    Return "icon:" + EncodeCustomURL(MarkerURL)
                Case Else
                    Return ""
            End Select
    
        End Function
    
        Private Function GetMarkerColor() As String
            Select Case Colour
                Case MarkerColour.BLACK
                    Return "color:black"
                Case MarkerColour.BLUE
                    Return "color:blue"
                Case MarkerColour.BROWN
                    Return "color:brown"
                Case MarkerColour.CUSTOM
                    Return "color:" + CustomColor
                Case MarkerColour.GRAY
                    Return "color:gray"
                Case MarkerColour.GREEN
                    Return "color:green"
                Case MarkerColour.NONE
                    Return ""
                Case MarkerColour.ORANGE
                    Return "color:orange"
                Case MarkerColour.PURPLE
                    Return "color:purple"
                Case MarkerColour.RED
                    Return "color:red"
                Case MarkerColour.WHITE
                    Return "color:white"
                Case MarkerColour.YELLOW
                    Return "color:yellow"
            End Select
        End Function
    
        Private Function GetMarkerSize() As String
            Select Case Size
                Case MarkerSize.TINY
                    Return "size:tiny"
                Case MarkerSize.SMALL
                    Return "size:small"
                Case MarkerSize.MID
                    Return "size:mid"
                Case Else
                    Return ""
            End Select
        End Function
    
        Private Function GetMarkerLabel() As String
            Select Case Label
                Case MarkerLabel.A
                    Return "A"
                Case MarkerLabel.B
                    Return "B"
                Case MarkerLabel.C
                    Return "C"
                Case MarkerLabel.D
                    Return "D"
                Case MarkerLabel.E
                    Return "E"
                Case MarkerLabel.F
                    Return "F"
                Case MarkerLabel.G
                    Return "G"
                Case MarkerLabel.H
                    Return "H"
                Case MarkerLabel.I
                    Return "I"
                Case MarkerLabel.J
                    Return "J"
                Case MarkerLabel.K
                    Return "K"
                Case MarkerLabel.L
                    Return "L"
                Case MarkerLabel.M
                    Return "M"
                Case MarkerLabel.N
                    Return "N"
                Case MarkerLabel.O
                    Return "O"
                Case MarkerLabel.P
                    Return "P"
                Case MarkerLabel.Q
                    Return "Q"
                Case MarkerLabel.R
                    Return "R"
                Case MarkerLabel.S
                    Return "S"
                Case MarkerLabel.T
                    Return "T"
                Case MarkerLabel.U
                    Return "U"
                Case MarkerLabel.V
                    Return "V"
                Case MarkerLabel.W
                    Return "W"
                Case MarkerLabel.X
                    Return "X"
                Case MarkerLabel.Y
                    Return "Y"
                Case MarkerLabel.Z
                    Return "Z"
                Case MarkerLabel.NUM_0
                    Return "0"
                Case MarkerLabel.NUM_1
                    Return "1"
                Case MarkerLabel.NUM_2
                    Return "2"
                Case MarkerLabel.NUM_3
                    Return "3"
                Case MarkerLabel.NUM_4
                    Return "4"
                Case MarkerLabel.NUM_5
                    Return "5"
                Case MarkerLabel.NUM_6
                    Return "6"
                Case MarkerLabel.NUM_7
                    Return "7"
                Case MarkerLabel.NUM_8
                    Return "8"
                Case MarkerLabel.NUM_9
                    Return "9"
                Case Else
                    Return ""
            End Select
    
        End Function
    
        Private Function EncodeCustomURL(ByVal strURL As String) As String
            Dim strTmpURL As String = strURL
            strTmpURL = Replace(strTmpURL, "|", "%257C", 1, -1, 1)
            strTmpURL = Replace(strTmpURL, "&", "%26", 1, -1, 1)
            Return strTmpURL
        End Function
    
        Public Function GenerateMarkerURLText() As String
            Dim strMarkerURLText As String = "markers="
            strMarkerURLText += IIf(GetMarkerSize() <> "", IIf(strMarkerURLText <> "markers=", "|", "") + GetMarkerSize(), "")
            strMarkerURLText += IIf(GetMarkerImageType() <> "", IIf(strMarkerURLText <> "markers=", "|", "") + GetMarkerImageType(), "")
            strMarkerURLText += IIf(GetMarkerLabel() <> "", IIf(strMarkerURLText <> "markers=", "|", "") + "label:" + GetMarkerLabel(), "")
            strMarkerURLText += IIf(MarkerShadow = False, IIf(strMarkerURLText <> "markers=", "|", "") + "shadow:false", "")
            strMarkerURLText += IIf(GetMarkerColor() <> "", IIf(strMarkerURLText <> "markers=", "|", "") + GetMarkerColor(), "")
            strMarkerURLText += IIf(Label <> Nothing, IIf(strMarkerURLText <> "markers=", "|", "") + "", "")
            strMarkerURLText += IIf(strMarkerURLText <> "markers=", "|", "") + MarkerLocation
    
            Return Replace(strMarkerURLText, "|", "%7C", 1, -1, 1)
        End Function
    
    End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: Static Google Map Class(es)

    Example Usage:

    Code:
    Private objMap As New StaticGoogleMap
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            objMap.SetUKPosition()
            objMap.Sensor = False
            objMap.ImageFormat = ImageFormats.PNG32
            objMap.MapType = MapTypes.SATELLITE
            
            PictureBox1.Image = objMap.GenerateMapAsImage
        
    End Sub
    Would show:


  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    40

    Including Markers

    Code:
        Private objMap As New StaticGoogleMap
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            objMap.SetUKPosition()
            objMap.Sensor = False
            objMap.ImageFormat = ImageFormats.PNG32
            objMap.MapType = MapTypes.SATELLITE
    
            Dim objMarker As New Data.Marker
            objMarker.MarkerLocation = "London,UK"
            objMarker.MarkerShadow = False
            objMarker.Size = Marker.MarkerSize.MID
            objMarker.Colour = Marker.MarkerColour.ORANGE
            objMap.Markers.Add(objMarker)
    
            objMarker = New Marker
            objMarker.MarkerLocation = "Morley,Leeds,UK"
            objMarker.MarkerShadow = False
            objMarker.Size = Marker.MarkerSize.NORMAL
            objMarker.Label = Marker.MarkerLabel.I
            objMarker.Colour = Marker.MarkerColour.ORANGE
            objMap.Markers.Add(objMarker)
    
            PictureBox1.Image = objMap.GenerateMapAsImage
        End Sub
    Displays:


  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Static Google Map Class(es)

    Cool! Although, just to be safe could you please include some details such as which version of vb.net you are using in the first post.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: Static Google Map Class(es)

    Cheers Nightwalker! (Updated now)

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    Re: Including Markers

    thank you sir for this wonderful code, i am using it to my project and have tweaked it a little to suit my needs though there's a problem i am facing right now

    about the marker label, as i understood it, you declare a class of marker label options (A-Z, 0-9).
    what i want to happen is make the marker label = to the primary ID of my coordinates from the database, so i will know will point is which.
    this part is critical to my project so i hope you could help me.
    i tried to add my own marker like ID=oID.ToString where oID is a variable where i store the primaryID of the coordinates when i call it.
    but its not working.
    im still a newbie when it comes to creating your own classes and something like that
    i hope you could help me.
    thank you very much

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: Static Google Map Class(es)

    Hi,

    The reason I did it the way I did was because there were only a certain amount of markers which were available by default from Google as I recall (A to Z and 1 to 9) - checking their website thats still the defaults.

    If you take a look at the following link: https://developers.google.com/maps/d...ro#CustomIcons this gives some pointers as to how to utilise custom markers into the static map.

    I would suggest you have or create some markers and use these using the mechanism linked to and update the GenerateMarkerURLText function.

    Hope thats a good start point for you but it's a very long time since I wrote the code and wouldn't write it the same way again!

    Chris

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width