CodeGarden 10: The sixth annual Umbraco Developer Conference
June 23-25th 2010 - free ASP.NET MVC pre-conference. Register today!

can't get googlemap to display on page

1/29/2010 11:02:54 AMAvatartrfletchLocation: Rochester, UKposts: 334Karma: 289

Hi,

I am using Darren Ferguson's Google map datatype but I am only getting the coordinates displayed on the page. I have the following scripts referenced in my master template:

<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAqKXxKQCv8T9beBgr7Qn7mBRZ8s8Y-ry6aFfAhHbHnw1KqI_47hShizyCiWXuth8iL3wk_kw9MuPvKw"></script>
<script type="text/javascript">
  google.load("maps", "2");
</script>
<script src="/Scripts/showgooglemap.js" type="text/javascript"></script>
<script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

The showgooglemap.js contains the following code:

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); 
 });
}

I have also added the following on my page:

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

And the following in my CSS sheet:

.map {
 width:200px;
 height:200px;
}

But I am getting the following javascript error on line 2 of the showgooglemap.js :

Uncaught ReferenceError: $ is not defined

Can anyone see where I am going wrong?

1/29/2010 11:21:27 AMAvatarDanLocation: bristol, ukposts: 254Karma: 319
Comment with ID: 24775

Have you got jQuery installed and referenced in your HTML?

1/29/2010 11:37:15 AMAvatarianhoughtonLocation: Hinckleyposts: 18Karma: 46
Comment with ID: 24777

I would agree with Dan, it sounds like jquery is missing. I've had this before.

1/29/2010 11:57:20 AMAvatartrfletchLocation: Rochester, UKposts: 334Karma: 289
Comment with ID: 24780

Thanks for the responses, please bare with me because I am not all that experienced with jquery but is that not what the following code is:

<script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

And I have put the jquery-1.4.1.min.js file I downloaded from the Jquery website in my scripts folder.

1/29/2010 12:20:18 PMAvatarDarren FergusonLocation: SE3 9EFVendor.posts: 376Karma: 533
Comment with ID: 24782

Move the include of jquery so it is before the include of your own JS.

Let me know if that helps?

 

1/29/2010 12:25:22 PMAvatarDanLocation: bristol, ukposts: 254Karma: 319
Comment with ID: 24785

Doh, yeah, I missed that you'd got that in there - I'm used to seeing it above other js inclusions, so I'd definitely second Darren's prognosis.

1/29/2010 12:51:53 PMAvatartrfletchLocation: Rochester, UKposts: 334Karma: 289
Comment with ID: 24792

Ok I have changed to order of the scripts and I no longer get a javascript error but it still just shows the coordinates. I then tried copying the code from Darren's website again and I get and error with the script saying that Uncaught SyntaxError: Unexpected end of input

Am I right in thinking that I need another } at the end of the code shown on Darren's website like I put in my example above? If I do put an extra } at the end then I gets rid of all my javascript errors but I still just get the coordinates shown. Any ideas? cheers

1/29/2010 1:24:05 PMAvatarSebastiaan JanssenLocation: 2584GJ, The Hague, The Netherlandsposts: 573Karma: 1359
Comment with ID: 24796

I've altered Darren's JS a little, most notably I initialize the "fm" variable, try this:

 

$(document).ready(function () {
    $('div[class=map_canvas]').each(function () {
        var fm = {};
        fm.maps = new Array();

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

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

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

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

        fm.maps[fm.maps.length] = new GMap2(document.getElementById(mapId));
        var map = fm.maps[fm.maps.length - 1];

        var point = new GLatLng(lat, lon);
        map.setCenter(point, zoom);
        map.enableScrollWheelZoom();
        map.addControl(new GSmallZoomControl())
        map.setMapType(G_NORMAL_MAP);

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

 

1/29/2010 1:25:09 PMAvatarSebastiaan JanssenLocation: 2584GJ, The Hague, The Netherlandsposts: 573Karma: 1359
Comment with ID: 24797

oh, I changed the class name from "map" to "map_marker", so that should look like this for you:

$('div[class=map]').each(function () {
1/29/2010 1:35:29 PMAvatartrfletchLocation: Rochester, UKposts: 334Karma: 289
Comment with ID: 24799

Excellent, that is now displaying the map but I get a javascript error saying "p is not defined" and it does not display the marker.

1/29/2010 1:42:27 PMAvatarSebastiaan JanssenLocation: 2584GJ, The Hague, The Netherlandsposts: 573Karma: 1359
Comment with ID: 24800

My fault! :-) Change the marker line to this:

var marker = new GMarker(point);

 

Pages:

Please login or Sign up To post replies