我的js代码的一部分不起作用

时间:2017-11-18 22:27:48

标签: javascript google-maps marker

我需要在我的页面上使用谷歌地图和标记,我使用来自developers.google.com的代码,但只有标记的位置才有效。但是其他命令例如缩放,标记的标题等不起作用。将鼠标粘贴在标记上后写入Hello世界,我不知道为什么。请帮助,谢谢。

这是我的代码



function initMap() {
        var uluru = {lat: 48.151953, lng: 17.073795};
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 15,
            center: uluru
        });

        var contentString = testtesttest;

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        var marker = new google.maps.Marker({
            position: uluru,
            map: map,
            title: 'Uluru (Ayers Rock)'
        });
        marker.addListener('click', function() {
            infowindow.open(map, marker);
        });
    }

.mapa {
    background-color: yellowgreen;
    width: 100%;
    height: 600px;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Mapy</title>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="map" class="mapa"></div>

<script src="js/script.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBigzZWrOz-3XuKavGo1NwskECGn8SkdCY&callback=initMap"  async defer></script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你只需要在contentString变量

的内容中加上引号

function initMap() {
        var uluru = {lat: 48.151953, lng: 17.073795};
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 15,
            center: uluru
        });

        var contentString = 'testtesttest';

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        var marker = new google.maps.Marker({
            position: uluru,
            map: map,
            title: 'Uluru (Ayers Rock)'
        });
        marker.addListener('click', function() {
            infowindow.open(map, marker);
        });
    }
.mapa {
    background-color: yellowgreen;
    width: 100%;
    height: 600px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Mapy</title>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="map" class="mapa"></div>

<script src="js/script.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBigzZWrOz-3XuKavGo1NwskECGn8SkdCY&callback=initMap"  async defer></script>
</body>
</html>