Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to function that creates a google map doesn't work

I'm assuming this is straight forward, just not having any luck with it.

I have the following functions

function init(myPoint) {
  var mapDiv = document.getElementById('map-canvas');
  var map = new google.maps.Map(mapDiv, {
  center: new google.maps.LatLng(myPoint, -122.39031314844),
  zoom: 8,
  mapTypeId: google.maps.MapTypeId.ROADMAP
  });
}

function start() {
  google.maps.event.addDomListener(window, 'load', init(37.2342));
}

If I remove the param and just hardcode myPoint everything works. However in its current state it does not work. Can someone explain to me what I'm doing wrong.

Thanks

like image 775
Steve Avatar asked Jan 21 '23 00:01

Steve


1 Answers

Your function is not called on window load; it is called upon code execution. instead of init(37.2342) you should use function(){init(37.2342);} with addDomListener:

google.maps.event.addDomListener(window, 'load', function () { init(37.2342); });
like image 86
Gabi Purcaru Avatar answered May 22 '23 05:05

Gabi Purcaru