Niaker
Niaker

Reputation: 45

Use initialized Google Maps in other .js

I've two diffrent .js-files: scripts.js and search.js

In script.js the Google Maps is initialized:

function initMap() {
var mapDiv = document.getElementById('market-map');
var map = new google.maps.Map(mapDiv, {
    center: {lat: 12345, lng: 12345},
    zoom: 8
});
}

It's working fine.

But I need this map in search.js as a variable, so I can work with this map in search.js (add Geocoder etc.)

Does anyone know a solution, how I can fix this problem?

Thanks for your help. :)

Upvotes: 0

Views: 35

Answers (1)

Robiseb
Robiseb

Reputation: 1606

You just have to set map as a globale variable

var map = null;

function initMap() {
  var mapDiv = document.getElementById('market-map');
  map = new google.maps.Map(mapDiv, {
    center: {lat: 12345, lng: 12345},
    zoom: 8
  });
}

Then, search.js will access to it.

Upvotes: 2

Related Questions