Google Maps API错误:initMap不是一个函数

时间:2017-08-04 11:34:53

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

我的谷歌地图标记出了问题。它们仍然有效,但我在开发控制台中得到以下输出:

maps.php:43 Uncaught SyntaxError: Unexpected token ,

这是我的代码中的这一行:

center: {lat: <?php echo $e[0]; ?>, lng: <?php echo $e[1]; ?>}

下一条错误消息是:

message
:
"initMap is not a function"
name
:
"InvalidValueError"
stack
:
"Error↵    at new mc (https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:47:499)↵    at Object._.nc (https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:48:96)↵    at $g (https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:98:420)↵    at https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:138:53↵    at Object.google.maps.Load (https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:21:5)↵    at https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:137:20↵    at https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap:138:68

我没有收到我犯的错误,这是我的JS代码:

<script async defer
                    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCmbNphkRdEmJqjC1rQwirD6NL-24Kbdb0&callback=initMap">
            </script>
            <script>
                function initMap() {
                    var map = new google.maps.Map(document.getElementById('map'), {
                        zoom: 10,
                        center: {lat: <?php echo $e[0]; ?>, lng: <?php echo $e[1]; ?>}
                    }); 

                    // Create an array of alphabetical characters used to label the markers. 

                    // Add some markers to the map.
                    // Note: The code uses the JavaScript Array.prototype.map() method to
                    // create an array of markers based on a given "locations" array.
                    // The map() method here has nothing to do with the Google Maps API.

                    var infoWin = new google.maps.InfoWindow();

                    var markers = locations.map(function (location, i) {

                        var marker = new google.maps.Marker({
                            position: location, 
                        });                        

                        google.maps.event.addListener(marker, 'click', function (evt) {
                            infoWin.setContent("<div style='color: black;'><p style='font-size:1.5em;'>"+ location.date +"</p><h2>" + location.headline + "</h2><h3>" + location.subheadline + "</h3><p style='font-size:1.2em;'><strong>Start:</strong> "+ location.start +" Uhr</br><strong>Ende:</strong> "+ location.end +" Uhr</p><hr><p>" + location.text + "</p><hr><p style='font-size: 1.2em;'><strong>Adresse:</strong></br>" + location.street + " " + location.streetnr + ", " + location.plz + " " + location.city + "\n\
                                        </p><a style='font-size: 1.2em;' target='blank_' href='http://www.google.com/maps/place/" + location.lat + "," + location.lng + "'>Routenführung</a></div>");
                            infoWin.open(map, marker);
                        })

                        return marker;

                    });

                    console.log(markers);

                    // Add a marker clusterer to manage the markers.
                    var markerCluster = new MarkerClusterer(map, markers,
                            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
                }
                var locations = <?php echo json_encode($r); ?>
                console.log(locations);

            </script>

问题是我有56个标记,但只有27个出现。我认为数据可能存在问题。它可以?我搜索但没有找到像我一样的熟悉问题。

3 个答案:

答案 0 :(得分:1)

did you try to define your function initMap before you execute the script from the Google Maps API?

Just move this line to the end of the file.

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

答案 1 :(得分:0)

解析时出现数据错误。某些数据没有设置Lat或Lng ......

答案 2 :(得分:0)

您需要首先定义您的函数,就像我在下面的示例代码中所做的那样。

df['ZipCode'] = df.ZipCode.astype(str)  # just in case
df['ZipCodePlusFour'] = df.ZipCode.apply(lambda x: x[-4:] if len(x) > 5 else None)
df['ZipCode'] = df.ZipCode.apply(lambda x: x[:-4] if len(x) > 5 else