JavaScript Google Maps Api v3 marker. google is undefined google maps marker icons,google maps marker color,google maps

I've a website with the google maps api which is loaded asynchronous. But this throws a errer: google is not found. My code is:

<script>

function initialize() {

  var mapOptions = {
    zoom: 10,
    center: new google.maps.LatLng(51.817116, 4.780616),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    panControl: false,
    mapTypeControl: false,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    rotateControl: false
  };

  var map = new google.maps.Map(document.getElementById('maps'),
  mapOptions);
};

var customMarker = new google.maps.Marker({
  position: new google.maps.LatLng(51.817116, 4.780616),
  map: map
});

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3&sensor=false&' +
    'callback=initialize';
  document.body.appendChild(script);
}

addLoadEvent(loadScript);

</script>

When I delete the marker the code works correctly. Why is'nt it working if I add the marker as specified in some examples?

The addLoad is a load event. That's not the problem... Can anybody help me to get this working?

Answer:1

You can't use the Google Maps Javascript API v3 until it is loaded. Your marker creation is running before the API is loaded. You need to move it in to the initialize function, which won't execute until the API is available.

<script>

function initialize() {

  var mapOptions = {
    zoom: 10,
    center: new google.maps.LatLng(51.817116, 4.780616),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    panControl: false,
    mapTypeControl: false,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    rotateControl: false
  };

  var map = new google.maps.Map(document.getElementById('maps'),
  mapOptions);

  var customMarker = new google.maps.Marker({
    position: new google.maps.LatLng(51.817116, 4.780616),
    map: map
  });

};  // end of initialize


function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3&sensor=false&' +
    'callback=initialize';
  document.body.appendChild(script);
}

addLoadEvent(loadScript);

</script>
Answer:2

I have the following demo data. var demoData= [{"EntryGroupDate":"November 2013", "DisplayName": "Hans Meier (November)", "EntryGroupDateSort": 11}, {"...

I have the following demo data. var demoData= [{"EntryGroupDate":"November 2013", "DisplayName": "Hans Meier (November)", "EntryGroupDateSort": 11}, {"...

I am trying to make a web form which has the option to add and remove a new element. I can get this working as buttons outside of the source or cloned elements, but I am having a problem getting a '...

I am trying to make a web form which has the option to add and remove a new element. I can get this working as buttons outside of the source or cloned elements, but I am having a problem getting a '...

  1. jquery remove cloned element
  2. jquery remove cloned row
  3. jquery remove cloned object
  4. jquery remove clone
  5. jquery remove clone div
  6. jquery remove clone append
  7. jquery remove clone table
  8. jquery remove clone on click
  9. jquery clone remove class
  10. jquery clone remove id
  11. jquery clone remove attribute
  12. jquery clone remove events
  13. jquery clone remove values
  14. jquery clone remove original
  15. jquery clone remove data

I've been experimenting and it seems like only form elements and the window object can trigger the keypress event. I checked MDN documentation, and also the spec, but couldn't find anywhere that ...

I've been experimenting and it seems like only form elements and the window object can trigger the keypress event. I checked MDN documentation, and also the spec, but couldn't find anywhere that ...

When defining a class in Javascript, how can I call one method from another one? exports.myClass = function () { this.init = function() { myInternalMethod(); } this....

When defining a class in Javascript, how can I call one method from another one? exports.myClass = function () { this.init = function() { myInternalMethod(); } this....

  1. calling method from another class
  2. calling method from another class c#
  3. calling method from another class python
  4. calling method from different class java
  5. calling method from main java
  6. calling method from parent class python
  7. calling method from same class java
  8. calling method from superclass java
  9. calling method from class python
  10. calling method from constructor java
  11. calling method from abstract class java
  12. calling method from parent class java
  13. calling method from another component angular
  14. calling method from this
  15. call method from another activity android
  16. call method from another class react
  17. call method from another script unity
  18. call method from activity to fragment
  19. call method from another controller laravel
  20. call method from another component vue