未捕获的TypeError:无法读取属性' targetLat'未定义的

时间:2017-04-10 13:50:06

标签: javascript html json ajax google-maps

好吧,我只是在谷歌地图上放了一些标记,它按照我想要的方式工作,并且标记在地图上显示为必要但我在控制台中收到此错误我不知道为什么这使得应用程序在Android上运行但不是在Windows手机上

function initMap() {
        var map;
        var faisalabad = { lat: 30.044281, lng: 31.340002 };

        $('#locaitonBtn').click(function () {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    map.setCenter(latlng);
                });
            }
        })

        map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: faisalabad,
            disableDefaultUI: true
        });
        $.ajax({
            async:false,
            url: "https://someUrl",
            method: "GET",
            dataType: 'json',
            success: function (data) {
                var infowindow = new google.maps.InfoWindow();
                for (i = 0; i <= data.targets.length; i++) {
                    var myLatLng = new google.maps.LatLng(data.targets[i].targetLat, data.targets[i].targetLong);
                    myMarker = new google.maps.Marker({
                        map: map,
                        animation: google.maps.Animation.DROP,
                        position: myLatLng
                    });

                    google.maps.event.addListener(myMarker, 'click', (function (myMarker, i) {
                        return function () {
                            infowindow.setContent(data.targets[i].targetName);
                            infowindow.open(map, myMarker);
                        }
                    })(myMarker, i));
                }
            }
        })

    }

2 个答案:

答案 0 :(得分:0)

$.ajax({
    async:false,
    url: "https://someUrl",
    method: "GET",
    dataType: 'json',
    success: function (data) {
        var infowindow = new google.maps.InfoWindow();
        for (var i = 0; i <= data.targets.length; i++) {
            var myLatLng = new google.maps.LatLng(data.targets[i].targetLat, data.targets[i].targetLong);
            var myMarker = new google.maps.Marker({
                map: map,
                animation: google.maps.Animation.DROP,
                position: myLatLng
            });

            google.maps.event.addListener(myMarker, 'click', (function (myMarker, i, data) {
                return function () {
                    infowindow.setContent(data.targets[i].targetName);
                    infowindow.open(map, myMarker);
                }
            })(myMarker, i, data));
        }
    }
});

改变了三件事:

  1. var i = 0 in for statement
  2. var myMarker = new google.maps.Marker ...(它不是全球性的)
  3. data注入匿名点击侦听器函数,因为它在数据对象上没有作用域。
  4. 我不确定这是否有效,因为我没有测试用途。但希望它可以帮助你弄清楚失败。

    我认为第一点是失败,因为您尝试在for循环中使用全局i并在代码中的任何其他位置重置它。所以数组索引返回undefined

答案 1 :(得分:0)

我无法解决问题或在任何地方找到答案,所以我将成功函数添加到try catch块,以便它不会控制错误,我现在可以在Windows上运行应用程序电话