Comments (7)
You can find a demo project here: https://github.com/gwidgets/gwty-leaflet-starter-guide
Thanks.
from gwty-leaflet.
Ok, so here is my solution. It somehow relies on lambda type inference instead of type casting.
Event2 is just the jsinterop version of Event :
@JsType(isNative = true)
public class Event2
{
@JsProperty
public native String getType();
@JsProperty
public native HTMLElement getTarget();
}
Then for example :
public class MouseEvent extends Event2 {
@JsProperty
public final native LatLng getLatlng();
@JsProperty
public final native Point getLayerPoint();
@JsProperty
public final native Point getContainerPoint();
@JsProperty
public final native DOMMouseEvent getOriginalEvent();
}
I've also replaced the elemental Function with a more generic functional interface :
@JsFunction
@FunctionalInterface
public interface EventCallback<T extends Event2>{
public void call(T event);
}
Then you can use the whole thing with :
map.on(EventTypes.MapEvents.CLICK, (MouseEvent evt) -> {
if (firstClickFlag) {
PopupOptions popoptions = new PopupOptions.Builder()
.zoomAnimation(false)
.build();
StringBuilder sb = new StringBuilder();
sb.append("Hello,");
sb.append("event type = ").append(evt.getType());
sb.append("mouse event latlng = ").append(evt.getLatlng());
map.openPopup(sb.toString(), L.latLng(52.51, 13.40), popoptions);
firstClickFlag = false;
}
});
Let me know what you think
from gwty-leaflet.
@olivergg Thanks for your fix. This works.
For the lambda, the type should be either of type Event or casted explicitly inside the call, it will not work in this way. or maybe keeping the normal function form:
map.on(EventTypes.MapEvents.CLICK, new EventCallback<MouseEvent>() {
@Override
public void call(MouseEvent event) {
GWT.log("Mouse event "+event.getLayerPoint().x);
}
});
I am converting all events to jsinterop.
from gwty-leaflet.
Hi @olivergg,
Thanks for your remark. I wanted also the project to be 100% JsInterop, but I run into a cast exception when casting the Event class to it's subclasses using JsInterop. For example, when I wrote the Event class and its subclasses using JsInterop class I did something like:
map.on(EventTypes.Map.CLICK, new Function(){
@Overeride
public Event call(Event event) {
//Throws cast exception
MouseEvent msEvent = (MouseEvent) Event;
map.openPopup("hello", msEvent.getLatlng(), new PopupOptions.Builder().build());
return null;
}
});
So I had to change it to JSNI which works fine using the cast method:
map.on(EventTypes.Map.CLICK, new Function(){
@Override
public JavaScriptObject call(JavaScriptObject event) {
MouseEvent msEvent = event.cast();
map.openPopup("hello", msEvent.getLatlng(), new PopupOptions.Builder().build());
return null;
}
});
If you have any better idea please let me know.
from gwty-leaflet.
@zak905 thanks for your answer. I did not know about this technical limitation. I've found this https://docs.google.com/document/d/10fmlEYIHcyead_4R1S5wKGs1t2I7Fnp_PaNaa7XTEk0/edit#heading=h.kog1ji9nzq7y that might be relevant.
I'll try to find some time to think of a better way to do that. Is there some sample minimal project to try out gwty-leaflet (in order to test a solution) ?
from gwty-leaflet.
Thanks, I will give it a try asap.
from gwty-leaflet.
from gwty-leaflet.
Related Issues (15)
- Compile failure path for type 'com.gwidgets.api.client.GwtyLeaflet HOT 5
- Interop for some Leaflet plugins HOT 1
- Problem with TileLayerOptions. HOT 13
- LeafletResources path HOT 1
- How to get absolute position from map latLng coordinate?
- createTile in GridLayer HOT 2
- Native methods require a JavaScript implementation enclosed with /*-{ and }-*/ HOT 3
- Point is missing z (zoom) HOT 2
- How to use an htmlpanel to initialize a map HOT 6
- 1.1 geometry MultiLineString, LineString HOT 4
- Members of MapOptions are not visible HOT 4
- Issue creating map with HTMLPanel HOT 3
- Issue initializing tile layer HOT 2
- How to pass another TileLayerOptions implementation to L.tileLayer ? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gwty-leaflet.