Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

javascript - Google map API is not displaying my map even though I have the correct authentication key

Google API map not displaying when I load my code in a google chrome browser. I created my authentication key with in a new project on the Google Cloud Platform.

I've tried generating a new authentication key on the Google Cloud platform by creating a new project, but that did not seemed to work.

<!DOCTYPE html>
<html lang="en">
<head>
   <style>
  #map{
     height:100%;
  }
  </style>
  <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>Document</title>
</head>
<body>
  <div id="output">Complete JavaScript Course </div>
<div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js? 
 key=<<my key>>"></script>
 <script src="app.js">

  window.onload = init;
      var m = document.getElementById('map');

      function init() {
          navigator.geolocation.getCurrentPosition(placeMap);
      }

      function placeMap(data) {
          var options = {
              center: {
                  lat: data.coords.latitude
                  , lng: data.coords.longitude
              }
              , zoom: 5
          }
          var map = new google.maps.Map(m, options);
          console.dir(data);
      }

</script>

</body>
</html>

As I've already stated, the google map api will not display a map of my actual location.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two issues with the posted code.

  1. Your map div doesn't have a size, you need to define the size of html and body so the #map 100% height has something to reference:
<style>
#map{
   height:100%;
}
</style>

should be:

<style>
html, body, #map {
  height: 100%;
  margin: 0;
  padding: 0;
}
</style>
  1. you have an unneeded src on your script tag:
<script src="app.js"> 

should be:

<script>

proof of concept fiddle

screenshot of resulting map

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#map {
  height: 90%;
}
<div id="output">Complete JavaScript Course </div>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<script>
  window.onload = init;
  var m = document.getElementById('map');

  function init() {
    navigator.geolocation.getCurrentPosition(placeMap);
  }

  function placeMap(data) {
    var options = {
      center: {
        lat: data.coords.latitude,
        lng: data.coords.longitude
      },
      zoom: 5
    }
    var map = new google.maps.Map(m, options);
    console.dir(data);
  }
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...