Darren Ferguson - Blog

14 March 2009 at 09:36

Quick tip: Displaying maps on your Umbraco website

Tags: Google , Robin Christopherson , Liverpool , United Kingdom , Javascript , API
Author: Darren Ferguson

I've had a few requests to demonstrate how to show maps on your Umbraco based site using my Map/Place datatype.

The location you specify is stored as a latitude/longitude point with a Google maps specific zoom level. There are a number of different ways to display maps, but here is the method I used in my demo screen cast.

In your Umbraco template display the value of your map field:

<div id="map1" class="map">
<umbraco:Item field="place1" runat="server"></umbraco:Item>
</div>

Your template should include jQuery, the google maps API Javascript and the following javascript:

if (typeof ItemEditing == 'undefined') {
	$('div[class=map]').each(function() {

		$(this).addClass('mapdimensions');
		var mapId = $(this).attr('id');
		
		var value = $(this).html();
		value = $.trim(value);
		
		var point = value.split(',');

		var lat = parseFloat(point[0]);
		var lon = parseFloat(point[1]);
		var zoom = parseFloat(point[2]);
		
		fm.maps[fm.maps.length] = new GMap2(document.getElementById(mapId));
		var m = fm.maps[fm.maps.length-1];
		
		var p = new GLatLng(lat, lon);
		m.setCenter(p, zoom); 
			

		var marker = new GMarker(p);
		m.addOverlay(marker);	
	});

	......

Note that the test for the existence of the ItemEditing is checking whether Canvas is present or not. I'm not sure whether this is the best way to do this. Also note that I add a class mapdimensions to the element containing the map. You should define this CSS class with width and height properties to specify the dimension of your map.

If you want to display a static map you can use something like the following:

$('div[class=staticmap]').each(function() {

		$(this).addClass('mapdimensions');
		var mapId = $(this).attr('id');
		
		var value = $(this).html();
		value = $.trim(value);
		
		var point = value.split(',');

		var lat = parseFloat(point[0]);
		var lon = parseFloat(point[1]);
		var zoom = parseFloat(point[2]);

		var apiKey = '.....';
		
		var imgSrc = 'http://maps.google.com/staticmap?';
		imgSrc += 'center='+lat+','+lon;
		imgSrc += '&zoom=' + zoom;
		imgSrc += '&markers='+lat+','+lon+',blues';
		imgSrc += '&size=400x400';
		imgSrc += '&key='+apiKey;
			
		var imgTag = '<img height="400" alt="map" src="'+imgSrc+'" width="400">';
		$(this).html(imgTag);
	});

You should probably display a static map in an unobtrusive fashion (not using Javascript) and then replace it with a dynamic map if Javacsript is enabled. The example above are just to get you started and are by no means best practice.

Finally if you are displaying a static Google map using the img tag you should display an alt tag detailing what the map is e.g.

A map of Anfield Road in Liverpool, England. Latitude 50.17843 Longitude 0.1232

I consider this *very* important. I was recently party to a presentation given by Robin Christopherson of AbilityNet. Robin is blind and gave his presentation using a screen reader. I'd always imagined how small things like poorly written alt tags could make life difficult for impaired users, I now realise it makes life *impossible*.

Depending on interest there will be more information to come on maps, including how to display multiple markers on a map.

Written by: Darren Ferguson

Comments

  1. David Conlisk says:

    Gravatar of David ConliskDarren - I am definitely interested in hearing more. Keep up the good work. Clients LOVE this kind of ease-of-use on a site, and the seamless Umbraco integration is priceless!

  2. Tom Bishop says:

    Gravatar of Tom BishopHey man, Great Work! I am getting a javascript error stating that fm is undefined using your example. Any ideas of where I should define this?

  3. Darren Ferguson says:

    Gravatar of Darren Ferguson@Tom - add this to the top of your script.

    var fm = {};
    fm.maps = new Array();

  4. Tommy Skaue says:

    Gravatar of Tommy SkaueGreat stuff - nicely done! :-D

  5. Sander says:

    Gravatar of SanderDarren,
    very nice package do you have here
    altough i might have a small issue
    when doing the javascript call to google in the head. (the include)
    you have to insert your API key again.
    while it's already configured in the place.config file...

    do i really have to store this key in 2 locations?


    other than that, great easy to use package!

  6. Chris Houston says:

    Gravatar of Chris HoustonHi Darren,

    I have just installed your Google Map doc type and it installed seamlessly, so well done :)

    Do you have an example of how to show multiple items on a single map? I have a collection of point which I would like to display on a single page and then if the user clicks the marker I need it needs to change the content of a DIV on the page (probably using AJAX) If you have an example of multiple node usage that would be a great step in the right direction!

    Cheers,

    Chris

  7. Darren Ferguson (1) says:

    Gravatar of Darren Ferguson (1)@chris - glad it is of use. I'll drop you a mail re multiple markers.

  8. Aron Gamble says:

    Gravatar of Aron GambleHi

    I'm trying to show a map in a site but can only see the co-ordinates. I've added the jQuery, Google script and the javascript above in a script block. I've also created the mapdimensions class but still no joy. Any suggestions please - this will be perfect for us..

    Cheers

    Aron

  9. Aron Gamble (1) says:

    Gravatar of Aron Gamble (1)I've sorted it - great package!
    Thanks

    Aron

  10. Lars Skovgaard says:

    Gravatar of Lars SkovgaardHi

    I'm in the same boat as Aron – I'm trying to show a map in a site, but can only see the coordinates. I've also added jquery, the Google API script and the javascript above to my main template, and added the mapdimensions class to the sites main css-file. I'm new to Umbraco and without any javascript-skills, and unlike Aron I can't seem to find the right combination.

    Hope you can help!

    Best regards,
    Lars

  11. Darren Ferguson (2) says:

    Gravatar of Darren Ferguson (2)@lars - I'd need you to send me a link to your page to see what is wrong.

  12. Lars Skovgaard (1) says:

    Gravatar of Lars Skovgaard (1)Hi Darren

    Thanks for taking the time! ...I've currently hidden the page I'm testing from the main menu, so you'll need to use this direct link:

    http://www.cafax.dk/forhandler-test.aspx

    Thanks,
    Lars

  13. Darren Ferguson (3) says:

    Gravatar of Darren Ferguson (3)@lars - suggest you wrap your javascript up in a jquery document ready event.

  14. Thomas Kahn says:

    Gravatar of Thomas KahnA simpler way of saying "$('div[class=map]')" in jQuery would be "$('div.map').

  15. Lars Skovgaard (2) says:

    Gravatar of Lars Skovgaard (2)Hi Darren

    I've wrapped the javascript up i a jquery document ready event (at least I think I have), and have tried every combination I could think of, but without luck. I've used the Developer Tools in Safari to check for errors, and it throws a "Parse error".

    I will be immensely grateful if you could be persuaded to take one more look at the sourcecode – I'm certain that I'm making a mistake somewhere, but I can't find it.

    /Lars

  16. Lars Skovgaard (3) says:

    Gravatar of Lars Skovgaard (3)Hi again, Darren

    I've been hacking a little more at it and gotten as far as the Javascript console telling me that "GMap2 is not defined". Any clues?

    /Lars

  17. Darren Ferguson (4) says:

    Gravatar of Darren Ferguson (4)You need to include the google maps API in your page.

  18. Marc Clifton says:

    Gravatar of Marc CliftonHi Darren, I'm interested in adding a map to the contact page of my neighbor's farm: www.cowberrycrossingfarm.com, and I've read through the posts here, but I frankly have no idea what you're all talking about, jQuery, document ready event, etc.

    Would it be possible to email me a complete example, or is there some doc somewhere that I haven't seen yet?

    Thanks!

    Marc

  19. Ruben Verschueren says:

    Gravatar of Ruben Verschuerenthis really is super helpfull!

    I've got one suggestion, should you make a new version (unless it already possible). can you increase the size of the map shown in the datatype/content node?

    You see, at the moment I display the google map at 800x300 dimensions on the frontend, which shows more of the map than the content map does. So i'm looking for a way to determine the center of the map (or where the flag is shown).

    anyway, great work!

  20. Patrick Rietveld says:

    Gravatar of Patrick RietveldHi Marc, you can view the source of my contact page to build youre own. My contact page is based on the example of Darren. http://www.maxelmo.nl/contact for those who are interested, I could make an Umbraco package with a complete implementation.

  21. heta says:

    Gravatar of hetafrom where I will get the jquery specified?

  22. Scott Leaver says:

    Gravatar of Scott LeaverHi Darren, we have used your Google Map package, and it's great. Can you tell me, or point me to info showing how we might be able to extend it to add info to the main pin and also add extra info/location pins to the map. The map I am looking at is at: http://greenlight.dev.crumpled-dog.net/contact-us/

    Thanks
    Scott

  23. codex says:

    Gravatar of codexYour plugin is great! I am using it with google maps api v3 with a few modifications to the code you suggest:

    <script type="text/javascript">

    $('div[class=map]').each(function() {



    $(this).addClass('mapdimensions');
    var mapId = $(this).attr('id');

    var value = $(this).html();
    value = $.trim(value);

    var point = value.split(',');

    var lat = parseFloat(point[0]);
    var lon = parseFloat(point[1]);
    var zoom = parseFloat(point[2]);

    var p = new google.maps.LatLng(lat, lon);

    var myOptions = {
    center: p,
    zoom: zoom,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    fm = new google.maps.Map(document.getElementById(mapId),
    myOptions);

    var marker = new google.maps.Marker({
    position: p,
    title:"We are here!",
    animation: google.maps.Animation.BOUNCE
    });


    // To add the marker to the map, call setMap();
    marker.setMap(fm);

    });



    </script>

  24. John says:

    Gravatar of JohnGreat job! But, i have a question. There is way in this module to highlight the area in google map?

    Thanks your answer:)

Leave a comment