In this video tutorial we shall teach you how to add a message window(popup) when the pin or the marker is clicked on a Google Map.
JavaScript file mySript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function addMarker(map, googleLatLng, title, content){
var markerOptn={
position:googleLatLng,
map:map,
title:title,
animation:google.maps.Animation.DROP
};
var marker=new google.maps.Marker(markerOptn);
var infoWindow=new google.maps.InfoWindow({ content: content,
position: googleLatLng});
google.maps.event.addListener(marker, "click", function(){
infoWindow.open(map);
});
}
function addMarker(map, googleLatLng, title, content){
var markerOptn={
position:googleLatLng,
map:map,
title:title,
animation:google.maps.Animation.DROP
};
var marker=new google.maps.Marker(markerOptn);
var infoWindow=new google.maps.InfoWindow({ content: content, position: googleLatLng}); google.maps.event.addListener(marker, "click", function(){
infoWindow.open(map);
});
}
Add the content and position information to InfoWindow constructor of Google Maps API. Also add a event listener: once the marker is clicked, open the message window on the map.
$(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);
addMarker(map, googleLatLng, "Technotip.com",
"SATISH B About Me:https://technotip.com/about/");
}
function addMarker(map, googleLatLng, title, content){
var markerOptn={
position:googleLatLng,
map:map,
title:title,
animation:google.maps.Animation.DROP
};
var marker=new google.maps.Marker(markerOptn);
var infoWindow=new google.maps.InfoWindow({ content: content,
position: googleLatLng});
google.maps.event.addListener(marker, "click", function(){
infoWindow.open(map);
});
}
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);
}
$(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);
addMarker(map, googleLatLng, "Technotip.com", "SATISH B About Me:https://technotip.com/about/");
}
function addMarker(map, googleLatLng, title, content){
var markerOptn={
position:googleLatLng,
map:map,
title:title,
animation:google.maps.Animation.DROP
};
var marker=new google.maps.Marker(markerOptn);
var infoWindow=new google.maps.InfoWindow({ content: content, position: googleLatLng}); google.maps.event.addListener(marker, "click", function(){
infoWindow.open(map);
});
}
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 above code, please visit previous day videos.
Add Popup message To Google Map Pin/Marker Click Event: HTML5
If you’re using a smart phone or any other GPS device, you can get to your exact location more accurately. To pin point your exact location, we can add a pin or the marker to your location.
Javascript File myScript.js
1
2
3
4
5
6
7
8
9
10
function addMarker(map, googleLatLng, title){
var markerOptn={
position : googleLatLng,
map:map,
title:title,
animation : google.maps.Animation.DROP
};
var marker = new google.maps.Marker(markerOptn);
}
function addMarker(map, googleLatLng, title){
var markerOptn={
position : googleLatLng,
map:map,
title:title,
animation : google.maps.Animation.DROP
};
var marker = new google.maps.Marker(markerOptn);
}
We do not generate map again. Since we already have it generated in success method, we’ll simply use the same map. We’ll get the position information from HTML5’s Geolocation API, convert it into googleLatLng object and pass to addMarker method. We also set some options for the marker, like the position, title and the animation. We could have two type of simple animation using properties BOUNCE and DROP. Finally pass the marker option to Marker constructor of google maps api. This would bring up the map with a nice little marker.
$(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);
addMarker(map, googleLatLng, "Technotip.com");
}
function addMarker(map, googleLatLng, title){
var markerOptn={
position: googleLatLng,
map:map,
title:title,
animation:google.maps.Animation.DROP
};
var marker = new google.maps.Marker(markerOptn);
}
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);
}
$(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);
addMarker(map, googleLatLng, "Technotip.com");
}
function addMarker(map, googleLatLng, title){
var markerOptn={
position: googleLatLng,
map:map,
title:title,
animation:google.maps.Animation.DROP
};
var marker = new google.maps.Marker(markerOptn);
}
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);
}
Please look at previous day videos to understand above code fully.
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.
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>
Geolocation API: Technotip.com
< !doctype html>
Geolocation API: Technotip.com
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);
}
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.
$(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);
}
$(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);
}
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.
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 :-)
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);
}
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.
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!
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.
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;
}
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 ).
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()
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.