diff options
author | Matthew Somerville <matthew@mysociety.org> | 2011-09-06 15:37:40 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2011-09-06 15:38:10 +0100 |
commit | 2444a7ed22ef8800fd6d9e6440dab4b7a4ee3dc6 (patch) | |
tree | 4e31a495e3e4d3ea04315d92b0c19a32a783e8b4 | |
parent | 572f1a9ffdfa895bb46184c21a0b960587fa6cf6 (diff) |
Add geolocation.
-rw-r--r-- | templates/web/default/common_header_tags.html | 1 | ||||
-rw-r--r-- | web/css/core.scss | 7 | ||||
-rw-r--r-- | web/i/flower.gif | bin | 0 -> 1644 bytes | |||
-rw-r--r-- | web/js/fixmystreet.js | 28 | ||||
-rw-r--r-- | web/js/geo.min.js | 85 |
5 files changed, 121 insertions, 0 deletions
diff --git a/templates/web/default/common_header_tags.html b/templates/web/default/common_header_tags.html index d0140d69c..44da6aa9b 100644 --- a/templates/web/default/common_header_tags.html +++ b/templates/web/default/common_header_tags.html @@ -1,5 +1,6 @@ <meta http-equiv="content-type" content="text/html; charset=utf-8"> <script type="text/javascript" src="/jslib/jquery-1.6.2.min.js"></script> +<script type="text/javascript" src="[% version('/js/geo.min.js') %]"></script> <script type="text/javascript" src="[% version('/js/fixmystreet.js') %]"></script> [% map_js %] diff --git a/web/css/core.scss b/web/css/core.scss index 5fafe625b..d5224ee0b 100644 --- a/web/css/core.scss +++ b/web/css/core.scss @@ -103,6 +103,12 @@ $map_width: 500px; } } + #geolocate_para { + font-size: 70%; + margin: 2px 0 0 0; + text-align: right; + } + #front_intro { float: left; width: 48%; @@ -232,6 +238,7 @@ $map_width: 500px; background-color: #f1f1f1; } + /* Drag is only present in noscript form */ #drag { position: absolute; width: $map_width; diff --git a/web/i/flower.gif b/web/i/flower.gif Binary files differnew file mode 100644 index 000000000..7e8f22a77 --- /dev/null +++ b/web/i/flower.gif diff --git a/web/js/fixmystreet.js b/web/js/fixmystreet.js index 4b19dc53e..b67c4d74b 100644 --- a/web/js/fixmystreet.js +++ b/web/js/fixmystreet.js @@ -57,4 +57,32 @@ $(function(){ timer = window.setTimeout(email_alert_close, 2000); }); + // Geolocation + if (geo_position_js.init()) { + $('#postcodeForm').append('<p id="geolocate_para">Or <a href="#" id="geolocate_link">locate me automatically</a>').css({ "padding-bottom": "0.5em" }); + $('#geolocate_link').click(function(e) { + e.preventDefault(); + // Spinny thing! + $('#geolocate_para').append(' <img src="/i/flower.gif" alt="" align="bottom">'); + geo_position_js.getCurrentPosition(function(pos) { + $('#geolocate_para img').remove(); + var latitude = pos.coords.latitude; + var longitude = pos.coords.longitude; + location.href = '/around?latitude=' + latitude + ';longitude=' + longitude; + }, function(err) { + $('#geolocate_para img').remove(); + if (err.code == 1) { // User said no + } else if (err.code == 2) { // No position + $('#geolocate_para').html("Could not look up location"); + } else if (err.code == 3) { // Too long + $('#geolocate_para').html("No result returned"); + } else { // Unknown + $('#geolocate_para').html("Unknown error"); + } + }, { + timeout: 10000 + }); + }); + } + }); diff --git a/web/js/geo.min.js b/web/js/geo.min.js new file mode 100644 index 000000000..4f44b30ca --- /dev/null +++ b/web/js/geo.min.js @@ -0,0 +1,85 @@ +// geo-location-javascript v0.4.8 http://code.google.com/p/geo-location-javascript/ Copyright (c) 2009 Stan Wiechers. Licensed under the MIT licenses. + +var bb_success;var bb_error;var bb_blackberryTimeout_id=-1;function handleBlackBerryLocationTimeout() +{if(bb_blackberryTimeout_id!=-1) +{bb_error({message:"Timeout error",code:3});}} +function handleBlackBerryLocation() +{clearTimeout(bb_blackberryTimeout_id);bb_blackberryTimeout_id=-1;if(bb_success&&bb_error) +{if(blackberry.location.latitude==0&&blackberry.location.longitude==0) +{bb_error({message:"Position unavailable",code:2});} +else +{var timestamp=null;if(blackberry.location.timestamp) +{timestamp=new Date(blackberry.location.timestamp);} +bb_success({timestamp:timestamp,coords:{latitude:blackberry.location.latitude,longitude:blackberry.location.longitude}});} +bb_success=null;bb_error=null;}} +var geo_position_js=function(){var pub={};var provider=null;var u="undefined";pub.showMap=function(latitude,longitude) +{if(typeof(blackberry)!=u) +{blackberry.launch.newMap({"latitude":latitude*100000,"longitude":-longitude*100000});} +else +{window.location="http://maps.google.com/maps?q=loc:"+latitude+","+longitude;}} +pub.getCurrentPosition=function(success,error,opts) +{provider.getCurrentPosition(success,error,opts);} +pub.init=function() +{try +{if(typeof(geo_position_js_simulator)!=u) +{provider=geo_position_js_simulator;} +else if(typeof(bondi)!=u&&typeof(bondi.geolocation)!=u) +{provider=bondi.geolocation;} +else if(typeof(navigator.geolocation)!=u) +{provider=navigator.geolocation;pub.getCurrentPosition=function(success,error,opts) +{function _success(p) +{if(typeof(p.latitude)!=u) +{success({timestamp:p.timestamp,coords:{latitude:p.latitude,longitude:p.longitude}});} +else +{success(p);}} +provider.getCurrentPosition(_success,error,opts);}} +else if(typeof(window.blackberry)!=u&&blackberry.location.GPSSupported) +{if(typeof(blackberry.location.setAidMode)==u) +{return false;} +blackberry.location.setAidMode(2);pub.getCurrentPosition=function(success,error,opts) +{bb_success=success;bb_error=error;if(opts['timeout']) +{bb_blackberryTimeout_id=setTimeout("handleBlackBerryLocationTimeout()",opts['timeout']);} +else +{bb_blackberryTimeout_id=setTimeout("handleBlackBerryLocationTimeout()",60000);} +blackberry.location.onLocationUpdate("handleBlackBerryLocation()");blackberry.location.refreshLocation();} +provider=blackberry.location;} +else if(typeof(window.google)!="undefined"&&typeof(google.gears)!="undefined") +{provider=google.gears.factory.create('beta.geolocation');pub.getCurrentPosition=function(successCallback,errorCallback,options) +{function _successCallback(p) +{if(typeof(p.latitude)!="undefined") +{successCallback({timestamp:p.timestamp,coords:{latitude:p.latitude,longitude:p.longitude}});} +else +{successCallback(p);}} +provider.getCurrentPosition(_successCallback,errorCallback,options);}} +else if(typeof(Mojo)!=u&&typeof(Mojo.Service.Request)!="Mojo.Service.Request") +{provider=true;pub.getCurrentPosition=function(success,error,opts) +{parameters={};if(opts) +{if(opts.enableHighAccuracy&&opts.enableHighAccuracy==true) +{parameters.accuracy=1;} +if(opts.maximumAge) +{parameters.maximumAge=opts.maximumAge;} +if(opts.responseTime) +{if(opts.responseTime<5) +{parameters.responseTime=1;} +else if(opts.responseTime<20) +{parameters.responseTime=2;} +else +{parameters.timeout=3;}}} +r=new Mojo.Service.Request('palm://com.palm.location',{method:"getCurrentPosition",parameters:parameters,onSuccess:function(p){success({timestamp:p.timestamp,coords:{latitude:p.latitude,longitude:p.longitude,heading:p.heading}});},onFailure:function(e){if(e.errorCode==1) +{error({code:3,message:"Timeout"});} +else if(e.errorCode==2) +{error({code:2,message:"Position unavailable"});} +else +{error({code:0,message:"Unknown Error: webOS-code"+errorCode});}}});}} +else if(typeof(device)!=u&&typeof(device.getServiceObject)!=u) +{provider=device.getServiceObject("Service.Location","ILocation");pub.getCurrentPosition=function(success,error,opts) +{function callback(transId,eventCode,result){if(eventCode==4) +{error({message:"Position unavailable",code:2});} +else +{success({timestamp:null,coords:{latitude:result.ReturnValue.Latitude,longitude:result.ReturnValue.Longitude,altitude:result.ReturnValue.Altitude,heading:result.ReturnValue.Heading}});}} +var criteria=new Object();criteria.LocationInformationClass="BasicLocationInformation";provider.ILocation.GetLocation(criteria,callback);}}} +catch(e){if(typeof(console)!=u) +{console.log(e);} +return false;} +return provider!=null;} +return pub;}();
\ No newline at end of file |