Google Maps Android v2 - Polygons?

Disco

Member
Licensed User
Longtime User
I see that this lib does polylines but how would one go about doing polygons? I would like to be able to draw a polygon that I can set the stroke color, fill color and opacity.

Any help would be appreciated

Loving B4A!

TIA
:)
 

warwound

Expert
Licensed User
Longtime User
Hi there.,

I've already added a couple of extras features to the B4A Google Maps Android library, you can see them here: http://www.b4x.com/forum/additional...ogle-map-library-tile-overlay.html#post147343.
And i've now added support for Polygons..!

This sample code draws a 4 point polygon with a black border and semi-transparent red fill color:

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim MapFragment1 As MapFragment
    Dim GoogleMap1 As GoogleMap
    Dim MapPanel As Panel
   Dim Polygon1 As Polygon
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Google Play services not available.", True)
    Else
        MapFragment1.Initialize("MapFragment1", MapPanel)
    End If 
End Sub

Sub MapFragment1_Ready
    Log("MapFragment1_Ready")
    GoogleMap1 = MapFragment1.GetMap
    If GoogleMap1.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
      GoogleMap1.MapType=GoogleMap1.MAP_TYPE_NORMAL
      
      Dim GoogleMapsExtras1 As GoogleMapsExtras
      Dim PolygonOptions1 As PolygonOptions
      Dim Point1, Point2, Point3, Point4, Point5 As LatLng
      Dim Points As List
      
      Points.Initialize
      
      '   points must be ordered in a  counterclockwise order
      '   (i found FillColor will fail if not correctly ordered)
      
      Point1.Initialize(52.756, 0.400)
      Points.Add(Point1)
      Point2.Initialize(52.417, 0.753)
      Points.Add(Point2)
      Point3.Initialize(52.638, 1.291)
      Points.Add(Point3)
      Point4.Initialize(52.902, 0.617)
      Points.Add(Point4)
      Point5.Initialize(52.756, 0.400)
      Points.Add(Point5)   '   closes the path
      
      PolygonOptions1.Initialize
      PolygonOptions1.FillColor=Colors.ARGB(128, 255, 0, 0)
      PolygonOptions1.Points=Points
      PolygonOptions1.StrokeColor=Colors.Black
      PolygonOptions1.StrokeWidth=2
      
      Polygon1=GoogleMapsExtras1.AddPolygon(GoogleMap1, PolygonOptions1)
      
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(52.689, 0.770, 10)
        GoogleMap1.AnimateCamera(CameraPosition1)
    End If
End Sub

Sub RemoveButton_Click
   Polygon1.Remove
End Sub

Sub VisibilityButton_Click
   Polygon1.Visible=Not(Polygon1.Visible)
End Sub

I haven't yet added a method to get the Polygon points or a method to set Polygon holes, i can look at that when i have more time to spare.

Demo project and library files are attached.
If you can post some feedback and let me know if it works ok for you.

I shall add some comments/reference to the library and then 'properly' release it to the forum.

Martin.

GoogleMapsExtras library files can now be found in this thread: http://www.b4x.com/forum/additional...pdates/26277-googlemapsextras.html#post152004
 
Last edited:
Upvote 0

Disco

Member
Licensed User
Longtime User
Thank-you so much Martin, this is fantastic! :)

I added your code to my app and it worked like a charm. I can't wait for you to polish it up with the extra methods/events etc. :icon_clap:

I'm using a Nexus 7 with the latest version of Jellybean (in case you were wondering).

D
 
Upvote 0
Top