Tuesday, February 28, 2012

Accessing GPSLocation data from images

There are a few things that people are unaware of and one of those happens to be the fact that images from cell phones contain GPS location data. What!?! You exclaim. Well, there are many social sites that do strip this information. Although we won't be covering that here, I will cover how to access this data using jQuery and the jQuery plugin Exif.

w00t! Let's get started on this...

Files you will need:

jQuery: https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
Exif Plugin: http://www.nihilogic.dk/labs/exifjquery/jquery.exif.js

I've also included code from Robert K. Davis showing how to convert DMS (Degrees, Minutes Seconds) to decimal for passing to Google Maps.

I'm not a javascript guru and if you see anything that would make this function better then please feel free to comment on this post.

**NOTE: exifPretty() is used to gather a complete list of exif data contained within the image(s).


DMS to DECIMAL: http://www.codingforums.com/archive/index.php/t-9709.html

$(".img").click(function() {
                
                    var longitude = $(this).exif("GPSLongitude");
                    var latitude = $(this).exif("GPSLatitude");
                    
                    var latRef = $(this).exif("GPSLatitudeRef");
                    var lngRef = $(this).exif("GPSLongitudeRef");
    
                    // Let's make the object to a string to split on the ,'s
                    var a1 = new Array();
                    var a2 = new Array();
                    
                    a1=longitude.toString().split(',');
                    a2=latitude.toString().split(',');
                    
                    if(lngRef == "W") {
                        a1[0] = Number(a1[0]);
                        a1[0] = -a1[0];
                    }
                    
                    if(latRef == "S") {
                        a2[0] = Number(a2[0]);
                        a2[0] = -a2[0];
                    }
                    
                    // Alert all of out exif data
                    // alert($(this).exifPretty());
                    
                    /* Populate the fields with the correct data */
                    
                    // Latitude
                    $("input[name=LatDegrees]").val(a2[0]);
                    $("input[name=LatMinutes]").val(a2[1]);
                    $("input[name=LatSeconds]").val(a2[2]);
                    // Longitude
                    $("input[name=LonDegrees]").val(a1[0]);
                    $("input[name=LonMinutes]").val(a1[1]);
                    $("input[name=LonSeconds]").val(a1[2]);
                    
                    //$("this.form").submit(toDecimal(this.form));
                    
                }); 
            });




            /******************************************
            DMS to Decimal Latitude/Longitude Converter
            © 2002 Robert K. Davis [DMS to DEC /START]
            *******************************************/
            function convert(D,M,S){
                 var DD;
                 D < 0 ? DD = roundOff(D + (M/-60) + (S/-3600),6) : DD = roundOff(D + (M/60) + (S/3600),6);
                 return DD;
            }
            function roundOff(num,decimalplaces){
                 var decimalfactor = Math.pow(10,decimalplaces);
                 var roundedValue = Math.round(num*decimalfactor)/decimalfactor;
                 return roundedValue;
            }
            function toDecimal(f){
                 var LatDegrees = parseInt(f.LatDegrees.value);
                 var LatMinutes = parseInt(f.LatMinutes.value);
                 var LatSeconds = parseInt(f.LatSeconds.value);
                 var LonDegrees = parseInt(f.LonDegrees.value);
                 var LonMinutes = parseInt(f.LonMinutes.value);
                 var LonSeconds = parseInt(f.LonSeconds.value);
            
                 var LatDecimalDegrees = convert(LatDegrees,LatMinutes,LatSeconds);
                 var LonDecimalDegrees = convert(LonDegrees,LonMinutes,LonSeconds);
            
                 !isNaN(LatDecimalDegrees) && !(LatDecimalDegrees > 90) && !(LatDecimalDegrees < -90) ? f.LatDecimalDegrees.value = LatDecimalDegrees : f.LatDecimalDegrees.value = "";
                 !isNaN(LonDecimalDegrees) && !(LonDecimalDegrees > 180) && !(LonDecimalDegrees < -180)  ? f.LonDecimalDegrees.value = LonDecimalDegrees : f.LonDecimalDegrees.value = "";
                 
                 sendToGoogle(LatDecimalDegrees, LonDecimalDegrees);
            }
            
            /******************************************
            DMS to Decimal Latitude/Longitude Converter
            © 2002 Robert K. Davis [DMS to DEC /END]
            *******************************************/

No comments: