Skip to content

Commit

Permalink
Some work on the geocoder, but doesn't work yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoriman committed Jan 25, 2010
1 parent 4d23f47 commit d3ae361
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 20 deletions.
8 changes: 7 additions & 1 deletion src/com/googlecode/maps3/client/MapWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public MapWidget(MapOptions opts)
this.map = MapJSO.newInstance(this.getElement(), opts);
}

/** */
public MapJSO getMapJSO()
{
return this.map;
}

/**
* Note: Call this *after* you add it to a LayoutPanel
*/
Expand Down Expand Up @@ -52,5 +58,5 @@ private void forceParentLayout(Widget w)
public void addListener(MapEventType type, Runnable handler)
{
this.map.addListener(type.getUnderlying(), handler);
}
}
}
17 changes: 7 additions & 10 deletions src/com/googlecode/maps3/client/auto/Geocoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,22 @@ public class Geocoder extends JavaScriptObject {
* @return An instance of Geocoder.
*/
public static native Geocoder newInstance() /*-{
return new $wnd.Geocoder();
return new $wnd.google.maps.Geocoder();
}-*/;

protected Geocoder() { }

/**
* Geocode a request.
*
* @param request
* @param callback
*
* TODO: Replace type Runnable with implementation of function(Array<GeocoderResponse>.
* TODO: Missing comments for parameter callback.
* TODO: Missing comments for parameter request.
* @param request defines the request that will be made
* @param callback will be called with whatever the geocoder returns
*/
public final native void geocode(GeocoderRequest request, Runnable callback) /*-{
public final native void geocode(GeocoderRequest request, GeocoderCallback callback) /*-{
this.geocode(
request,
callback
request, function(results, status) {
[email protected]::callback([Lcom/googlecode/maps3/client/auto/GeocoderResponse;Lcom/googlecode/maps3/client/auto/GeocoderStatus;)(results, status);
}
);
}-*/;

Expand Down
29 changes: 29 additions & 0 deletions src/com/googlecode/maps3/client/auto/GeocoderCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.googlecode.maps3.client.auto;

/**
* Interface called by the geocoder when results arrive.
* @author Jeff Schnitzer <[email protected]>
*/
public interface GeocoderCallback {

/**
* Result of the query is now available
*/
public void callback(GeocoderResponse[] responses, GeocoderStatus status);
}
4 changes: 1 addition & 3 deletions src/com/googlecode/maps3/client/auto/GeocoderRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ public class GeocoderRequest extends JavaScriptObject {
* Instantiates a GeocoderRequest object.
*
* @return An instance of GeocoderRequest.
*
* TODO: This class did not have a specified constructor in JavaScript. Consider removing factory method.
*/
public static native GeocoderRequest newInstance() /*-{
return new $wnd.GeocoderRequest();
return {};
}-*/;

protected GeocoderRequest() { }
Expand Down
4 changes: 2 additions & 2 deletions src/com/googlecode/maps3/client/auto/GeocoderStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public class GeocoderStatus extends JavaScriptObject {
* TODO: This class contains only static members. Consider removing factory method.
*/
public static native GeocoderStatus newInstance() /*-{
return new $wnd.GeocoderStatus();
return new $wnd.google.maps.GeocoderStatus();
}-*/;

private static native String getConstant(String name) /*-{
return $wnd.GeocoderStatus[name];
return $wnd.google.maps.GeocoderStatus[name];
}-*/;

protected GeocoderStatus() { }
Expand Down
81 changes: 77 additions & 4 deletions src/com/googlecode/maps3/demo/client/Demo.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package com.googlecode.maps3.demo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.googlecode.maps3.client.InfoWindow;
import com.googlecode.maps3.client.LatLng;
import com.googlecode.maps3.client.LatLngBounds;
import com.googlecode.maps3.client.MapOptions;
import com.googlecode.maps3.client.MapWidget;
import com.googlecode.maps3.client.auto.Geocoder;
import com.googlecode.maps3.client.auto.GeocoderCallback;
import com.googlecode.maps3.client.auto.GeocoderRequest;
import com.googlecode.maps3.client.auto.GeocoderResponse;
import com.googlecode.maps3.client.auto.GeocoderStatus;

/**
* Entry point classes define <code>onModuleLoad()</code>.
Expand All @@ -22,15 +37,73 @@ public void onModuleLoad()
// opts.setCenter(LatLng.newInstance(37.760773,-122.434448));
// opts.setZoom(14);

MapWidget map = new MapWidget(opts);
DockLayoutPanel mainDock = new DockLayoutPanel(Unit.EM);
RootLayoutPanel.get().add(mainDock);

FlowPanel controls = new FlowPanel();
controls.setStyleName("controls");
mainDock.addNorth(controls, 2);

final MapWidget map = new MapWidget(opts);
mainDock.add(map);

LatLngBounds bounds = LatLngBounds.newInstance();
bounds.extend(LatLng.newInstance(37.660773,-122.534448));
bounds.extend(LatLng.newInstance(37.860773,-122.334448));
map.fitBounds(bounds);

RootLayoutPanel.get().add(map);
//RootPanel.get("basic").add(map); // doesn't work for some reason
// Add the controls
final TextBox latLonBox = new TextBox();
latLonBox.setText("37,-122");
controls.add(latLonBox);

map.fitBounds(bounds);
controls.add(new Button("Reverse Geocode", new ClickHandler() {
@Override
public void onClick(ClickEvent event)
{
String latLonStr = latLonBox.getText();
String[] split = latLonStr.split(",");

float lat = Float.parseFloat(split[0]);
float lon = Float.parseFloat(split[1]);
final LatLng point = LatLng.newInstance(lat, lon);

map.getMapJSO().panTo(point);

GeocoderRequest request = GeocoderRequest.newInstance();
request.setLatLng(point);

GWT.log("Geocoding " + point, null);

Geocoder geocoder = Geocoder.newInstance();
geocoder.geocode(request, new GeocoderCallback() {
@Override
public void callback(GeocoderResponse[] responses, GeocoderStatus status)
{
GWT.log("Got callback with status " + status, null);

if (status.equals(GeocoderStatus.OK))
{
InfoWindow info = InfoWindow.newInstance();
info.setPosition(point);
if (responses.length > 0)
{
info.setContent(responses[0].toString());
}
else
{
info.setContent("No results from geocoder");
}

info.open(map.getMapJSO());
}
else
{
Window.alert("Geocoder failed: " + status);
}
}
});
}
}));
}
}
6 changes: 6 additions & 0 deletions war/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<title>Maps3 Demo</title>

<script type="text/javascript" language="javascript" src="demo/demo.nocache.js"></script>

<style type="text/css">
.controls {
text-align: center;
}
</style>
</head>

<body>
Expand Down

0 comments on commit d3ae361

Please sign in to comment.