更改了网络,现在我的标记不会显示。可能是什么问题?

时间:2018-12-22 14:01:28

标签: javascript google-maps google-maps-markers

我有地图来显示基于API请求的JSON的标记。我有自定义标记图标,当我不得不在圣诞节期间移交给父母时,我即将开始使用Google Distance Matrix API。突然,当我转到网页检查我的代码是否正确时,页面上没有显示标记。控制台中也没有错误!

这可能是引起问题的网络更改吗? Google地图可以正确显示,但不能正确显示标记。

var map;
var infowindow;

function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        center: {
            lat: 54.97328,
            lng: -1.61396
        },
        zoom: 11
    });
}


for (var j = 1; j < 5; j++) {
    console.log(j);
    (function (j) { // protects i in an immediately called function
        $.getJSON('https://cors-anywhere.herokuapp.com/http://ratings.food.gov.uk/enhanced-search/en-GB/%5E/Newcastle%20Upon%20Tyne/Relevance/0/%5E/Equal' + (j) + '/0/1/5/json', function (data) {
        console.log(data);


        for (var i = 0; i < data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail.length; i++) {
            console.log(j);

            infowindow = new google.maps.InfoWindow({
                size: new google.maps.Size(50, 50) // too small 
            });


            var establishmentInfo = "<b>Restaurant Name</b><p>" + data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].BusinessName + "</p><b>Address</b><p>" + data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].AddressLine1 + "<br>" + data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].AddressLine2 + "<br>" + data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].AddressLine3 + "<br>" + data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].PostCode + "</p>";



            var image = "./images/" + (i + 1) + "marker.png";
            console.log(image);
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].Geocode.Latitude, data.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail[i].Geocode.Longitude),
                title: "Place " + i,
                animation: google.maps.Animation.DROP,
                icon: image
            });

            marker.setMap(map);

            makeInfoWindow(marker, establishmentInfo); 

        }
    });
})(j);
}

function makeInfoWindow(marker, content) {
google.maps.event.addListener(marker, 'click', function () {
    infowindow.setContent(content);
    infowindow.open(map, marker);
});
}

1 个答案:

答案 0 :(得分:0)

事实证明,我正在使用FHRS API的V1。此后已贬值,应使用V2。

您可以在此处找到所有信息: http://api.ratings.food.gov.uk/help