Wednesday, December 09, 2009

10 Minute Guide to Google Maps

Map-based applications are becoming parts of our digital life. Visualizations of travel data, current news events, graphical presentation of weather reports, historical sites, concert locations, public transportation, corporate offices, and real estate sites through maps are very frequently used applications now-a-days. It is extremely simple to add map support to your web site. I have compiled here a 10 min guide for a map with a simple balloon marker on the location based on your IP.

Prerequisites: You need very basic knowledge of the following:
  1. HTML
  2. JavaScript

Step 1. Create Page: Create a new HTML page in your favorite text editor or HTML page. The page will roughly contain the following text (You can better copy these lines to a text file and save it with .htm or .html extension):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>My Location</title>
</head>
<body>

</body>
</html>

2 min.
Step 2. Add Canvas:  Add a <div> tag inside the <body> where the map will be displayed and give it an id (e.g. canvas in the following example) 

<body>
<div id="canvas" style="height: 400px; width: 400px;"></div>
</body>

2 min.
Step3. Reference maps API: Add the following <script> tag just under <title> tag. This line will set a reference to JavaScript API libraries provided by google.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
1 min.

Step4. Create Map: Load API library and initialize the map in a <script> tag inside <head> tag (or copy & paste the following lines of code).

<script type="text/javascript">

function initialize() {
    /* give a default zoom level*/
    var zoom = 8;
    /*default center of the map*/
    var latlng = new google.maps.LatLng(49.008054, 8.388969);

   /* get the your location with geocoding */
   var location;
    if (google.loader.ClientLocation) {
      latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
     /* Build a text of the location to display */
     location = "Latitude: " + google.loader.ClientLocation.latitude + "<br/>";
     location += "Longitude: " + google.loader.ClientLocation.longitude + "<br/>";
     location +=  "City: " + google.loader.ClientLocation.address.city + "<br/>";
     if (google.loader.ClientLocation.address.region)
         location += "Region: " + google.loader.ClientLocation.address.region + "<br/>";
     location += "Country: " + google.loader.ClientLocation.address.country;
    }

    /* Options for the map*/
    var myOptions = {
      zoom: zoom,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    /* Create the map on the canvas */
    var map = new google.maps.Map(document.getElementById("canvas"), myOptions);

   /* Create an info window to display the location text */
   var infowindow = new google.maps.InfoWindow({
        content: location
    });

   /* Create a default balloon marker at your location */
   var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title:"My Location"
    });
 
    /* Display the location text when marker is clicked */
google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  }
</script>
5 min.

Browse and download the complete HTML page from here. Click the balloon on the following map to see your current location.






There are a lot of tutorials, samples and demos available at the Google Maps API web site.

Monday, December 07, 2009

Basic/Primitive Types


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

In a previous post Identifiers, I have discussed identifiers that we can write in the source. Type restricts the identifier to size & kind of data and in addition, the operations that can be performed on that data. Both Java and C# support almost all basic data types of C++ but change characteristics of some. C++/Java/C# are strongly typed languages meaning each object must have a predefined data type.

Type Modifier:
C++ has 7 basic data types. They are int, float, double, bool, char, wchar_t, void. It uses four type modifiers to restrict the signedness and size of data that can be stored on a data type identifier -
  1. signed: Defines a positive (+ve) or negative (-ve) type. All types are implicitly positive.
  2. unsigned: A positive (+ve) type. Variable of this type can't hold negative values.
  3. short: Types are implicitly short if not otherwise specified.
  4. long: Extends the size of basic type to holder larger value.
.Net programs are compiled into Microsoft Intermediate Language (MSIL). Similar to Java Byte Code, it is  independent of hardware architecture and is translated to machine code during or before execution. But unlike Java, it brings a common type system (CTS) which means the actual program can be written in arbitrary language which can compile for .Net and the respective compiler is responsible for translating it to MSIL. MSIL defines a set of data types and each .Net compatible compiler must translate its own data types to the corresponding data types in MSIL. In other words, each data type in C# resembles a data type in MSIL.

In Java and C#, the primitive types are value types (more in Value Type vs. Reference Type and Boxing vs. Unboxing) which are allocated in the stack and copied during assignment. There are corresponding reference types in both the languages with implicit conversion from each other. The boxed types of Java are listed in "Java Boxed" and that of C# are in ".Net Type" columns.

Following is a list of the primitive data types in these languages. The sign "-" means the type is not directly supported by some particular language and the type in bracket beside it shows what can be used as a replacement.


C++
Java
Java Boxed
C#
.Net
- (unsigned char)
- (byte)
- (Byte)
byte (8)
Byte
- (signed char)
byte
Byte
sbyte (8)
SByte
int
int(32)
Integer
int (32)
Int32
unsigned int
-
-
uint (32)
UInt32
short
(short int)
short
Short
short (16)
Int16
unsigned short
-
-
ushort (16)
UInt16
long (long int)
long (64)
Long
long (64)
Int64
unsigned long
-
-
ulong (64)
UInt64
float
float
Float
float (32)
Single
double
double
Double
double (64)
Double
wchar_t
char
Character
char (16)
Char
bool
boolean
Boolean
bool (8)
Boolean
- (void*)
Object
Object
object
Object
-
-
decimal (128)
Decimal
- (std::wstring)
java.lang.String
java.lang.String
string
String

Type Qualifiers

Further keywords are used to guarantee data safety or specify data vulnerability. const

C++
Java
C#
.Net
const
final
const
-
-
-
readonly
-
volatile
-
volatile
-

Storage Class Specifiers
Further keywords are used to guarantee data safety or to specify data vulnerability.

C++
Java
C#
.Net
auto
-
-
-
register
-
-
-
static
-
-
-
extern
-
extern
-

Variables

A variable has a name and we keep some information in it. The information can be altered afterward, hence the name variable. C++/Java/C# are strongly typed languages which means each variable has a defined type i.e. you cannot assign value of a type to a variable of another type.

int textLength = 0; //defines a variable of integer type
float average = 1.0; // defines a variable of float type 

Function (aka method/routine/subroutine/operation/procedure) Names

A function or method defines a particular responsibility of the program. It has a return type and a set of parameters.

/*Add two integers and return the result*/
int
 doSum(int x, int y)
{

    return
 x + y;
}