views:

52

answers:

2

hi,

I am trying to make google maps v3 work with ASP MVC with markers from a database. I got this working fine with version 2 of the api as decribed in an article I found ( http://mikehadlow.blogspot.com/2008/10/using-google-maps-with-mvc-framework.html?showComment=1280600518852#c4603834263614352338 ), but I am unable to convert this v2 code to v3. The problem seems to lie in the getJSON because that breaks everything. I've only edited the google maps code from the article. I'm not getting any errors with firebug.

This is the code I use:

$(function () {
  $.getJSON("/Home/Map", initialise);
});

$(function initialise(mapData) {

  var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map"), myOptions);

  $.each(mapData.Locations, function (i, location) {
    setupLocationMarker(map, location);
});

});

$(function setupLocationMarker(map, location) {

  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(location.LatLng.Latitude, location.LatLng.Longitude),
    map: map,
    title: location.Title
  });

});

Can anyone see whats wrong with it?

A: 

If jQuery's getJSON() function cannot parse the data returned by the server, i.e. your JSON is malformed, it silently fails (you don't get any errors in Firebug or other tools). Check the data you feed to the client, and use a validator to make sure it conforms to the JSON spec.

David Parunakian
I havent changed anything about the JSON output which worked with the old maps v2 code. To be sure I just validated the JSON that gets outputted and its valid.
Prd
A: 

I would write

$(function initialise(mapData) { 
}); 

as

function initialise(mapData) { 
} 

and the same goes for setupLocationMarker.

I tested this with a single line that creates a LatLng object, and when removed the code works properly. Otherwise it does not recognise the google maps LatLng object.

CRice