我正在尝试在我的 GWT Web 应用程序中添加一个利用 InputElement 和 AutoComplete 的搜索栏。搜索栏基本上是在 GoogleMap 上搜索位置。 这是我到目前为止所做的代码:
@UiField
InputElement input;
//
//
//
final Autocomplete autocomplete = Autocomplete.create(input);
final InfoWindow infowindow= InfoWindow.create();
autocomplete.addPlaceChangedListener(new PlaceChangedHandler(){
public void handle(){
PlaceResult place=autocomplete.getPlace();
String address=place.getAddressComponents().get(0).getShortName();
infowindow.setContent(place.getName()+", "+address);
addMarker(place.getGeometry().getLocation(),place,infowindow);
map.setCenter(place.getGeometry().getLocation());
map.setZoom(17.0);
}
});
//
//
//
<g:north size='5'>
<g:HTMLPanel>
<div>
<g:Label ui:field="label1">PublicFortress</g:Label>
</div>
<div>
<g:Anchor ui:field="signin" href="#">SignIn</g:Anchor>
<g:Button ui:field="home">Home</g:Button>
<div>
<input type="text" name="Search" ui:field="input" class="custom" />
</div>
</div>
</g:HTMLPanel>
</g:north>
我知道这不是完成这项工作的正确方法,因此我收到以下错误:
com.google.gwt.core.client.JavaScriptException: (TypeError) @com.google.maps.gwt.client.places.Autocomplete::create(Lcom/google/gwt/dom/client/InputElement;)([JavaScript object(30)]): $wnd.google.maps.places is undefined at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
请帮忙!
最佳答案
我成功了。 代码主要部分:
1)在.html文件中:(这是我之前错过的部分)
<head>
.
.
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
</head>
2)在我的java类中:(使用g:TextBox)
final AutocompleteOptions options = AutocompleteOptions.newInstance();
final Autocomplete autocomplete = Autocomplete.newInstance(input.getElement(), options);
final InfoWindow infowindow= InfoWindow.create();
autocomplete.addPlaceChangeHandler(new PlaceChangeMapHandler(){
@Override
public void onEvent(PlaceChangeMapEvent event) {
PlaceResult place=autocomplete.getPlace();
String address=place.getAddress_Components().get(0).getShort_Name();
infowindow.setContent(place.getName()+", "+address);
LatLng latLng = LatLng.create(place.getGeometry().getLocation().getLatitude(), place.getGeometry().getLocation().getLongitude());
addMarker(latLng,place,infowindow);
map.setCenter(latLng);
map.setZoom(17.0);
}
});
关于java - GWT:UiBinder + 输入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19604504/