package welcome.client.ui.maps;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import welcome.client.ui.Content;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
public class MapsForm extends Content {
private VerticalPanel verticalPanel = new VerticalPanel();
private Label titre = new Label("Map");
private FlowPanel mapPanel;
private final CaptionPanel searchPanel = new CaptionPanel("New panel");
private final TextBox txtbxCity = new TextBox();
private final Grid grid = new Grid(1, 3);
private final Button searchBtn = new Button("Search");
private MapWidget map;
private LatLng result ;
private final InlineLabel nlnlblVille = new InlineLabel("Ville");
private final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
private final HorizontalPanel horizontalPanel = new HorizontalPanel();
public MapsForm(){
titre.setStyleName("mapLabel");
verticalPanel.add(titre);
verticalPanel.setSize("100%", "600px");
Maps.loadMapsApi("", "2", false, new Runnable() {
public void run() {
createMap("Mulhouse");
}
});
initWidget(verticalPanel);
verticalPanel.add(horizontalPanel);
horizontalPanel.setSize("100%", "20px");
verticalPanel.add(horizontalPanel_1);
horizontalPanel_1.setSize("100%", "100%");
horizontalPanel_1.add(searchPanel);
searchPanel.setCaptionHTML("Search");
searchPanel.setSize("200px", "100%");
searchPanel.setContentWidget(grid);
grid.setSize("100%", "100%");
txtbxCity.addKeyUpHandler(new KeyUpHandler() {
public void onKeyUp(KeyUpEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
changeCenter(txtbxCity.getText());
}
}
});
grid.setWidget(0, 0, nlnlblVille);
nlnlblVille.setSize("28px", "31px");
txtbxCity.setName("City");
grid.setWidget(0, 1, txtbxCity);
txtbxCity.setWidth("90px");
txtbxCity.setText("City");
searchBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
changeCenter(txtbxCity.getText());
}
});
grid.setWidget(0, 2, searchBtn);
grid.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
grid.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
grid.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
this.mapPanel = new FlowPanel();
horizontalPanel_1.add(this.mapPanel);
this.mapPanel.setSize("100%", "100%");
}
public void createMap(String location){
Geocoder geoCoder = new Geocoder();
geoCoder.getLatLng(location, new LatLngCallback() {
@Override
public void onSuccess(LatLng point) {
initMap(point);
}
@Override
public void onFailure() {
System.out.println("bl�me");
}
});
}
private void initMap(LatLng point) {
// TODO Auto-generated method stub
map = new MapWidget(point,15 );
map.setSize("700px", "600px");
map.addControl(new LargeMapControl());
// final DockLayoutPanel dock = new DockLayoutPanel(Unit.PX);
// dock.addNorth(map, 500);
mapPanel.add(map);
}
public void changeCenter(String location){
Geocoder geoCoder = new Geocoder();
geoCoder.getLatLng(location, new LatLngCallback() {
@Override
public void onSuccess(LatLng point) {
result =LatLng.newInstance(point.getLatitude(), point.getLongitude()) ;
map.setCenter(point);
}
@Override
public void onFailure() {
System.out.println("bl�me");
}
});
}
}