开发者

getJSON problem google maps v3

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 googl开发者_Go百科e 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜