dlsc-software-consulting-gmbh / GMapsFX

Java API for using Google Maps within a JavaFX application.
http://rterp.github.io/GMapsFX/
Apache License 2.0
319 stars 119 forks source link

ReferenceError: Can't find variable: LatLong<number> when I try to create new LatLong #180

Open AdrianStaskiewicz opened 5 years ago

AdrianStaskiewicz commented 5 years ago

Hello, I'm trying to use a method addMapShape(), but when I'm adding a new point to my array (on mouse click event) and run MVCArray pmvc = new MVCArray(points.toArray()); I get: ReferenceError: Can't find variable: LatLong This issue shows only when I'm catching new point on this mouse event. When I'm predefining some points on method initializeMap() and create new MVCArray with this points everything is OK, all methods work correctly and the new polygon shows.

Additionally the number new LatLong object get a random number. Often starts from 13 or something like this, and increase by random value, fro example 13, 15, 17, 22, 27.... And then the error occurs with the number of first element of array. F.E. ReferenceError: Can't find variable: LatLong13

GeoffCapper commented 5 years ago

Hi @AdrianStaskiewicz ,

My guess is you're not converting to an proper LatLong in the mouse event. Without seeing code I can't be sure. It needs to be done like this:

map.addUIEventHandler(UIEventType.click, (JSObject obj) -> {
    LatLong myLatLong = new LatLong((JSObject) obj.getMember("latLng"));
    ... add that to your raw array....
});

LatLong[] ary = new LatLong[]{myLatLong, myLatLong2};
MVCArray mvc = new MVCArray(ary);

etc.

Hope that helps, if not, post some code, the mouse event at least.

Cheers, Geoff

AdrianStaskiewicz commented 5 years ago

Hi @GeoffCapper Thanks for your answer! I'm not sure what do You mean, so I'm pasting my source code. This is a whole mouse event implementation.

        map.addMouseEventHandler(UIEventType.click, (GMapMouseEvent event) -> {
            LatLong latLong = event.getLatLong();
            points.add(latLong);

            MVCArray pmvcTest = new MVCArray();
            if (points.size() > 2) {
                try {
                    pmvcTest = new MVCArray(points.toArray());
                } catch (Exception e) {
                    System.out.println(e);
                }
                polygOpts = new PolygonOptions()
                        .paths(pmvcTest)
                        .strokeColor("blue")
                        .strokeWeight(2)
                        .editable(false)
                        .fillColor("lightBlue")
                        .fillOpacity(0.5);

                pg = new Polygon(polygOpts);

                map.addUIEventHandler(pg, UIEventType.click, (JSObject obj) -> {
                    pg.setEditable(!pg.getEditable());
                });
                map.addMapShape(pg);
            }
        });

And of course variable points is declared as ArrayList

The issue occurs at this line: pmvcTest = new MVCArray(points.toArray());

What I have to change on my code?

GeoffCapper commented 5 years ago

Sorry, I'd forgotten about the addMouseEventHandler method. In that case I suspect there is a problem with the delegates converting the original javascript event over to the java event.

Try using the addUIEventHandler method instead of the addMouseEventHandler, and see if that makes a difference. The top of your code would look like:

map.addUIEventHandler(UIEventType.click, (JSObject obj) -> {
    LatLong latLong = new LatLong((JSObject) obj.getMember("latLng"));
    points.add(latLong);
AdrianStaskiewicz commented 5 years ago

@GeoffCapper It still doesn't work.. This same issue.. Now in my array exists LatLong10, but I'm getting

Can't find variable: LatLong10

I supose that the problem is at line: this.runtime.execute("var " + this.variableName + " = " + this.runtime.getArrayConstructor(type, ary));

It is in JavaScriptObject.class:48

Morstern commented 5 years ago

@GeoffCapper Hey, I encountered same problem in my case. So creating "static" fields work. But whenever I want to add them in real-time (after user clicks) - it crashes: same error as above. Any ideas how to fix it?

Best Regards, Morstern