Tag Archives: Javascript

Ajax with Golang

Lately, I thought of trying my hands on the classic Ajax alongside Go language. The requirement is to write a simple web server which sends ‘current timestamp’ to the requester every second. I have written a small program to do exactly the same. The Ajax part is taken care by jquery.

In order to see it in action, one needs to run it “go run tick-tock.go”. When the program is running, it essentially becomes a tiny web server listening to requests at port ‘9999’.

Now all you need to do is fire up a browser and raise a request to the tick-tock server: http://localhost:9999/time

Go is awesome. Isn’t it?

Locating current position on map

HTML5 introduced new set of specifications which include geolocation APIs. Using them one can find the location of a device. While exploring it further, I found a simple Javascript program that neatly demonstrates the overall idea (references (i)). The following code has been modified a bit from the original one to make it more verbose.

  if(navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(

      // success callback, where 'position' holds the found coordinates.
      function(position) {
      // Instantiate a Google Map's LatLng object using the position coordinates.
      initialLocation = new google.maps.LatLng(position.coords.latitude,
                                               position.coords.longitude);
      /*..
        Now 'initialLocation' can be used to place a marker on the map or
        other coordinates related stuff.
      ..*/
      },

      // error callback, where 'positionError' holds various error related
      // attributes (see reference (ii) for details).
      function(positionError) {
      // log the error message
      console.log("Geolocation service failed. MSG: " + positionError.message);
      },
      {maximumAge:Infinity, timeout:10000});
  }

  // Browser doesn't support Geolocation
  else {
    console.log("Your browser doesn't support geolocation.");
  }
}

References:
i) https://developers.google.com/maps/articles/geolocation
ii) http://dev.w3.org/geo/api/spec-source.html