My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GeolocationApi  
Quick getting started introduction to the Geolocation API
Phase-Implementation, API-Geolocation
Updated Mar 28, 2010 by bguijt

Introduction

The 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 support

Not 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:

  1. iPhone Safari (OS3.0)
  2. Android (all versions)
  3. Firefox (3.5)
  4. Any browser with the Google Gears plugin

Usage

Get the Geolocation API using Maven

Add 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 project

If 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 instance

Geolocation geo = Geolocation.getGeolocation();

Obtaining your position

geo.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();

Beware

Android: "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.

Comment by jvanderd...@gmail.com, Dec 6, 2009

This wiki would be more helpful if it included a description of a typical installation of the GeolocationApi library.

Comment by project member bguijt, Dec 7, 2009

@jvanderdoes What do you mean exactly?

Comment by jvanderd...@gmail.com, Dec 16, 2009

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.

Comment by armeltn...@gmail.com, Dec 17, 2009

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.

Comment by project member bguijt, Dec 17, 2009

Aha!

I forgot to document that part! Will amend to wiki pages with that. Thanks!

Comment by armeltn...@gmail.com, Dec 17, 2009

@bguit, thanks but I found the following "<inherits name="com.google.code.gwt.geolocation.Html5Geolocation" />" in the HelloGeolocation? samples.

Good project!

Comment by reid...@gmail.com, Mar 5, 2010

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.

Comment by project member bguijt, Mar 5, 2010

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

Comment by daniel.s...@gmail.com, Mar 29, 2010

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.

Comment by t3hef...@gmail.com, Feb 23, 2011

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


Sign in to add a comment
Powered by Google Project Hosting