Geolocation support
Dependency :
compile ":geolocation:0.4.1"
Summary
This plugin adds HTML5 geolocation support and offers some utlity methods to calculate distances and range checks
Installation
grails install-plugin geolocation
Description
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
- 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.
- From a gsp (inside a javascript function) :
var latlng = new google.maps.LatLng(${session.position.coords.latitude},${session.position.coords.longitude});
- From a controller :
def position = session['position']
your_app_name/geolocation/showMap
Utility methods
Since version 0.2 you are able to perform some operations on your positions instances :- Calculate a distance
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
geolocation.length.unit="MILE" //or KILOMETER, METER, NAUTIC_MILEBrowsers support
The geolocation feature is actually supported by :- iPhone Safari
- Safari (5.0)
- Firefox (3.5 and above)
- Opera (Tested with 11.64)
Domain classes
Geoposition.groovypackage org.grails.plugins.geolocation import org.grails.plugins.geolocation.Coordinates;class GeoPosition { Coordinates coords long timestamp static constraints = { } }
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