B4J Question Changing the fill colour of a jGoogleMaps polygon

Ralph Parkhurst

Member
Licensed User
Longtime User
I'm trying to change the fill colour of an existing jGoogleMaps polygon, when it is clicked.

By studying the GmapsFX api documentation here I tried this code - but it failed to work:

B4X:
Private Sub GMap_PolygonClick (SelectedPolygon As MapPolygon)
    'change fill colour to green when the user clicks
    Private joFillColor = SelectedPolygon As JavaObject
    joFillColor = joFillColor.InitializeNewInstance("com.lynden.gmapsfx.shapes.Polygon", Null)
    joFillColor = joFillColor.RunMethod("setProperty", Array As Object("fillColor", Array As Object(fx.Colors.Green)))
End Sub

Where have I gone wrong?

Best regards,
Ralph
 

DonManfred

Expert
Licensed User
Longtime User
Do you get any error?

joFillColor = joFillColor.InitializeNewInstance("com.lynden.gmapsfx.shapes.Polygon", Null)
try to remove that line. You are creating a NEW Polygon here overwriting the existing.
InitializeNewInstance will create a new one with zero properties set and even this Polygon is not added to your GoogleMap too.
Setting a color to it will have no effect.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You probably have to create a new PolygonOptions object and set the Fillcolor here.
replace the PolygonOptions in the Polygon then. But there is not method to set a new PolygonOptions.

Guess you have to remember the PolygonOptions you use when filling the Map.

Change the right PolygonOptions for the Polygon clicked.
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
That sounds like a good suggestion. It might be a bit beyond my current understanding, but I will give it a shot and report back. And if nothing else I will learn along the way.
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Yes, there do seem to be some differences between B4A and J when it comes to implementation of Google Maps.

Here is the code I used to add the initial polygon:
B4X:
gmap.AddPolygon(lstPointsLatLng, LineWidthPolygon, LineColorPolygon, FillColorPolygon, OpacityPolygon)
where my variables are:
lstPointsLatLng is a list of LatLng coordinates,
LineWidthPolygon is the stroke width,
LineColorPolygon is the stroke colour,
FillColorPolygon is the Fill Color, and
OpacityPolygon is the Opacity.

One other way I thought of is to create two polygons, each with a different colour. Then alternate their visibility when the user clicks. I've tested that this works, however will require some more effort to manage the extra polygons.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Here is the code I used to add the initial polygon
i already thought so. I checked the Library code.

The PolygonOptions is used internally but you do not have access to it.
Starting at line 523

B4X:
/**
     * Adds a polygon to the map.
     *Points - A list or array of LatLng points.
     *StrokeWidth - Stroke width.
     *StrokeColor - Stroke color.
     *FillColor - Inner color.
     *Opacity - Inner color opacity. Value between 0 to 1.
     */
    public PolygonWrapper AddPolygon(List Points, float StrokeWidth, Paint StrokeColor, Paint FillColor, double Opacity)
    {
        PolygonOptions po = new PolygonOptions();
        po.strokeWeight(StrokeWidth);
        po.fillColor(htmlColor(FillColor));
        po.strokeColor(htmlColor(StrokeColor));
        po.fillOpacity(Opacity);
        boolean eventExist = ba.subExists(eventName + "_polygonclick");
        if (eventExist == false)
            po.clickable(false);
        po.paths(new MVCArray(Points.getObject().toArray()));
        Polygon gon = new Polygon(po);
        map.addMapShape(gon);
        if (eventExist) {
            map.addUIEventHandler(gon, UIEventType.click, new UIEventHandler() {
                @Override
                public void handle(JSObject obj) {
                    ba.raiseEventFromUI(GoogleMapWrapper.this, eventName + "_polygonclick",
                            (PolygonWrapper)AbsObjectWrapper.ConvertToWrapper(new PolygonWrapper(), gon));
                }
            });
        }
        return (PolygonWrapper)AbsObjectWrapper.ConvertToWrapper(new PolygonWrapper(), gon);

    }

I guess one have to change the library to add the possibility to access the PolygonOptions.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
options see:
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Thank you everyone. The method I am currently using is actually based on your idea Klaus - using Canvas to overlay a border in a different colour. But I thought if I could directly change the fill colour of the polygon all would be easier (and nicer!). I think I will pursue Plan-B - two polygons with different fill colours, and switch visibility. If this creates a workable and manageable solution I will revert back with my finding.

Warm regards,
Ralph
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
It's because I suggested the link above, it's because it has a lot of logic to work with polygons with canvas.

In the click event you can see the logic of how to handle polygons.

Regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…