Permalink
Browse files

Added the reverse geolocation functionality

  • Loading branch information...
1 parent 3aa7f31 commit 4da828355de8ba90ba9b61d75cfcfa847702c317 @parolkar committed Oct 20, 2009
@@ -1,7 +1,8 @@
class GeoMereLaalController < ApplicationController
def init
- response["Content-type"] = "text/javascript"
+ response["Content-type"] = "text/javascript"
+
end
end
@@ -6,7 +6,41 @@
<%= latitude_field model_name, "lat" %>
<br>
<%= label model_name,"lng","Longitude" -%>
- <%= longitude_field model_name, "lng" %>
+ <%= longitude_field model_name, "lng" %>
+
+
+
+ <br>
+ <%= label model_name,"street","Street" -%>
+ <%= street_field model_name, "street" %>
+
+ <br>
+ <%= label model_name,"locality","Locality" -%>
+ <%= locality_field model_name, "locality" %>
+
+ <br>
+ <%= label model_name,"city","City" -%>
+ <%= city_field model_name, "city" %>
+
+ <br>
+ <%= label model_name,"state","State" -%>
+ <%= state_field model_name, "state" %>
+
+ <br>
+ <%= label model_name,"country_name","Country Name" -%>
+ <%= country_name_field model_name, "country_name" %>
+
+ <br>
+ <%= label model_name,"country_code","Country Code" -%>
+ <%= country_code_field model_name, "country_code" %>
+
+ <br>
+ <%= label model_name,"postal_code","Postal Code" -%>
+ <%= postal_code_field model_name, "postal_code" %>
+
+ <br><br><br>
+ <%= label model_name,"address","Full Address" -%>
+ <%= full_address_field model_name, "address" %>
</div >
@@ -15,11 +49,12 @@
{
document.getElementById("geo-mere-laal-wait").style.display = "block";
GeoMereLaal.onReceive = function(){
-
+
document.getElementById("geo-mere-laal-fields").style.display = "block";
document.getElementById("geo-mere-laal-wait").style.display = "none";
+
}
- GeoMereLaal.request();
+
}else{
document.getElementById("geo-mere-laal-fields").style.display = "block";
@@ -3,8 +3,7 @@
GeoMereLaal = function(){
-
-
+
return {
lat: function(){
return this.getCookie("latitude");
@@ -82,6 +81,7 @@ GeoMereLaal = function(){
successCallbacks:function(){
GeoMereLaal.updateLatitude_callback();
GeoMereLaal.updateLongitude_callback();
+ GeoMereLaal.reverseCoderHook();
GeoMereLaal.onReceive();
},
@@ -97,10 +97,109 @@ GeoMereLaal = function(){
},
updateLongitude_callback:function(){
//This is a callback stub to be overridden by user level code
+ },
+ reverseCoderHook:function(){
+ //This is a stub to be overridden by GMLReverseCoder's code
}
+}}();
+GMLloadGMapAPI= function(api_key){
+ document.write("<script src=\"http://maps.google.com/maps?file=api&v=2&sensor=true&key="+api_key+"\" type=\"text/javascript\"></script>");
+ }
+
+
+GMLReverseCoder = function(){
+
+
+ if(typeof GClientGeocoder == "undefined"){
+ GMLloadGMapAPI("<%= GML_GMAP_APIKEY -%>")};
+
+
+return {
+ request:function(gml_obj){
+ geocoder = new GClientGeocoder();
+ latlng = new GLatLng(parseFloat(gml_obj.lat()),parseFloat(gml_obj.lng()));
+ geocoder.getLocations(latlng, this.responseHandler);
+
+ },
+ responseHandler:function(response){
+ if (!response || response.Status.code != 200) {
+ alert("GMLError (Unable to make GMap Request): Status Code:" + response.Status.code);
+ } else {
+ place = response.Placemark[0];
+ point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
+
+
+ try{
+ GMLReverseCoder.updateFullAddress_callback(place.address);
+ } catch (e) {}
+
+ try{
+ GMLReverseCoder.updateCountryCode_callback(place.AddressDetails.Country.CountryNameCode);
+ } catch (e) {}
+
+ try{
+ GMLReverseCoder.updateCountryName_callback(place.AddressDetails.Country.CountryName);
+ } catch (e) {}
+
+ try{
+ GMLReverseCoder.updateState_callback( place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
+ } catch (e) {}
+
+ try{
+ GMLReverseCoder.updateCity_callback( place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);
+ } catch (e) {}
+
+ try{
+ GMLReverseCoder.updateLocality_callback( place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
+ }catch (e) {
+ GMLReverseCoder.updateLocality_callback( place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.DependentLocality.DependentLocalityName);
+ }
+
+ try{
+ GMLReverseCoder.updateStreet_callback( place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.DependentLocality.Thoroughfare.ThoroughfareName);
+ }catch (e) {}
+
+
+ try{
+ GMLReverseCoder.updatePostalCode_callback( place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber);
+ }catch (e) {}
+
+
+ }
+
+
+ },
+ updateFullAddress_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updateCountryName_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updateCountryCode_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updateState_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updateCity_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updateLocality_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updateStreet_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ },
+ updatePostalCode_callback:function(value){
+ //This is a callback stub to be overridden by user level code
+ }
+
+
+}}();
+
+
-}}();
View
@@ -1,5 +1,4 @@
require "geo_mere_laal"
require "geo_mere_laal_helper"
-
ActionView::Base.send(:include, GeoMereLaalHelper) # The helper beauty comes here
View
@@ -1,7 +1,7 @@
class GeoMereLaal
def self.init
- " <script id=\"geo_mere_laal_loader\"></script><script>if(typeof GeoMereLaal == 'undefined') document.getElementById(\"geo_mere_laal_loader\").src = \"/geo_mere_laal/init\" ;</script>"
+ " <script id=\"geo_mere_laal_loader\" src=\"/geo_mere_laal/init\" > </script>"
end
end
@@ -4,19 +4,94 @@ module GeoMereLaalHelper
def latitude_field(object, method, options={})
it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
- result = it.to_input_field_tag("text", options)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml
result << "<script> GeoMereLaal.updateLatitude_callback = function(){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",GeoMereLaal.lat())}; </script>"
end
def longitude_field(object, method, options={})
it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
- result = it.to_input_field_tag("text", options)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml
result << "<script> GeoMereLaal.updateLongitude_callback = function(){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",GeoMereLaal.lng())}; </script>"
+ end
+
+
+ def full_address_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateFullAddress_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
end
-
+ def country_name_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateCountryName_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+
+ def country_code_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateCountryCode_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+
+ def state_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateState_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+
+ def city_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateCity_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+
+ def locality_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateLocality_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+
+ def street_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updateStreet_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+ def postal_code_field(object, method, options={})
+ it = ActionView::Helpers::InstanceTag.new(object.dup, method, self)
+ result = it.to_input_field_tag("text", options)
+ result << init_gml_rc
+ result << "<script> GMLReverseCoder.updatePostalCode_callback = function(resp_value){ document.getElementById(\"#{it.object_name.to_s+'_'+it.method_name.to_s}\").setAttribute(\"value\",resp_value)}; </script>"
+ end
+
+ def init_gml
+ result = ""
+ if !@gml_initialised
+ result << GeoMereLaal.init
+ result << "<script> old_onload = window.onload; window.onload = function() { if(GeoMereLaal.isBrowserCompatible()){ GeoMereLaal.request(); } if(typeof old_onload == \"function\"){old_onload();}} </script>"
+ @gml_initialised ||= true
+ end
+ result
+ end
+ def init_gml_rc
+ result = ""
+ if !@gml_rc_initialised
+ result << init_gml
+ result << "<script> if(GeoMereLaal.isBrowserCompatible()){ GeoMereLaal.reverseCoderHook = function(){ GMLReverseCoder.request(GeoMereLaal); } } </script>"
+ @gml_rc_initialised ||= true
+ end
+ result
+ end
end

0 comments on commit 4da8283

Please sign in to comment.