【问题标题】:Google MAP not displaying markers with custom labels谷歌地图不显示带有自定义标签的标记
【发布时间】:2017-06-04 17:48:31
【问题描述】:

我知道这是在我身上。我之前的问题已经让我失望了,我只是错过了一个逗号。谢天谢地,一个不正确的语法错误将我指向修复,但现在没有给出错误,正在创建地图,但没有放置任何标记。

我的 xml 文件正在正确生成,如此处xml generated file 所示,但没有显示任何内容。有人可以把这段代码看作第二双眼睛吗?

<!DOCTYPE html >
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>L3 CARDS</title>
    <style>
    /* Always set the map height explicitly to define the size of the div
    * element that contains the map. */
    #map {
        height: 50%;
        width: 50%;
    }
    /* Optional: Makes the sample page fill the window. */
    html, body {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
    }
    </style>
</head>

<body>
    <div id="map"></div>

    <script>
    var customLabel = {
        1: {
            label: 'A'
        },
        2: {
            label: 'I'
        },
        3: {
            label: 'L'
        },
        4: {
            label: 'R'
        },
        5: {
            label: 'G'
        }
    };

    function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
            center: new google.maps.LatLng(45.3791021, -122.7613788),
            zoom: 15
        });
        var infoWindow = new google.maps.InfoWindow;

        // Change this depending on the name of your PHP or XML file
        downloadUrl('maptest2.php', function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName('marker');
            Array.prototype.forEach.call(markers, function(markerElem) {
                var id = markerElem.getAttribute('id');
                var reporttype = markerElem.getAttribute('reporttype');
                var propertyname = markerElem.getAttribute('propertyname');
                var point = new google.maps.LatLng(
                    parseFloat(markerElem.getAttribute('lat')),
                    parseFloat(markerElem.getAttribute('lng'))
                );
                var infowincontent = document.createElement('div');
                var strong = document.createElement('strong');
                strong.textContent = propertyname
                infowincontent.appendChild(strong);
                infowincontent.appendChild(document.createElement('br'));

                var text = document.createElement('text');
                text.textContent = propertyname
                infowincontent.appendChild(text);
                var icon = customLabel[reporttype] || {};
                var marker = new google.maps.Marker({
                    map: map,
                    position: point,
                    label: icon.label
                });
                marker.addListener('click', function() {
                    infoWindow.setContent(infowincontent);
                    infoWindow.open(map, marker);
                });
            });
        });
    }

    function downloadUrl(url, callback) {
        var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                request.onreadystatechange = doNothing;
                callback(request, request.status);
            }
        };

        request.open('GET', url, true);
        request.send(null);
    }

    function doNothing() {}
    </script>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=myAPIkey&callback=initMap"></script>
</body>
</html>

提前致谢

【问题讨论】:

  • 你的纬度和经度值是相反的。
  • geocodezip,你是神。我盯着代码看了好几个小时,从来没想过。请重新提交此作为答案,以便我接受。它正在工作!

标签: javascript php mysql google-maps google-maps-api-3


【解决方案1】:

你的纬度和经度值是相反的

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2013-05-20
    • 2015-01-28
    • 2015-04-25
    相关资源
    最近更新 更多