-1

I'm trying to insert a google map tp my html code but it keeps on giving me this error: "myMap is not a function", here is my code:

function myMap() {
    var mapOptions = {
        center: new google.maps.LatLng(51.5, -0.12),
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.HYBRID
    }
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
#map{
  width: 100%;
  height: 250px;
  }
<div id="map"></div>

    <script src="https://maps.googleapis.com/maps/api/js?key=APIKEY4&callback=myMap"></script>
Josh Walshaw
  • 200
  • 1
  • 2
  • 17
stephjhonny
  • 227
  • 3
  • 9
  • 22

2 Answers2

1

You never initialize the actual map. Try the following. (it'll throw errors as I removed the API Key)

function myMap() {
    var mapOptions = {
        center: new google.maps.LatLng(51.5, -0.12),
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.HYBRID
    }
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
}

google.maps.event.addDomListener(window, 'load', myMap);
#map{
  width: 100%;
  height: 250px;
  }
<div id="map"></div>

    <script src="https://maps.googleapis.com/maps/api/js?key=APIKEY4&callback=myMap"></script>
Josh Walshaw
  • 200
  • 1
  • 2
  • 17
1

The posted code works as is if the code snippet is rearranged to define the myMap function before the include of the API. Note that you don't (currently) need a key on stackoverflow.com as it is grandfathered with keyless access.

#map {
  width: 100%;
  height: 250px;
}
<div id="map"></div>
<script>
  function myMap() {
    var mapOptions = {
      center: new google.maps.LatLng(51.5, -0.12),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.HYBRID
    }
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);
  }
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=myMap"></script>
geocodezip
  • 158,664
  • 13
  • 220
  • 245