0

I have the following script in my HTML head element.

<script src="" id="mapScriptID" type="text/javascript"></script>
<script type="text/javascript">
    var resloc = "http://maps.google.com/maps/api/js?sensor=true&key="+key;
    var mapScript = document.getElementById('mapScriptID');
    mapScript.src = resloc;
</script>  

The reason for it is that I would like the key value for the google map api to be defined in once only as I am using it in multiple places in my application. So that in the future when the key value changes I only have to change it in one place. But now for some reason the application is not seeing the google map api anymore. It's giving me 'google is not defined' message when I call google.maps.LatLng(....);

it previously used to be

 <script src="http://maps.google.com/maps/api/js?sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxx" type="text/javascript"></script>

And it worked completely fine.

5
  • 2
    Have a look to how to load js files dinamically. You should be able to use this this: stackoverflow.com/questions/950087/… Commented Jun 18, 2019 at 8:37
  • "It's giving me 'google is not defined' message when I call google.maps.LatLng(....);" — You don't appear to be doing that though. Provide a minimal reproducible example. Commented Jun 18, 2019 at 8:50
  • Where is the value for key set? Commented Jun 18, 2019 at 9:36
  • @phuzi in a separate .js file Commented Jun 20, 2019 at 15:22
  • @Quentin sorry about that. I meant when I used the library that I tagged in the HTML Commented Jun 20, 2019 at 15:23

1 Answer 1

1

After trying multiple solutions. Eventually I used JQuery's $.getScript() method which worked sufficiently well in my scenario.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.