cordova-plugin-googlemaps: Uncaught TypeError: Cannot read property 'apply' of undefined

Hi there I am getting this error when I create map and add markers to it.

Uncaught TypeError: Cannot read property ‘apply’ of undefined at Map.trigger (BaseClass.js:62) at Map.js:195

Here is my code snippet: Am I doing anything wrong ?

function displayRestaurantResultsOnMap(data) {
  //Get all the restaurants info and parse the lat and long
  var JERSEY = new plugin.google.maps.LatLng(49.217231, -2.140589); //Center of JERSEY
  $("#searchmap-text").html('Loading...');
  setTimeout(function() {
    var div = document.getElementById("searchmap_canvas_div");
    $('#searchmap_canvas_div').css('height', $(window).height() - $('#searchmap_canvas_div').offset().top);
    mapNew = plugin.google.maps.Map.getMap(div, {
      'camera': {
        'latLng': JERSEY,
        'zoom': 12
      }
    });
    mapNew.on(plugin.google.maps.event.MAP_READY, searchMapInit(data));
  }, 1000);

}

function searchMapInit(data) {
  mapNew.clear();
  mapNew.setVisible(true);
  mapNew.setCameraZoom(11);



  //	var mData =[];
  $.each(data, function(key, val) {
    if (val.latitude) {
      var point = new plugin.google.maps.LatLng(val.latitude, val.longitude);
      var entry = {
        'position': point,
        'title': val.restaurant_name,
        'icon': {
          'url': 'https://www.cuisine.je/assets/images/menu-icon.png'
        }
      };
      //mData.push(entry);
      dump("marker options" + entry);
      //Add marker
      dump(mapNew)
      mapNew.addMarker(entry, function(marker) {
        dump("Marker added " + marker);
        // Display the infoWindow
        marker.showInfoWindow();
        marker.setAnimation(plugin.google.maps.Animation.BOUNCE);

      });
    }
  });


  $("#searchmap-text").html('Loaded!!');
  addSearchMarkers(mData, function(markers) {
    markers[markers.length - 1].showInfoWindow();
    markers[markers.length - 1].setAnimation(plugin.google.maps.Animation.BOUNCE);
  });

}

function addSearchMarkers(data, callbac) {
  var markers = [];

  data.forEach(function(markerOptions) {
    dump(markerOptions);
    mapNew.addMarker(markerOptions, onMarkerAdd);
  });

  function onMarkerAdd(marker) {
    dump("Marker added " + marker);
    markers.push(marker);
    if (markers.length === data.length) {
      callbac(markers);
    }
  }
}

Thanks and cheers Raj

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25 (13 by maintainers)

Most upvoted comments

will do

If you want to check your project by me, please share your project files on github or bitbucket.

mapNew.on(plugin.google.maps.event.MAP_READY, searchMapInit(data));

This is incorrect code.

Because you try to register the return value of searchMapInit(data) (and it’s undefined). That’s why you get error.

If you want to pass initial data to searchMapInit, you need to do this

mapNew.on(plugin.google.maps.event.MAP_READY,function() {
  searchMapInit(data)
});

Its throwing undefined variable at the Baseclass’ trigger funciton

trigger: function(eventName) {
  if (!eventName) {
    return this;
  }

  if (!this[SUBSCRIPTIONS_FIELD][eventName]) {
    return this;
  }

  var listeners = this[SUBSCRIPTIONS_FIELD][eventName];
  var i = listeners.length;
  var args = Array.prototype.slice.call(arguments, 1);

  while (i--) { **
    listeners[i].apply(this, args); **
  }

  return this;
},