Search code examples
jqueryjquery-mobilemobilegeolocationgoogle-latitude

How to geolocate specific smartphones with javascript or jQuery Mobile?


I'm trying to track specific smartphones for a cab company using jQuery Mobile. What would be the easiest way to go about this? I've heard good things about Google Latitude, but it's still a little tricky. Is there a way to "plug in" to specific smartphones? (With permission from its owner of course.)


Solution

  • You'll find HTML GeoLocation to be much easier to implement, but troublesome for tracking more than a few minutes at a time. Google Latitude is the easiest way to constantly track Android or iOS devices, as the client-side work is done for you and you need only figure out how to ask Google correctly from your server. I have done it a few times on Rails with the Omniauth gem. Here are some snippets that may speed things along for you even if you don't use that particular library:

    Omniauth.rb line dictating which permissions are required:

    provider :google_oauth2, GOOGLE_KEY, GOOGLE_SECRET, { access_type: "offline", approval_prompt: "", scope: "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/latitude.current.best" }
    

    Elsewhere, calling for latitude given the access token:

    latitude_data = HTTParty.get('https://www.googleapis.com/latitude/v1/currentLocation?granularity=best&access_token='+self.access_token)['data']
    

    Another thing that will take some time is setting up a background task that exchanges the long-term "refresh" token for a new access token once per hour. This is needed for Latitude because of extra security concerns. You'll find that the refresh token is only sent on the first time your app is authorized, and that subsequently it's necessary to deauthorize the application at accounts.google.com and reauthorize to get a refresh token. I think there's an easier way involving an extra parameter when authorizing that forces reauth, but I haven't used it yet.

    Good luck! Latitude is my favorite API, a great tool in the new geo-aware internet! For more examples of both Geolocation API and Latitude, check out my project using both at github.com/nelsonblaha/groovitation