1

Hi I have created the core parts of a web app with Google Maps V3 API and Codeigniter and now I want to make the website aware of the location of the user. From my current knowledge, I will be able to get it from the GPS sensor of the user if he is on a phone/tablet with a GPS sensor, or get it from the IP address of the browser on a desktop/laptop.

  1. How do I get his location (in Lat/Lng?) from his GPS sensor on his phone?
  2. How do I get the location from his IP address of his browser
  3. I remember Firefox being a location aware browser. How do I access the location data if its firefox? Is this a viable option?
  4. Are there other methods? (Wifi, celltower triangulation?)
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • 1
    You might find this question a good starting point for some of this: http://stackoverflow.com/questions/221592/geolocation-api-on-the-iphone – Paul Dixon Jun 05 '11 at 21:49

1 Answers1

1

Read all about how to get the user's location at the Google Maps API v3 documentation here: http://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation

It does not matter if the source is a GPS sensor or something else--you still get it the same way. It seems to me that the documentation there answers all four of your questions, more or less.

Here's the relevant sample code from that page:

// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);
  }
}
Trott
  • 66,479
  • 23
  • 173
  • 212