Google Maps Integration: HTML5

In this video tutorial we illustrate the implementation of Google Maps API.

Here we supply latitude and longitude values fetched by Geolocation API and feed it to Google Map API, which is turn returns the location on its MAP.

globe-latitude-longitude

It has 4 variations:
ROADMAP (normal, default 2D map)
SATELLITE (photographic map)
HYBRID (photographic map + roads and city names)
TERRAIN (map with mountains, rivers, etc.)

HTML File
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< !doctype html>
<html>
 <head>
  <title>Geolocation API: Technotip.com</title>
  <meta charset="utf-8"/>
  <link href="css/myStyle.css" rel="stylesheet"/>
  <script src="js/jquery-1.10.1.min.js"></script>
  <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
  <script src="js/myScript.js"></script>
 </head>
 <body>
  <p id="map"></p>
 </body>
</html>

Here we’ve added an id to paragraph tag. And included Google Map API’s javascript file within the head tag. We must include it before myScript.js, as we’ll be using Google Map API’s inside myScript.js

Javascript File: Google Map API
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function success(position)
{
	var googleLatLng = new google.maps.LatLng(position.coords.latitude, 
						  position.coords.longitude);
	var mapOtn={
zoom:10,
center   : googleLatLng,
mapTypeId: google.maps.MapTypeId.ROAD
	}
 
	var Pmap= document.getElementById("map");
 
	var map= new google.maps.Map(Pmap, mapOtn);
}

Pass position.coords.latitude and position.coords.longitude values to LatLng constructor of Google Maps API, which converts it into googleLatLng object.

Set the options of Google Maps:
zoom takes value from 0 to 21.
We center the map to our users/visitors location.
map type: ROADMAP, SATELLITE, HYBRID, TERRAIN.

Full Javascript File: Google Map API
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$(document).ready(function(){
 
	if( navigator.geolocation )
	 navigator.geolocation.getCurrentPosition(success, fail);
	else
	 $("p").html("HTML5 Not Supported");
 
});
 
function success(position)
{
	var googleLatLng = new google.maps.LatLng(position.coords.latitude, 
						  position.coords.longitude);
	var mapOtn= {
zoom:10,
		center   :googleLatLng,
		mapTypeId:google.maps.MapTypeId.ROAD
	}
 
	var Pmap= document.getElementById("map");
 
	var map= new google.maps.Map(Pmap, mapOtn);
}
 
function fail(error)
{
	var errorType = {
0: "Unknown Error",
1: "Permission denied by the user",
2: "Position of the user not available",
3: "Request timed out"
	};
 
	var errMsg = errorType[error.code];
 
	if(error.code == 0 || error.code == 2){
		errMsg = errMsg+" - "+error.message;
	}
 
	$("p").html(errMsg);
}

For explanation of other parts of above code:
Geolocation API – Success Handler: HTML5
Geolocation API – Error Handle: HTML5

Google Maps API Integration: HTML5


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=UroKHzuuve8 [Watch the Video In Full Screen.]



Note:
If you’re accessing this application via your desktop computer, then it may be showing you the location of your ISP’s(Internet Service Provider’s) local office location.
To get the accurate location, access our demo page via your smart phone.

Google Maps: Demo

Smartphone
1. Install Google Chrome browser.
2. Switch on your GPS and permit Google to access your location information, for testing purpose.
3. Visit our demo link(present above).
4. Move around and see your location information being updated in real-time.
5. We’ll teach you to make your application show real time data in coming videos, stay subscribed 🙂

Geolocation API – Error Handle: HTML5

This video tutorial teaches about error handle of Geolocation API – of HTML5.

It is a continuation of Geolocation API – Success Handler: HTML5

Cumputer-Bug-Error

This code is helpful when geolocation API is unable to determine the users location. In this situation we, as good developers, shouldn’t leave the user keep thinking about what happened! We should gracefully inform the user about what happened in the background of our application data center.

Javascript File
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function fail(error)
{
	var errorType = {
0:"Unknown Error",
1:"Permission denied by the user",
2:"Position of the user not available",
3:"Request timed out"
	};
 
	var errMsg = errorType[error.code];
 
	if(error.code == 0 || error.code == 2){
		errMsg = errMsg+" - "+error.message;
	}
 
	$("p").html(errMsg);
}

Geolocation API passes error object to the error handle(fail method), which contains error.code and corresponding error.message properties, using which we output the error information to the user.

Error code starts from 0 through 3, which we customize by giving our own, user understandable error messages, inside an object called errorType.

If the error code is 0 or 2, we’ll also output the original error message(sent by geolocation API) associated with the error code along with our own custom error message.

Full Free Code
Javascript File
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$(document).ready(function(){
 
	if( navigator.geolocation )
	 navigator.geolocation.getCurrentPosition(success, fail);
	else
	 $("p").html("HTML5 Not Supported");
 
});
 
function fail(error)
{
	var errorType = {
0: "Unknown Error",
1: "Permission denied by the user",
2: "Position of the user not available",
3: "Request timed out"
	};
 
	var errMsg = errorType[error.code];
 
	if(error.code == 0 || error.code == 2){
		errMsg = errMsg+" - "+error.message;
	}
 
	$("p").html(errMsg);
}
 
function success(position)
{
	$("p").html("Latitude: "+position.coords.latitude+
	            "<br />Longitude: "+position.coords.longitude+
				"<br />Accuracy: "+position.coords.accuracy);
}

HTML5 page(index.html) and the CSS3(myStyle.css) are same as present in our previous video tutorial.

Geolocation API – Error Handle: HTML5


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=j_5EGTb9dhA [Watch the Video In Full Screen.]



We can control “request timeout” property of our application via options(3rd parameter) parameter of getCurrentPosition() method, which we’ll look in coming videos.

Next, we’ll integrate the output of geolocation API with Google Maps API and locate the actual position of the user on a MAP!

Geolocation API – Success Handler: HTML5

In this video tutorial we’ll show you how to write your first(simple) HTML5 geolocation API application. Basics of Geolocation API is also explained in the notes below, so don’t skip, read and practice it.

Technologies included
Basic HTML5 tags
jQuery – for Javascript
CSS basics

With Geolocation API we can know our position/location on earth by fetching the latitude and longitude information from the browser.

HTML5 File
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
< !doctype html>
<html>
 <head>
  <title>Geolocation API: Technotip.com</title>
  <meta charset="utf-8"/>
  <link href="css/myStyle.css" rel="stylesheet"/>
  <script src="js/jquery-1.10.1.min.js"></script>
  <script src="js/myScript.js"></script>
 </head>
 <body>
  <p></p>
 </body>
</html>

Here we’re making use of HTML5 markup.
Know more: HTML5 – Getting Started Guide

We’re also including a jQuery library file. Make sure to include it before our custom javascript file(myScript.js), as our javascript file depends on jQuery library file.

CSS File
myStyle.css

1
2
3
4
p{
	width: 300px;
	height: 300px;
}

This is a simple Cascading Stylesheet file, which is allocating 300px width and height to the paragraph tag present in index.html file. It’ll be helpful when we integrate Google Maps into our application( in the future videos ).

Javascript File
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready(function(){
 
	if( navigator.geolocation )
	 navigator.geolocation.getCurrentPosition(success);
	else
	 $("p").html("HTML5 Not Supported");
 
});
 
function success(position)
{
	$("p").html("Latitude: "+position.coords.latitude+
	            "<br />Longitude: "+position.coords.longitude+
				"<br />Accuracy: "+position.coords.accuracy);
}

This is a minimalistic approach to using geolocation API. We’ll improve it in future videos.

Once the document is loaded and ready, we check if the browser supports Geolocation API.
We do that by checking if navigator.geolocation object exists.
If it doesn’t exist, we display appropriate message to the user(“HTML5 Not Supported”).

If it does support navigator.geolocation, we’ll make use of it’s methods and get the user location.
navigator.geolocation has 3 methods:
getCurrentPosition
watchPosition
clearWatch

In this video tutorial we’ll be looking at the first method, i.e., getCurrentPosition()

navigator.geolocation.getCurrentPosition(success, error, option);

getCurrentPosition()
It takes 3 parameters:
success handler
error handler
and options.

success handler is mandatory.
error handler and options are optional.

getCurrentPosition method passes the users current location information to the success handler, in the form of position object.

position object: success handler method

position object has two properies: coords and timestamp

coords(coordinates) inturn has 7 properties:
3 are supported by most browsers:
latitude
longitude
accuracy

..other 4 are not widely supported:
altitude
altitudeAccuracy
heading
speed

In this tutorial we’re using latitude, longitude and accuracy properties, as it’s supported by most browsers.

Geolocation API – Success Handler: HTML5


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=a3-ekroQoHo [Watch the Video In Full Screen.]



Note:
1. Geolocation API returns latitude and longitude values in decimal number system.
2. It determines users location by using various factors like: IP Address, GPS, Cell phone triangulation, WiFi etc.
3. We can’t know or specify which method it has to use to determine the users location.
4. If you’re getting 404 or googleapi not found, [object][object] etc error messages, then reload your webpage after you restart your system and/or your WiFi modem.
Make sure your WiFi modem is ON: because it’s one of the way Geolocation API will fetch your location from.

Missing things:
Error handling.
Options, to configure maps output. etc
Showing the location of the user on a MAP.
Showing real time movement of the user on a MAP.
All these will be covered in future videos. Stay Subscribed. Share this video with your friends.

HTML5 – Getting Started Guide

This video tutorial teaches how to start with HTML5.
Some basic changes that you need to know before starting to learn HTML5.

Technologies involved to learn HTML5:
Javascript API.
CSS3.
HTML5.

html5-features

Some of the things we’ll be learning throughout this course:
New Elements
New Attributes
CSS3 Selectors
Rounded Corners
Shadow Effects
Video and Audio
2D/3D Graphics
Local Storage
offline caching
web workers
Drag and Drop
Geolocation
Working with Google Maps API.
Forms
Sockets
Canvas ..etc

HTML5 File
index.html

1
2
3
4
5
6
7
8
9
10
< !doctype html>
<html>
 <head>
  <title>HTML5 Tutorials: from Technotip</title>
  <meta charset="UTF-8"/>
 </head>
 <body>
  <p>This is a video tutorial sponsored by or produced by Technotip.com</p>
 </body>
</html>

Doctype: no more Googling or copy pasting or relying on your fancy text editors to get the clumsy looking doctypes. With HTML5, the doctype has been simplified and is easy to remember.

Doctypes for HTML6, HTML7 ?
The doctype will stay the same for HTML5, HTML6, HTML7, HTML8, HTML9, HTML10, HTML11, HTML12 etc 🙂 This is a promise from W3C.

Previous HTML versions(like xHTML, HTML4, HTML4.0.1 etc) depended on SGML, but HTML5 and onwards it doesn’t depend on SGML. When the browser parses the first line(i.e., the doctype of HTML5), it’ll execute in HTML5 standards mode.

By using a doctype the browser is able to be more precise in the way it interprets and renders your pages. If you don’t specify any doctype, it still works, but it may cause the browser to render things wrongly. And it may take more time for the browser to fail gracefully, after trying to figureout all possible ways to parse the tags and to try its best to present your web pages properly.

Meta tag
It has also been simplified and now, only character set information is enough.

CSS – Cascading Stylesheet

1
 <link rel="stylesheet" href="style.css"/>

In HTML5 there is no need to write the type attribute with link tag.
Because CSS has been declared as the standard and default style for HTML5.

JavaScript

1
 <script src="myScript.js"></script>

In HTML5 there is no need to write the type attribute with script tag.
Because JavaScript has been declared as the standard and default scripting language for HTML5.

Embeded JavaScript

1
2
3
 <script>
	var name = "Technotip.com";
 </script>

If you’re writing embeded javascript, then you can simply start with a opening script tag and a closing script tag. Here too, you need not specify the type attribute.

Introduction To HTML5


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=cBW5PUiAG7s [Watch the Video In Full Screen.]



Upgrade your web pages to HTML5
To upgrade your web pages to HTML5 standards, simply make these small changes:
1. doctype.
2. remove type attribute from link and script tags.
3. remove and if needed replace deprecated tags(will discuss in coming video tutorials) from your website.

Use validator.w3.org tool to check your webpage.

CSS Hover Over Effect On HTML Table: pseudo class

In this tutorial we are illustrating the css hover effect using html4 table and simple css coding.

The CSS pseudo class which is used to accomplish hover over effect is:

  :hover

HTML4 Table Syntax(table.html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<html>
 <head>
  <title>Hover Over On Table</title>
  <link type="text/css" href="style.css" rel="stylesheet">
 </head>
 <body>
	<table border="1">
		<thead>
			<td>Apple</td>
			<td>Microsoft</td>
			<td>Google</td>
		</thead>
		<tfoot>
			<td>4</td>
			<td>3</td>
			<td>6</td>
		</tfoot>
		<tbody>
			<tr>
				<td>2</td>
				<td>2</td>
				<td>3</td>
			</tr>
			<tr>
				<td>2</td>
				<td>1</td>
				<td>2</td>
			</tr>
		</tbody>
	</table>
 </body>
</html>

[If you are used to old stay table syntax, then it’s completely OK to use this CSS Hover over technique on it. It’s not mandatory to use HTML4. ]
In HTML4, table mainly contains 3 parts. thead, tfoot and tbody.

table-html4-syntax

thead represents the heading and the tfoot, the footer of the table. tbody represents the body except the head and the foot of the table.
In thead, I have taken “Apple”, “Microsoft” and “Google”.
In tfoot and tbody some random numbers for the purpose of illustration.

Just don’t try worry about these numbers, instead concentrate on the structure of the table.

  <link type="text/css" href="style.css" rel="stylesheet">

This line is very important, since it connects the HTML file and the CSS file.
This type of StyleSheet is called as external stylesheet.

Styling information of the HTML document. (style.css)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
body {
	font-family: verdana;
	font-size: 24px;
}
 
table {
	width: 400px;
}
 
thead { font-weight: bold; }
tfoot  { font-weight: bold; }
 
thead:hover { 
	background-color: red; 
	color: blue;
}
 
tfoot:hover {
	background-color: yellow; 
	color: blue;
}
 
tbody tr:hover {
	background-color: green; 
	color: blue;
}

Cascading Style Sheet
Here we apply all the styling property to the HTML file.

Make sure you try this :hover pseudo class with other html elements too. You can create very good user interface with the help of this simple CSS hover over effect.

Video Tutorial



YouTube Link: http://www.youtube.com/watch?v=BAk9qYQyBlY [Watch the Video In Full Screen.]



Please watch the above video tutorial in full screen.

Related:
You may also like: CSS Hover Over Effect on Lists

Named Anchors In HTML – For Internal Linking

Often times we spend a lot of time and end up preparing a lengthy document thinking it will be helpful, but as a matter of fact people can’t scroll up and down to find what they are looking for – instead they will goto Google and search for some other sites.

In this situation “Named Anchors” are very helpful. This simple technique will make your miles long document more usable.

Here is a working example of Named Anchor: Clicking on Take Me To The End Of This Article will take you to the end of this article.

Here is how it works:
Imagine that you have a premium software and you have written a manual for Windows, Apple and Unix users on a single page:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<html>
<head><title>My Software Manual</title>
</head>
<body>
 
<i>Index</i>
<a href="#microsoft">Windows 7 Manual</a>
<a href="#apple">Macintosh OS X Manual</a>
<a href="#unix">Unix/Linux Manual</a>
 
 
<i>Content</i>
1. <a name="microsoft">Windows 7</a>:  
Here you may write about the system requirement
for your software to work on Windows 7. 
And the amount of resources that your application 
is going to consume. Some tips and tricks. 
    Imagine its about 50 lines.
 
 
2. <a name="apple">Macintosh OS X</a>:  
Imagine there is about 500 lines of documentation 
about using your application on a Apple Macintosh.
 
3. <a name="unix">Unix/Linux</a>:   
Imagine there is about 1000 lines of documentation 
about using your application on a Apple Macintosh.
 
</body>
</html>

These lines of code(from about spinet) act as index.

1
2
3
<a href="#microsoft">Windows 7 Manual</a>
<a href="#apple">Macintosh OS X Manual</a>
<a href="#unix">Unix/Linux Manual</a>

When people click on Windows 7 Manual link in the index section, it takes them directly to the 1. Windows 7 paragraph(in the content section) without even reloading the page – as all the information present in that page has already been loaded.



Make sure you check this working example:
You can find a link at the top of this article – “Take Me To The End Of This Article”, clicking which you will end up here!