A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Monday, February 20, 2012

HTML5 Geolocation and SharePoint 2010

Let's see how we can take advantage of HTML5

Geolocation feature in SharePoint. Geolocation allows you to find the location of your site visitors. There can be many uses of this feature. For example, you can use it to gather statistics for your site. You can then create a graph or chart widget and show countries that visited your site or blog the most. There are endless possibilities. You can also show cities. It can really come in handy if you run a business on the net. This thing also works in mobile devices. Looking at the number of mobile applications that are sprouting everyday, this feature can really play a pivotal role in gathering geographic data of your visitors. It will directly impact your sales and marketing campaigns. Plus point is that this feature has good support in every browser. Well if you are thinking about IE 7 and IE 8, forget it. You need IE 9 for this thing to work but that's for computers. For devices that use non-IE browsers, this isn't a problem. Almost all browsers support this feature. If you are afraid that this thing will not work in SharePoint, let me tell you that it will work. All you need is IE9 browser. If you don't have IE 9, start using Chrome or Firefox.


They work well with SharePoint 2010 and also support other standards that are popular today. This includes HTML5 technology.


If you want to learn how to prepare SharePoint for HTML5 usage, read following article:


HTML5 Video and SharePoint 2010


Jump to section "Using HTML5 in SharePoint 2010". You will find all the details in that section.

Once you are ready, start coding the HTML5 geolocation page.

Using Geolocation is very easy. Check out the following code:



navigator.geolocation.getCurrentPosition(
function(position)
{
alert("Your
location is: " + position.coords.latitude + ", " +
position.coords.longitude);
}<BR>);












We get latitude and longitude coords to find the

position of the user. OK, now we know how to find the location. Next goal is to show this location on the map. We will use Google Maps for this purpose. Again, it's not difficult to use Google Maps. If you haven't used Google Maps before, you may want to check out the documentation online at http://code.google.com/apis/maps/documentation/javascript. You have to include a Google Map link in your code before you can actually use this service. Add following line between the <head> ... </head> tags.




<head><br><title>HTML5Geolocation</title><br><scriptsrc="http://maps.google.com/maps/api/js?sensor=true"></script>
...<
BR>
</head>








Add a div tag to the page. This will be used to draw the map.



<body>
<div
id="mapCanvas"></div>







Use following style to design the map layout:



#mapCanvas
{
width: 650px;
height:
450px;
border: solid
1pxblue;
<BR>}











Now, you are ready to see everything in action. Call following function when the window loads:



window.onload = function() {
results =
document.getElementById("results");
var
myOptions = {
zoom: 13,

mapTypeId: google.maps.MapTypeId.ROADMAP
};










This code writes results in the "results" div. Variable myOptions contains options like zoom level and the map type. Map type that we are going to use is ROADMAP. After setting the options,
create the map:



map = new
google.maps.Map(document.getElementById("mapCanvas"),
myOptions);






Following code will set user's location on the
map:



function geolocationSuccess(position)
{


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

map.setCenter(location);


var infowindow = new
google.maps.InfoWindow();



infowindow.setContent("This is your location on the
map!");

infowindow.setPosition(location);

infowindow.open(map);


results.innerHTML = "You are on the
map.";
}





























Variable location contains the exact location.

Then we show a box on the map with a message. We use InfoWindow() function to show the box. This box will appear at user's location. The following function will be called in case of failure:




function
geolocationFailure(positionError) {
if (positionError.code
== 1) {
results.innerHTML =

"You have decided not to share your
location.";
}
else if (positionError.code == 2)
{
results.innerHTML
=
"The network is down or the
positioning service can't be reached.";
}
else
if (positionError.code == 3) {

results.innerHTML =
"The attempt timed
out before it could get the location data.";
}

else {
results.innerHTML
=
"Unknown error. Please try
later.";
}


}


























In geolocationFailure() function we check different conditions to know the cause of failure and then show message accordingly. That's it.

Click here to download complete code (html file in zip
format)

Now you have the code. All you have to do is use it inside a SharePoint page. This has been demostrated in the following article so I am not going to repeat the steps again.

HTML5 Video and SharePoint 2010

Best way of running HTML5 code on a SharePoint page is to use a content editor web part and then access the URL of the HTML5 page inside the web part. You need to host the page in IIS first. After you have configured the content editor web part, open the SharePoint page in browser.

This is how the map will look like on your page:

Figure 1: Google Map with
Geolocation

And this is how it will look on your mobile:

Figure 2: Permission to allow device to use your current location

After you click OK, you see the map.

Figure 3: Google Map on your device using Geolocation

If you want to see your location in the browser, open following link in your browser:

http://walisystemsinc.com/sites/html5/GeolocationMap.html

Stay tuned! Next article will show you how to use Geolocation to find Twitter users in your area.

1 comment:

  1. Hi,

    Thanks for your post!
    It works fine on Firefox, Chrome, Safari... but I encountered some issues with IE9.

    If I try directly your link "http://www.walisystemsinc.com/sharepoint/art/html5_p2/HTML5_Video_and_SharePoint2010.htm" in IE9 browser, it works fine : IE9 prompts me to share my current location, I allow it and I can see the map with my current location.

    Nevertheless, if I try your code into a SharePoint Page (both with a Content Editor Web Part linked to http://walisystemsinc.com/sites/html5/GeolocationMap.html and by adding the downloaded complete code into the Content Editor Web Part), IE9 does not prompt me to share my current location, and I can only see the following error message "This browser doesn't support geolocation.". Conversely, Firefox, Chrome, Safari prompt me and it works as expected with these browsers.

    I point out that I did the modifications needed in the master page, to prepare SharePoint 2010 for HTML5 usage (according to your post "http://www.walisystemsinc.com/sharepoint/art/html5_p2/HTML5_Video_and_SharePoint2010.htm").

    Tell me what I forgot please...

    ReplyDelete