(そのタイトルだけで、人々は木工品から出てきて、クラブで私を打ちのめすはずですが、私を聞いてください)。
非同期呼び出しから値を返す必要があるユースケースがあります。(私はGWT-Platformを使用していますが、概念は同じです。)最後のJavaScriptObject配列を宣言し、AsyncCallback内で値を割り当てました。ただし、値を返す必要があり、AsyncCallbackが完了する前にメソッドが戻ります。したがって、AsyncCallbackが完了するまで何らかの方法でブロックする必要があります。別のメソッドで戻り値が必要です。または、onSuccess()で必要なことを実行します。
ループ、タイマー、その他いくつかの方法を試しましたが、うまくいきませんでした。誰か助けてもらえますか?
@Override
public JavaScriptObject doGetWhereAmIMarker(final double lng, final double lat) {
final JavaScriptObject[] markerArray = new JavaScriptObject[1]; // ugly hack, I know
dispatch.execute(new GetLocationDescriptionsAction(lng, lat), new AsyncCallback<GetLocationDescriptionsResult>() {
@Override
public void onFailure(Throwable caught) {
caught.printStackTrace();
}
@Override
public void onSuccess(GetLocationDescriptionsResult result) {
Map<String, Location> resultMap = result.getResult();
StringBuffer message = new StringBuffer();
for (String key : resultMap.keySet()) {
message.append(key).append(": ").append(resultMap.get(key)).append("\n");
}
Map tempMap = new HashMap();
tempMap.put("TITLE","Location Information");
tempMap.put("LAT", lat);
tempMap.put("LNG", lng);
tempMap.put("CONTENTS", message.toString());
JavaScriptObject marker = GoogleMapUtil.createMarker(tempMap);
markerArray[0] = marker;
if (markerArray[0] != null) {
GWT.log("Marker Array Updated");
}
}
});
return markerArray[0];
}
更新:要求に応じて、doGetWhereIAmMarker()を呼び出すコードを次に示します。Google Mapオブジェクト(JavaScriptObjectとして)をパラメーターとして使用して別のネイティブメソッドを使用しようとしましたが、ネイティブメソッド間でそのオブジェクトを渡すと、そのオブジェクトを更新する機能が失われるようです。
public native void initMap(JavaScriptObject mapOptions, JavaScriptObject bounds, JavaScriptObject border, JsArray markerArray, Element e) /*-{
// create the map and fit it within the given bounds
map = new $wnd.google.maps.Map(e, mapOptions);
if (bounds != null) {
map.fitBounds(bounds);
}
// set the polygon for the borders
if (border != null) {
border.setMap(map);
}
// set up the info windows
if (markerArray != null && markerArray.length > 0) {
var infoWindow = new $wnd.google.maps.InfoWindow({
content:"InfoWindow Content Goes Here"
});
for (var i = 0; i < markerArray.length; i++) {
var marker = markerArray[i];
marker.setMap(map);
$wnd.google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(marker.content);
infoWindow.open(map, this);
});
}
}
// need to reference the calling class inside the function(), so set a reference to "this"
var that = this;
$wnd.whereAmI=function(lng, lat) {
that.@org.jason.mapmaker.client.view.MapmakerMapViewImpl::whereAmI(DD)(lng,lat);
}
$wnd.google.maps.event.addListener(map, 'click', function(event) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
$wnd.whereAmI(lng, lat);
});
}-*/;