Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi there,

Google Maps JavaScript API Tutorial - YouTube[^]

I followed the video tutorial above which is called "
Google Maps JavaScript API Tutorial".


My problem is that when I compile it on chrome, it displays the title I set but not the google map itself with the markers.

Here is the code:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Arduino Smart Stick Location Map</title>
  <style>
    #map{
      height:400px;
      width:100%;
    }
  </style>
</head>
<body>
  <h1>Arduino Smart Stick Location Map</h1>
  <div id="map"></div>
  <script>
    function initMap(){
      // Map options
      var options = {
        zoom:10,
        center:{lat:53.4808,lng:-2.2426}
      }

      // New map
      var map = new google.maps.Map(document.getElementById('map'), options);

      // Listen for click on map
      google.maps.event.addListener(map, 'click', function(event){
        // Add marker
        addMarker({coords:event.latLng});
      });

      /*
      //Add marker
      var marker = new google.maps.Marker({
        position:{lat:53.4808,lng:-2.2426},
        map:map,
        icon:'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'
      });

      var infoWindow = new google.maps.InfoWindow({
        content:'<h1>Lynn MA</h1>'
      });

      marker.addListener('click', function(){
        infoWindow.open(map, marker);
      });
      */

      // Array of markers
      var markers = [
        {
          coords:{lat:53.470912,lng:-2.241365},
          iconImage:'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
          content:'<h1>John Dalton East Carpark</h1>'
        },
        {
          coords:{lat:53.471740,lng:-2.240842},
          content:'<h1>John Dalton East Tower</h1>'
        },
        {
          coords:{lat:53.472499,lng:-2.240830}
		  content:'<h1>John Dalton Foods</h1>'
        },
		{
          coords:{lat:53.470493,lng:-2.238513}
		  content:'<h1>AllSaints Park</h1>'
        },
		{
          coords:{lat:53.442371,lng:-2.201845}
		  content:'<h1>My Home</h1>'
        }
      ];

      // Loop through markers
      for(var i = 0;i < markers.length;i++){
        // Add marker
        addMarker(markers[i]);
      }

      // Add Marker Function
      function addMarker(props){
        var marker = new google.maps.Marker({
          position:props.coords,
          map:map,
          //icon:props.iconImage
        });

        // Check for customicon
        if(props.iconImage){
          // Set icon image
          marker.setIcon(props.iconImage);
        }

        // Check content
        if(props.content){
          var infoWindow = new google.maps.InfoWindow({
            content:props.content
          });

          marker.addListener('click', function(){
            infoWindow.open(map, marker);
          });
        }
      }
    }
  </script>
  <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA4mOCKmQe-ZovpqDeyvmTnrTK2Spcewzo-4M&callback=initMap">
    </script>
</body>
</html>



Why is only displaying the title and not the map with the markers and how can I go about resolving the issue ?

What I have tried:

YouTube Tutorials, Stackoverflow forum and javascript tutorials
Posted
Comments
ZurdoDev 19-Feb-19 13:25pm    
I'd suggest looking in the console of chrome's developer tools to see what the error is, if any.
Richard MacCutchan 19-Feb-19 13:30pm    
Did you ask the person who wrote the tutorial?
Kornfeld Eliyahu Peter 20-Feb-19 5:25am    
Open your built in debugger and see the errors...
Like https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key-map-error

1 solution

There were some hidden characters which were causing an issue. Just copy and paste below piece (you may not find differences but do it).

JavaScript
// Array of markers
var markers = [
  {
    coords:{lat:53.470912,lng:-2.241365},
    iconImage:'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
    content:'<h1>John Dalton East Carpark</h1>'
  },
  {
    coords:{lat:53.471740,lng:-2.240842},
    content:'<h1>John Dalton East Tower</h1>'
  },
  {
    coords:{lat:53.472499,lng:-2.240830},
    content:'<h1>John Dalton Foods</h1>'
  },
  {
    coords:{lat:53.470493,lng:-2.238513},
    content:'<h1>AllSaints Park</h1>'
  },
  {
    coords:{lat:53.442371,lng:-2.201845},
    content:'<h1>My Home</h1>'
  }];
 
Share this answer
 
Comments
Richard Deeming 22-Feb-19 11:38am    
Rather than a cryptic comment about "hidden characters", you could just have explained that there were missing commas between the coords and content properties for all but the first two markers.

{
    coords:{lat:53.472499,lng:-2.240830}
    content:'<h1>John Dalton Foods</h1>'
},
vs
{
    coords:{lat:53.472499,lng:-2.240830},
    content:'<h1>John Dalton Foods</h1>'
},
Member 13710355 23-Feb-19 12:05pm    
Thank you for your help
GSThakur 1-Mar-19 8:04am    
Good catch, I didn't noticed that difference. My style of debugging for an issue is different. Lot many other solution providers might have read your question and I'm hoping you also could have spent good amount of hours to make this working. Earlier, for such similar issue, I spent sleepless night, just for hidden ASCII chars.

However, if the solution worked for you, please mark as accepted solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900