我正在尝试在GWT网络应用中添加一个利用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 ::创建(LCOM /谷歌/ GWT / DOM /客户端/ InputElement;)([JavaScript的 object(30)]):$ wnd.google.maps.places未定义 在com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
请帮忙!
答案 0 :(得分:0)
我得到了它的工作。 代码的主要部分:
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);
}
});