开发者

JQuery, Google Maps : "Cannot call methods on gmap prior to initialization"

I have a problem I've been trying to figure out for a while now.

There's a block of code that works fine, but then if I add some JQuery in it, it crash everything.

I get the following error : "Uncaught cannot call methods on gmap prior to initialization; attempted to call method 'addMarker"

The faulty code starts from "$.each(markersList.markers,"

Any help to point me out in the right direction would be greatly appreciated. Thanks !

function initialize() {
var myOptions = {
    zoom: 7,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

$.each(markersList.markers,
      function(map, marker) {
          var destination = (marker.lat + ', ' + marker.lng);
          $('#map_canvas').gmap('addMarker',
          { 'position': new google.maps.LatLng(marker.lat, marker.lng), 'title': marker.title }, function(map, marker) {
               $('#map_canvas').gmap('addInfoWindow',
                  { 'content': null }, function(iw) {
                      $(marker).click(function() {iw.open(map, marker);
                          map.panTo(marker.getPosition());
                          $('#to').val(destination);
                          alert("You've selected " + marker.title + " as your destination. Please enter the origin");
                      });
               });
          });
      });

if (navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(f开发者_开发技巧unction(position) {
        initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        contentString = "Your location was found and has been added to starting point.";
        map.setCenter(initialLocation);
        $('#from').val(initialLocation);
    }, function() {
        handleNoGeolocation(browserSupportFlag);
    });


I had a similar issue with mine, what I did was create the initialize function as a separate function from the rest of the code, when the page loads I call Initialize() and at the end of the function I call searchLocations() which adds all of the markers to the map.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜