Google Maps Marker不会出现在我的网站上

时间:2016-06-22 01:01:16

标签: javascript

有人可以帮助我,但我不懂javascript

    <script type="text/javascript">
        var myOptions = {
            zoom: 16,
            center: new google.maps.LatLng(31.6321288,-8.0099319,787),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        var myLatLng = new google.maps.LatLng(31.6321288,-8.0099319,787);
        var beachMarker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: "We Are Here"
        });
    </script>

2 个答案:

答案 0 :(得分:1)

根据Google文档,它如下。 确保在html标记中包含此行,并让Google Map API Key替换YOUR_API_KEY

<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple markers</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

      function initMap() {
        var myLatLng = {lat: -25.363, lng: 131.044};

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: myLatLng
        });

        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          title: 'Hello World!'
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
  </body>
</html>

答案 1 :(得分:0)

1 - 您需要创建Google地图项目并生成API密钥(https://console.developers.google.com)。它非常简单快速。

2 - 因此,您需要导入GoogleMapsAPI,为您的API KEY更改YOUR_API_KEY:

<script async defer
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>

结果是这样的:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Simple markers</title>
        <style>
            html, body {
                height: 100%;
                margin: 0;
                padding: 0;
            }
            #map {
                height: 100%;
            }
        </style>
    </head>
    <body>
        <div id="map"></div>
        <script>
            function initMap() {
                var myLatLng = {lat:31.6321288, lng:-8.0099319};

                var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 16,
                    center: myLatLng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: 'Hello World!'
                });
            }
        </script>
        <script async defer
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
        </script>
    </body>
</html>