|
GeolocationApi
IntroductionThe Geolocation API (download) leverages the W3C Geolocation API. As of June 2009 some browsers started to add support for this API, specifically Firefox 3.5 and iPhone Safari 3.0. This API gives you the WGS84 coordinates of the location of the browser. Browser supportNot every browser supports this feature. Fortunately, there is a fallback scenario to rely on the Gears plugin - that API is very similar. We built support for the following browsers:
UsageGet the Geolocation API using MavenAdd the repository and the following to your pom.xml file: <dependencies>
...
<dependency>
<groupId>com.google.code.gwt-mobile-webkit</groupId>
<artifactId>gwt-html5-geolocation</artifactId>
</dependency>
</dependencies>Add the Geolocation API to your projectIf you don't use Maven, copy the gwt-html5-geolocation-x.x.x.jar file to your project classpath. This jar contains everything you need to use the Geolocation API. Next, in your GWT module gwt.xml file (which uses the Geolocation API), add the following entry: <inherits name="com.google.code.gwt.geolocation.Html5Geolocation" /> This imports the Geolocation API into your module. Now you are ready to use the API! is API supported?The following code will test whether the API is supported in your browser: if (Geolocation.isSupported()) {
// get your Geo location...
}Getting a Geolocation instanceGeolocation geo = Geolocation.getGeolocation(); Obtaining your positiongeo.getCurrentPosition(new PositionCallback() {
public void onFailure(PositionError error) {
// Handle failure
}
public void onSuccess(Position position) {
Coordinates coords = position.getCoords();
// ...
}
});The location is enclosed in the Position instance returned in the onSuccess() callback method: //... Coordinates coords = position.getCoords(); double latitude = coords.getLatitude(); double longitude = coords.getLongitude(); double accuracy = coords.getAccuracy(); BewareAndroid: "Null or undefined passed for required argument 1."The Gears API in the Android browser is implemented differently from the 'regular' (desktop) plugin. Due to some weird API error, you could get an error message whenever you try to call a Geolocation method (like getCurrentPosition()). This is only for pre-2.0 Android versions, as of 2.0 Android uses the regular HTML5 API. If the error pops up, and you use the Geolocation API version 0.9.4 or higher, please report! The 0.9.4 release should mitigate any Android Gears error. |
This wiki would be more helpful if it included a description of a typical installation of the GeolocationApi library.
@jvanderdoes What do you mean exactly?
I am using the Eclipse plugin and have a difficult time integrating the Geolocational API with my existing project. I would like to see a tutorial that includes setting up the project and importing the API correctly.
I have downloaded the API but found it a bit difficult integrating with my app too. Can you tell me which name should I inherit in Main.gwt.xml. What I am looking for is something on the line of <inherits name="com.google.code.gwt.Geolocation"/>. Looking forward to your reply.
Aha!
I forgot to document that part! Will amend to wiki pages with that. Thanks!
@bguit, thanks but I found the following "<inherits name="com.google.code.gwt.geolocation.Html5Geolocation" />" in the HelloGeolocation? samples.
Good project!
It's excellent code segment to help us understanding Geolocation API. However, in your example, we could only obtain Latitude and Long Latitude information in OnSuccess? method. How about to obtain both value out Callback function, for example, to bind in GWT UI components say Label or textbox? Thanks for your advice.
@reidlai The actual Javascript API to obtain geolocation data is asynchronous; this means we have to use callback methods to get the data, there is no way around that. If you need that, you should 'pre-fetch' the location, and in the onSuccess() method construct and bind the widgets with the data, just like you would do using GWT's RPC.
Has anyone attempted using the GeographicalArea? object. It seems to be returning values much greater than expected for getDistance method...This seems to be unrelated to the UnitType? parameter.
for those still facing issues getting this working
it's not your gwt.xml file it's your PROJECTNAMEHERE.gwt.xml file
and "add the jar file to your class path" means http://stackoverflow.com/questions/179024/adding-a-jar-to-an-eclipse-java-library