views:

220

answers:

1

I have read up on GPS Real time tracking and found out several things about it, mostly requiring PHP, zope and a database to store the incoming data. Some other methods uses ajax with relations to PHP.

As regards to my question, is it possible to do so with just html and JS, using markers or anything else to populate the Google Map when you move anywhere in the city? Need some help on this, Thanks!

+2  A: 

Yes, it is possible. Most browsers in the latest smartphones have implemented the W3C Geolocation API:

The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude. The API itself is agnostic of the underlying location information sources. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. No guarantee is given that the API returns the device's actual location.

The API is designed to enable both "one-shot" position requests and repeated position updates, as well as the ability to explicitly query the cached positions.

Using the Geolocation API to plot a point on Google Maps, will look something like this:

if (navigator.geolocation) { 
  navigator.geolocation.getCurrentPosition(function(position) {  

    var point = new google.maps.LatLng(position.coords.latitude, 
                                       position.coords.longitude);

    // Initialize the Google Maps API v3
    var map = new google.maps.Map(document.getElementById('map'), {
       zoom: 15,
      center: point,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    // Place a marker
    new google.maps.Marker({
      position: point,
      map: map
    });
  }); 
} 
else {
  alert('W3C Geolocation API is not available');
} 

The above will only gather the position once, and will not auto update when you start moving. To handle that, you would need to keep a reference to your marker, periodically call the getCurrentPosition() method, and move the marker to the new coordinates. The code might look something like this:

// Initialize the Google Maps API v3
var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 15,
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var marker = null;

function autoUpdate() {
  navigator.geolocation.getCurrentPosition(function(position) {  
    var newPoint = new google.maps.LatLng(position.coords.latitude, 
                                          position.coords.longitude);

    if (marker) {
      // Marker already created - Move it
      marker.setPosition(newPoint);
    }
    else {
      // Marker does not exist - Create it
      marker = new google.maps.Marker({
        position: newPoint,
        map: map
      });
    }

    // Center the map on the new position
    map.setCenter(newPoint);
  }); 

  // Call the autoUpdate() function every 5 seconds
  setTimeout(autoUpdate, 5000);
}

autoUpdate();

Now if by tracking you mean that you should also store this information on a server (so that someone else could see you moving from a remote location), then you'd have to send the points to a server-side script using AJAX.

In addition, make sure that the Google Maps API Terms of Use allow this usage, before you engage in such a project.


UPDATE: The W3C Geolocation API exposes a watchPosition() method that can be used instead of the setTimeout() mechanism we used in the above example.

Daniel Vassallo
will try it out this instant
cheesebunz
Hmmm..the phone im currently working on, samsung omnia, seems to not have the w3c geolocation installed, as it keeps popping up the message that w3c is not installed.
cheesebunz
how do you check whether the phone has it installed or not?
cheesebunz
@cheesebunz: You may want to load the following URL from your mobile device: `http://www.w3.org/2010/01/wctmb2/` and click on "Details". This will show you if the Geolocation API is available (among other features).
Daniel Vassallo
@cheesebunz: The Omnia is on Windows Mobile, right? You may want to give a look to the Iris browser as it [seems to support the Geolocation API](http://mobilitydigest.com/iris-browser-118-released/).
Daniel Vassallo
thanks for the help, found out that it doesn't have geolocation :D
cheesebunz
@cheesebunz: Note that I had a missing `new` in the `var point` point part. I tested it on an iPhone.
Daniel Vassallo
im gonna try it on other phones, if worse come to worse, i'll try the browser, thanks for being so patient with me :D
cheesebunz
by the way, iris browser doesn't seem to be available for download anymore.
cheesebunz
i dled the browser, if the company is down, will the browser still be usable?
cheesebunz
@cheesebunz: Not sure. Never tried it personally. I just read on that article that it supported the Geolocation API. I only tried my example on an iPhone with Safari.
Daniel Vassallo
On another note, is there other ways to do it without using w3c geolocation?
cheesebunz
@cheesebunz: No I don't think so. Unless you write a native application not running in the browser.
Daniel Vassallo