【问题标题】:Google Maps API error: initMap is not a functionGoogle Maps API 错误:initMap 不是函数
【发布时间】:2018-01-12 07:58:57
【问题描述】:

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

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 个。我认为数据可能存在问题。它可以?我搜索但没有找到像我这样的类似问题。

【问题讨论】:

  • lat 和 lng 的值是多少,对吗?好像是解析问题
  • 谢谢,应该是这个问题。

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


【解决方案1】:

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

【讨论】:

  • 到底是什么问题?我遇到了同样的事情,但谷歌自动完成。一切都永远正常。我什么都没做,然后突然就开始崩溃了
  • 我的问题是坐标的格式不正确。
【解决方案2】:

您是否尝试定义您的函数initMap您从 Google Maps API 执行脚本之前?

只需将此行移至文件末尾即可。

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

【讨论】:

  • 我也测试过。问题是我的数组的某些字段有 lat: 0 和 lng: 0.
【解决方案3】:

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

function GoogleDistanceService() {
    var map;
    var directionDisplay;
    var distanceOption;
    var directionsService = new google.maps.DirectionsService();

    return {
        "initializeLoad": function (mapContext) {
            directionDisplay = new google.maps.DirectionsRenderer();
            var chicago = new google.maps.LatLng(35.220033, -50.5500100);
            var mapOptions = {
                zoom: 6,
                center: newyork
            };
            map = new google.maps.Map(mapContext, mapOptions);
            directionDisplay.setMap(map);

            ////////////////// here put code to hide your map div 

        },
        "callBack": function (response, status) {
            var distance = "";
            if (status != google.maps.DistanceMatrixStatus.OK) {
                distanceOption.errorCallBack(null);
            }
            else {
                var origins = response.originAddresses;
                var destinations = response.destinationAddresses;

        /////////// here show your map div

                for (var i = 0; i < origins.length; i++) {
                    var results = response.rows[i].elements;
                    for (var j = 0; j < results.length; j++) {
                        distance = results[j].distance.text.replace(" km", "") * 0.62137;
                    } 
                } 
            } 
            distanceOption.updatedCallBack({ "start": distanceOption.start, "end": distanceOption.end, "distance": distance });
        },
        "MapLoad": function (option) {
            distanceOption = option;
            this.initializeLoad(option.mapContext);
            var service = new google.maps.DistanceMatrixService();
            var request = { origin: option.start, destination: option.end, travelMode: google.maps.TravelMode.DRIVING };

            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionDisplay.setDirections(response);
                }
            });

            service.getDistanceMatrix({
                origins: [option.start],
                destinations: [option.end],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            }, this.callBack);
        }
    };    
};

【讨论】:

    【解决方案4】:
    You Calling the Function before it create. after create initMap function call it just like that:
    <script src="js/jquery.min.js"></script>
    1st create function: 
    <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>
    2nd call the function:
    <script async defer
                    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCmbNphkRdEmJqjC1rQwirD6NL-24Kbdb0&callback=initMap">
            </script>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2020-02-03
    • 2017-03-28
    • 1970-01-01
    • 2017-07-19
    • 2018-05-31
    相关资源
    最近更新 更多