Add a google maps component to the GeoLocation generator.

git-svn-id: https://zxing.googlecode.com/svn/trunk@797 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
leppoc 2008-12-19 21:02:36 +00:00
parent f50dcf6d27
commit c303242f8a
5 changed files with 97 additions and 4 deletions

View file

@ -6,5 +6,6 @@
<classpathentry kind="lib" path="gwt.jar"/> <classpathentry kind="lib" path="gwt.jar"/>
<classpathentry kind="var" path="JUNIT_HOME/junit.jar"/> <classpathentry kind="var" path="JUNIT_HOME/junit.jar"/>
<classpathentry kind="lib" path="libs/gwt-incubator.jar"/> <classpathentry kind="lib" path="libs/gwt-incubator.jar"/>
<classpathentry kind="lib" path="libs/gwt-maps.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

Binary file not shown.

View file

@ -6,13 +6,16 @@
<inherits name='com.google.gwt.user.theme.standard.Standard'/> <inherits name='com.google.gwt.user.theme.standard.Standard'/>
<!-- Other module inherits --> <!-- Other module inherits -->
<inherits name="com.google.gwt.widgetideas.WidgetIdeas" /> <inherits name='com.google.gwt.widgetideas.WidgetIdeas' />
<inherits name='com.google.gwt.libideas.LibIdeas' /> <inherits name='com.google.gwt.libideas.LibIdeas' />
<inherits name='com.google.gwt.maps.GoogleMaps' />
<!-- Specify the app entry point class. --> <!-- Specify the app entry point class. -->
<entry-point class='com.google.zxing.web.generator.client.Generator'/> <entry-point class='com.google.zxing.web.generator.client.Generator'/>
<!-- Specify the application specific style sheet. --> <!-- Specify the application specific style sheet. -->
<stylesheet src='Generator.css' /> <stylesheet src='Generator.css' />
<stylesheet src="DatePickerDemo.css" /> <stylesheet src='DatePickerDemo.css' />
<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAjEOGGB_IEuss4QYUzB6Z0BRlY4xXjGBjgubjQXCS8Djm2jdpYBQn9f8dHtxpg30ararqd3GCyq_T4A' />
</module> </module>

View file

@ -16,11 +16,22 @@
package com.google.zxing.web.generator.client; package com.google.zxing.web.generator.client;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.http.client.URL; import com.google.gwt.http.client.URL;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.event.MarkerDragEndHandler;
import com.google.gwt.maps.client.event.MapClickHandler.MapClickEvent;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener; import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget; import com.google.gwt.user.client.ui.Widget;
@ -30,7 +41,7 @@ import com.google.gwt.user.client.ui.Widget;
* *
* @author Yohann Coppel * @author Yohann Coppel
*/ */
public class GeoLocationGenerator implements GeneratorSource { public class GeoLocationGenerator implements GeneratorSource, ChangeListener {
private static final String LON_REGEXP = "[+-]?[0-9]+(.[0-9]+)?"; private static final String LON_REGEXP = "[+-]?[0-9]+(.[0-9]+)?";
private static final String LAT_REGEXP = "[+-]?[0-9]+(.[0-9]+)?"; private static final String LAT_REGEXP = "[+-]?[0-9]+(.[0-9]+)?";
@ -39,12 +50,18 @@ public class GeoLocationGenerator implements GeneratorSource {
TextBox longitude = new TextBox(); TextBox longitude = new TextBox();
TextBox query = new TextBox(); TextBox query = new TextBox();
TextBox mapsLink = new TextBox(); TextBox mapsLink = new TextBox();
MapWidget map = new MapWidget();
Marker mapMarker = null;
private ChangeListener changeListener;
public GeoLocationGenerator(ChangeListener listener) { public GeoLocationGenerator(ChangeListener listener) {
this.changeListener = listener;
latitude.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED); latitude.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
longitude.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED); longitude.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
latitude.addChangeListener(listener); latitude.addChangeListener(listener);
latitude.addChangeListener(this);
longitude.addChangeListener(listener); longitude.addChangeListener(listener);
longitude.addChangeListener(this);
query.addChangeListener(listener); query.addChangeListener(listener);
} }
@ -106,7 +123,7 @@ public class GeoLocationGenerator implements GeneratorSource {
if (table != null) { if (table != null) {
return table; return table;
} }
table = new Grid(5, 2); table = new Grid(7, 2);
table.setText(0, 0, "Latitude"); table.setText(0, 0, "Latitude");
table.setWidget(0, 1, latitude); table.setWidget(0, 1, latitude);
@ -127,9 +144,60 @@ public class GeoLocationGenerator implements GeneratorSource {
table.setWidget(4, 0, fill); table.setWidget(4, 0, fill);
table.setWidget(4, 1, mapsLink); table.setWidget(4, 1, mapsLink);
map.setSize("256px", "256px");
map.addControl(new SmallMapControl());
map.addMapClickHandler(new MapClickHandler() {
public void onClick(MapClickEvent event) {
mapClick(event);
}
});
table.setText(5, 0, "OR");
table.setText(5, 1, "use the map to select a location:");
SimplePanel sp = new SimplePanel();
sp.add(map);
table.setWidget(6, 1, sp);
return table; return table;
} }
protected void mapClick(MapClickEvent event) {
latitude.setText("" + event.getLatLng().getLatitude());
longitude.setText("" + event.getLatLng().getLongitude());
setMapMarker(event.getLatLng().getLatitude(), event.getLatLng().getLongitude(), false);
changeListener.onChange(latitude);
changeListener.onChange(longitude);
}
protected void mapMarkerMoved() {
latitude.setText("" + mapMarker.getLatLng().getLatitude());
longitude.setText("" + mapMarker.getLatLng().getLongitude());
changeListener.onChange(latitude);
changeListener.onChange(longitude);
}
protected void setMapMarker(double lat, double lon, boolean zoomAndCenter) {
if (mapMarker != null) {
map.removeOverlay(mapMarker);
}
LatLng ll = LatLng.newInstance(lat, lon);
if (zoomAndCenter) {
map.setCenter(ll);
map.setZoomLevel(12);
}
if (mapMarker != null) {
mapMarker.setLatLng(ll);
} else {
MarkerOptions opt = MarkerOptions.newInstance();
opt.setDraggable(true);
mapMarker = new Marker(ll, opt);
mapMarker.addMarkerDragEndHandler(new MarkerDragEndHandler() {
public void onDragEnd(MarkerDragEndEvent event) {
mapMarkerMoved();
}
});
}
map.addOverlay(mapMarker);
}
protected void fillWithMaps() { protected void fillWithMaps() {
String link = mapsLink.getText(); String link = mapsLink.getText();
if (!link.matches("http://maps.google.com/.*")) { if (!link.matches("http://maps.google.com/.*")) {
@ -172,6 +240,9 @@ public class GeoLocationGenerator implements GeneratorSource {
query.setText(URL.decode(q)); query.setText(URL.decode(q));
latitude.setText(lat); latitude.setText(lat);
longitude.setText(lon); longitude.setText(lon);
changeListener.onChange(latitude);
changeListener.onChange(longitude);
this.onChange(latitude);
} }
public void validate(Widget widget) throws GeneratorException { public void validate(Widget widget) throws GeneratorException {
@ -182,4 +253,16 @@ public class GeoLocationGenerator implements GeneratorSource {
public void setFocus() { public void setFocus() {
latitude.setFocus(true); latitude.setFocus(true);
} }
public void onChange(Widget sender) {
if (sender == latitude || sender == longitude) {
try {
double lat = Double.parseDouble(getLatitudeField());
double lon = Double.parseDouble(getLongitudeField());
setMapMarker(lat, lon, true);
} catch (NumberFormatException e) {
} catch (GeneratorException e) {
}
}
}
} }

View file

@ -121,3 +121,9 @@ body {
font-size: 11px; font-size: 11px;
color: red; color: red;
} }
div .gmnoprint {
white-space: normal !important;
/* overflow: hidden; */
}