1.是否可以将gwt中的本机方法调用为其他本机方法?
这是我从VectorSource.java调用Map.java的方法https://github.com/VOL3/v-ol3/blob/master/gwt-ol3/src/main/java/org/vaadin/gwtol3/client/source/VectorSource.java
public final native JsArray<Feature> getFeatures()/*-{
return this.getFeatures();
}-*/;
我在Map.java类中创建了一个本地方法,并获得了Features,我想将这些特征值返回给addOnPostRenderListener下面的方法是Map.java类别中的更改
public native final Feature getFeatures(VectorSource sourceFeature)/*-{
var features=sourceFeature.@org.vaadin.gwtol3.client.source.VectorSource::getFeatures();
return features;
}-*/;
public native final void addOnPostRenderListener(OnPostRenderListener listener)/*-{
if(!this.__registered){
var that=this;
that.once('postrender',function(){
var feature=that.@org.vaadin.gwtol3.client.Map::getFeatures(vectorSource);
if(feature!=null){
var coordinate=feature.getGeometry().getCoordinate();
if(coordinates!=null){
var MapEvent={Pixel:that.getPixelFromCoordinate(that.__transformInputCoordinate(coordinates))};
that.__notifyPostRenderListeners(MapEvent);
}
}})
this.__postRenderListeners.push(listener);
}
}-*/;
剩余的代码与下面的链接中所示的保持不变https://github.com/VOL3/v-ol3/blob/master/gwt-ol3/src/main/java/org/vaadin/gwtol3/client/Map.java
在下面的代码中,我得到了错误,因为在JSNI方法引用中需要一个有效的参数类型签名这些代码行在addOnPostRenderListener方法中
var feature=that.@org.vaadin.gwtol3.client.Map::getFeatures(vectorSource);
我的目标是将VectorSource.java类中的方法getFeatures((调用到Map.java类,并将值发送到另一个本地方法addOnPostRenderListener方法。
接口
public interface OnPostRenderListener {
public void onRender(MapEvent posEvent);
}
MapEvent
public class MapEvent extends JavaScriptObject {
protected MapEvent() {
}
public static final native Feature create()/*-{
return new $wnd.ol.Feature();
}-*/;
public native final Geometry getGeometry()-{
return this.getGeometry();
}-;*/
public native final Geometry getGeometry()/*-{
return this.geometry;
}-*/;
public native final Coordinate getCoordinate()/*-{
return this.coordinate;
}-*/;
public native final Pixel getPixel()/*-{
return this.Pixel;
}-*/;
//written code not used
public native final Map getPixelFromCoordinate(Coordinate coord)/*-{
return this.getPixelFromCoordinate(coord);
}-*/;
}
您也需要传递VectorSource sourceFeature
参数。您在that.@org.vaadin.gwtol3.client.Map::getFeatures(Lcom/google/gwt/core/client/Source;);
中缺少它。
一种方法是将其添加到中
addOnPostRenderListener(OnPostRenderListener listener)
例如addOnPostRenderListener(OnPostRenderListener listener, VectorSource vectorSource)
然后像这样访问它:
var feature=that.@org.vaadin.gwtol3.client.Map::getFeatures(vectorSource);
尽管我建议完全放弃JSNI,并使用更好的JsInterop。还有一个使用JsInterop的OL实现。查看此处