谷歌地图api标记不会出现在地图上

时间:2018-02-01 14:27:40

标签: javascript html google-maps google-maps-api-3

制作使用谷歌地图的应用我制作标记并将其初始化为其网站上的演示https://developers.google.com/maps/documentation/javascript/markers

当我在chrome中打开DevTools控制台时,我发现没有任何错误 My Application

这是HTML,CSS& JS文件

HTML

    <!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>Neighborhood Map</title>

    <!-- Bootstrap CSS CDN -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- Our Custom CSS -->
    <link rel="stylesheet" type="text/css" href="css\custom-style.css">
    <!-- Scrollbar Custom CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css">
    <!-- -->
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry,drawing&key=Your_AP_KEY&v=3&callback=initMap"
        async defer></script>
</head>

<body>

    <div class="wrapper">
        <!-- Sidebar Holder -->
        <nav id="sidebar">
            <div id="dismiss">
                <i class="glyphicon glyphicon-arrow-left"></i>
            </div>

            <div class="sidebar-header">
                <h3>Bootstrap Sidebar</h3>
            </div>

            <ul class="list-unstyled components">
                <p>Dummy Heading</p>
                <li class="active">
                    <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Home</a>
                    <ul class="collapse list-unstyled" id="homeSubmenu">
                        <li>
                            <a href="#">Home 1</a>
                        </li>
                        <li>
                            <a href="#">Home 2</a>
                        </li>
                        <li>
                            <a href="#">Home 3</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#">About</a>
                    <a href="#pageSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
                    <ul class="collapse list-unstyled" id="pageSubmenu">
                        <li>
                            <a href="#">Page 1</a>
                        </li>
                        <li>
                            <a href="#">Page 2</a>
                        </li>
                        <li>
                            <a href="#">Page 3</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#">Portfolio</a>
                </li>
                <li>
                    <a href="#">Contact</a>
                </li>
            </ul>

            <ul class="list-unstyled CTAs">
                <li>
                    <a href="https://bootstrapious.com/tutorial/files/sidebar.zip" class="download">Download source</a>
                </li>
                <li>
                    <a href="https://bootstrapious.com/p/bootstrap-sidebar" class="article">Back to article</a>
                </li>
            </ul>
        </nav>
        <!-- Page Navigation Bar -->
        <button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn">
            <i class="glyphicon glyphicon-align-left"></i>
            <span>Open Sidebar</span>
        </button>

        <!-- Page Content Holder -->
        <div id="content">
            <div id="map"></div>
        </div>
    </div>

    <div class="overlay"></div>

    <!-- jQuery CDN -->
    <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <!-- Bootstrap Js CDN -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!-- jQuery Custom Scroller CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.concat.min.js"></script>
    <!-- -->
    <script src="js\lib\knockout-3.4.2.js" async></script>
    <!-- -->
    <script src="js\app.js"></script>

</body>

</html>

CSS

    /*
    DEMO STYLE
*/

@import "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700";
html {
    box-sizing: border-box;
}

body {
    font-family: 'Poppins', sans-serif;
    background: #fafafa;
}

p {
    font-family: 'Poppins', sans-serif;
    font-size: 1.1em;
    font-weight: 300;
    line-height: 1.7em;
    color: #999;
}

a, a:hover, a:focus {
    color: inherit;
    text-decoration: none;
    transition: all 0.3s;
}

.navbar {
    background: #fff;
    border: none;
    border-radius: 0;
    margin-bottom: 40px;
    box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 1;
}

.navbar-btn {
    box-shadow: none;
    outline: none !important;
    border: none;
    margin-left: 10px;
    position: relative;
    z-index: 1;
}

.navbar-header {
    height: 30%;
}

.line {
    width: 100%;
    height: 1px;
    border-bottom: 1px dashed #ddd;
    margin: 40px 0;
}

/* ---------------------------------------------------
    SIDEBAR STYLE
----------------------------------------------------- */

#sidebar {
    width: 250px;
    position: fixed;
    top: 0;
    left: -250px;
    height: 100vh;
    z-index: 999;
    background: #7386D5;
    color: #fff;
    transition: all 0.3s;
    overflow-y: scroll;
    box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.2);
}

#sidebar.active {
    left: 0;
}

#dismiss {
    width: 35px;
    height: 35px;
    line-height: 35px;
    text-align: center;
    background: #7386D5;
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
    -webkit-transition: all 0.3s;
    -o-transition: all 0.3s;
    transition: all 0.3s;
}

#dismiss:hover {
    background: #fff;
    color: #7386D5;
}

.overlay {
    position: fixed;
    width: 100vw;
    height: 100vh;
    bottom: 0px;
    background: rgba(0, 0, 0, 0.7);
    z-index: 998;
    display: none;
}

#sidebar .sidebar-header {
    padding: 20px;
    background: #6d7fcc;
}

#sidebar ul.components {
    padding: 20px 0;
    border-bottom: 1px solid #47748b;
}

#sidebar ul p {
    color: #fff;
    padding: 10px;
}

#sidebar ul li a {
    padding: 10px;
    font-size: 1.1em;
    display: block;
}

#sidebar ul li a:hover {
    color: #7386D5;
    background: #fff;
}

#sidebar ul li.active>a, a[aria-expanded="true"] {
    color: #fff;
    background: #6d7fcc;
}

a[data-toggle="collapse"] {
    position: relative;
}

a[aria-expanded="false"]::before, a[aria-expanded="true"]::before {
    content: '\e259';
    display: block;
    position: absolute;
    right: 20px;
    font-family: 'Glyphicons Halflings';
    font-size: 0.6em;
}

a[aria-expanded="true"]::before {
    content: '\e260';
}

ul ul a {
    font-size: 0.9em !important;
    padding-left: 30px !important;
    background: #6d7fcc;
}

ul.CTAs {
    padding: 20px;
}

ul.CTAs a {
    text-align: center;
    font-size: 0.9em !important;
    display: block;
    border-radius: 5px;
    margin-bottom: 5px;
}

a.download {
    background: #fff;
    color: #7386D5;
}

a.article, a.article:hover {
    background: #6d7fcc !important;
    color: #fff !important;
}

/* ---------------------------------------------------
    CONTENT STYLE
----------------------------------------------------- */

#content {
    width: 100%;
    padding: 0;
    min-height: 100vh;
    transition: all 0.3s;
    position: absolute;
    top: 0;
    right: 0;
    display: flex
}

#map {
    width: 100%;
    flex: 2;
}

JS

    // map variable holds an object from MAP class
var map;
// Markers array holds my markers
var Markers = [];
// These are the real estate listings that will be shown to the user.
var favouriteLocations = [
    { title: 'Primoz Pizza', location: { lat: 30.112503, lng: 31.348311 } },
    { title: 'El-Sadat Park', location: { lat: 30.165822, lng: 31.424106 } },
    { title: 'Chili\'s Al Merghani', location: { lat: 30.085275, lng: 31.333463 } },
    { title: 'Saudi German Hospital - Cairo', location: { lat: 30.132663, lng: 31.384305 } },
    { title: 'Post Office - El Salam City', location: { lat: 30.167742, lng: 31.413091 } },
    { title: 'Citystars Heliopolis', location: { lat: 30.072979, lng: 31.346050 } }
];
// Intiats map and adds markers
function initMap() {

    // Boundary Object to fit locations in the map
    var bounds = new google.maps.LatLngBounds();

    // Constructor creates a new map - only center and zoom are required.
    map = new google.maps.Map(document.getElementById('map'), {
        center: { lat: 30.165822, lng: 31.424106 },
        zoom: 13
    });


    // The following group uses the favouriteLocations array to create an array of markers on initialize.
    for (let i = 0; i < favouriteLocations.length; i++) {
        // Get position & title from the locations array
        var posision = favouriteLocations[i].location;
        var title = favouriteLocations[i].title;
        debugger;
        // Create a marker per loction, and put it into Markers array.
        var marker = new google.maps.Marker({
            map: map,
            posision: posision,
            title:title,
            animation: google.maps.Animation.DROP,
            id: i
        });
        // Push The marker to Markers Array
        Markers.push(marker);
        // Holds Boundaries of all markers
        bounds.extend(Markers[i].posision);
    }
    // Extend the boundaries of the map for each marker
    map.fitBounds(bounds);

}


// Sliders
$(document).ready(function () {
    $("#sidebar").mCustomScrollbar({
        theme: "minimal"
    });

    $('#dismiss, .overlay').on('click', function () {
        $('#sidebar').removeClass('active');
        $('.overlay').fadeOut();
    });

    $('#sidebarCollapse').on('click', function () {
        $('#sidebar').addClass('active');
        $('.overlay').fadeIn();
        $('.collapse.in').toggleClass('in');
        $('a[aria-expanded=true]').attr('aria-expanded', 'false');
    });
});

我无法弄清楚出了什么问题?

1 个答案:

答案 0 :(得分:1)

检查此JSBin:http://jsbin.com/bacoyor/4/edit?js,output

我在以下代码中将'posision'更改为'position':

var marker = new google.maps.Marker({
    map: map,
 -> position: posision,
    title:title,
    animation: google.maps.Animation.DROP,
    id: i
});

bounds.extend(Markers[i].position);

我还将用于加载Google Maps JavaScript API的脚本移动到正文标记的末尾。

<body>
    [...]
    <script src="js\app.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry,drawing&key=YOUR_API_KEY&v=3&callback=initMap"
    async defer></script>
</body>