Tomomi Imura

Tomomi Imura

An Open Web advocate and front-end engineer, who loves everything mobile, and writes about HTML5, CSS, JS, UX, tech events, gadgets, etc. She unintentionally got 15min of fame by creating The HTTP Status Cats. Also, the opinions expressed here are solely her own and do not express the views or opinions of my employer.

Twitter LinkedIn Github Flickr

Creative Commons License
My articles are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Find Your Tweeting Neighbor on iPhone with GeoLocation

screenshot

iPhone OS 3.0 is now available, and developers can take advantage of the newly introduced geolocation feature in Safari browser.

To try it out quickly, I used Twitter Search API again to create a tiny test app called, NeighborTweet</a>, which enable you to find out who are tweeting in your neighborhood. Basically, what it does is that obtain your location, and pass the latitude and longitude data to Twitter search and display the result tweets.

Try it out on your iPhone with:
Short URL http://bit.ly/K0ZaE</a>
or
<a href=http://qrcode.kaywa.com/img.php?s=8&d=http%3A%2F%2Fgirliemac.com%2Fgeo”>This QR Code</a> with scanning app like BeeTagg.

If you are interested in learning more on Twitter search API and geocode, please read Twitter Wiki.

OK, now here’s the code.
To find out your location with Geolocation class is simple – you just call getCurrentPosition() method. This initiates an asynchronous request to detect the user’s position.

navigator.geolocation.getCurrentPosition(someFunction)

Get latitude and longitude, by using coords instance:

latitude = position.coords.latitude;
longitude = position.coords.longitude;

Here’s an actual code I used to create the sample app:

if (navigator.geolocation) {  
  navigator.geolocation.getCurrentPosition(function(position) {  
    callback(position.coords.latitude, position.coords.longitude);  
  });
} else {  
  alert("Geolocation services are not supported by your browser.");  
} 

function callback(lat,lon){
  // twitter search json-p callback
  var geocode = "&#038;geocode=" + lat + "%2C" + lon + "%2C1mi"; 
  var fullUrl = url + geocode;   
  ...
}
var url = "http://search.twitter.com/search.json?callback=getTweets";

function getTweets (json) {
  // display json data
  ...
}

References

Geolocation References:

More References:



comments powered by