Last updated by gauravchauhan 4 months ago
Introduction
This plugin provides support for the HTML5's geolocation feature. In 3 steps you will be able to add location based behaviour to your application.
User's current position will be retrieved and put into the session. The Geolocation is representated by the Geolocation domain class which implements the official W3C specifications.
Usage
3 steps to make your app location based (assuming you are already using a Ajax lib like jquery or prototype) :
- grails install-plugin geolocation
- add the tag <geolocation:resources/> in the head section of your page you want to be location aware
- add the tag <geolocation:locateMe/> in the body section of your page
If before installing the plugin you don't have yet include a Ajax lib in your page you can use :
- Grails 1.3.x <geolocation:resourcesPrototype/> : If you add this line then you don't need to add <geolocation:resources/> line in your page. All you need is just one line. It will load json.js as well as the prototype.js library in your gsp page. It won't work in a Grails 2.0 app if you don't have prototype.js library located inside the js folder of your app.
- Grails 2.0.0 <geolocation:resourcesJquery/> : If you add this line then you don't need to add <geolocation:resources/> line in your page. All you need is just one line. It will load json.js as well as the jquery.js library in your gsp page.
After the user confirms he wants to share it's position, the Geoposition instance will be available in the session :
- From a gsp (inside a javascript function) :
var latlng = new google.maps.LatLng(${session.position.coords.latitude},${session.position.coords.longitude});
def position = session['position']
Once the user's location is known you can use the test page provided by the plugin to test it with a google map widget :
your_app_name/geolocation/showMap
Utility methods
Since version 0.2 you are able to perform some operations on your positions instances :
double distance = myPosition.distanceFrom(otherPosition)
- Check if your position is in a range
boolean isInRange = myPosition.isInRange(otherPosition,50) // check if the position is an range of 50 Km
These 2 methods are also available in the GeolocationService bean.
The default length unit is "KILOMETER" but you can configure it by setting in your config the following key :
geolocation.length.unit="MILE" //or KILOMETER, METER, NAUTIC_MILE
Browsers support
The geolocation feature is actually supported by :
- iPhone Safari
- Safari (5.0)
- Firefox (3.5 and above)
I noticed that with Safari 5.0 , it can take a while (about a minute

!) before your position is retrieved .
Please update this section if you test it with other browser (i.e : Android browsers)
Domain classes
Geoposition.groovy
package org.grails.plugins.geolocation
import org.grails.plugins.geolocation.Coordinates;class GeoPosition {
Coordinates coords
long timestamp
static constraints = {
}
}Coordinates.groovy
package org.grails.plugins.geolocationimport org.grails.plugins.geolocation.GeoPosition;class Coordinates {
GeoPosition position
static constraints = {
latitude(nullable:true)
longitude(nullable:true)
altitude(nullable:true)
accuracy(nullable:true)
altitudeAccuracy(nullable:true)
heading(nullable:true)
speed(nullable:true)
}
double latitude
double longitude
double altitude
double accuracy
double altitudeAccuracy
double heading
double speed
static belongsTo = GeoPosition
}Source code
Source code is available at the the svn codehaus site but also on github :
http://github.com/sebastienblanc/geolocation