Pregunta

I am trying to add a search bar in my GWT web app that takes advantage of InputElement and AutoComplete. The search bar is basically to search locations on GoogleMap. Here is the code that I have made so far :

@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>

I know this is a not the correct way to do the job and hence I am getting the following error :

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)

Please help !

¿Fue útil?

Solución

I got it working. The main parts of the code :

1) In the .html file : (This is the part I had missed earlier)

<head>
.
.
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
</head>

2) in my java class : (Used 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);              
            }
        });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top