关闭后在信息窗口内更改和保存内容

时间:2017-06-13 09:08:48

标签: javascript json google-maps google-maps-api-3 infowindow

我有一张地图,可以对从JSON检索到的位置进行地理编码,并在地图上粘贴标记。

标记还有一个显示标记名称和等级的信息窗口。

infowindow中有一个按钮,当您单击按钮时会增加评级计数器。

从JSON检索初始计数器。

令人讨厌的是,当信息窗口关闭并重新打开时,计数器值会重置。

我尝试使用所有标记评级填充对象,在单击按钮时更新对象,然后从infowindow的contentString内的对象输出评级。这不起作用,重新打开infowindow后显示相同的初始评级计数器。

我的代码如下,任何帮助都将不胜感激。

var map;
var geocoder;
var infowindow;
var rating;

function initMap() {
    var mapElement = document.getElementById("map_canvas");

    geocoder = new google.maps.Geocoder();

    var mapOptions = {
        center: new google.maps.LatLng(53.481949, -2.237430),
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    infowindow = new google.maps.InfoWindow();

    map = new google.maps.Map(mapElement, mapOptions);
}

$(document).on('click', '#been-there', function(event){
    var entryID = $(this).data('entry-id');
    var rating = $(this).data('rating');
    var dataString = 'id='+ entryID;

    rating++;

    $.ajax({
        type: "POST",
        url: "db-been-there.php",
        data: dataString,
        cache: false,
    }).success(function(html){
        console.log(html);
    }).fail(function(html){
        console.log(html);
    });

    $('#rating').html(rating);

});

$.getJSON("db-output.php", function(dbJSON) {
    $.each(dbJSON, function(key, data) {

        var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + data.lat + "," + data.lng + "&result_type=sublocality|political&key=AIzaSyCsY-LOPL7dGbA1ShBEu7zMO8_GaSBA3Vg";

        $.getJSON(url, function(reverseGeo) {
            var postal_town = reverseGeo.results[0].formatted_address;

            geocoder.geocode( { 'address': postal_town}, function(results, status) {
                if (status == 'OK') {

                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location,
                        title: data.name
                    });

                    var contentString = '<div id="content">'+
                        '<h1>' + data.name + '</h1>' +
                        '<button id="been-there" data-rating="' + data.rating + '" data-entry-id="' + data.id + '">Seen it</button>' +
                        '<p><strong>Rating: </strong><span id="rating">' + data.rating + '</span></p>' +
                        '</div>';

                    marker.addListener('click', function() {
                        infowindow.setContent(contentString);
                        infowindow.open(map, marker);
                    });

                } else {
                    alert('Geocode was not successful for the following reason: ' + status);
                }
            });
        });
    });
});

编辑:已解决的代码和JSFiddle

已解决的JSFiddle https://jsfiddle.net/BleepyEvans/3czqmqav/

var locations = [
    {
        "id": "1",
        "name": "Location 1",
        "lat": "53.408371",
        "lng": "-2.991573",
        "rating": "17"
    },
    {
        "id": "2",
        "name": "Location 2",
        "lat": "53.789528",
        "lng": "-2.969411",
        "rating": "8"
    },
]

var map;
var geocoder;
var infowindow;
var rating;
var markers = [];

function initMap() {
    var mapElement = document.getElementById("map_canvas");

    geocoder = new google.maps.Geocoder();

    var mapOptions = {
        center: new google.maps.LatLng(53.481949, -2.237430),
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    infowindow = new google.maps.InfoWindow();

    map = new google.maps.Map(mapElement, mapOptions);
}

$(document).on('click', '#been-there', function(event){
    var entryID = $(this).data('entry-id');
    var dataString = 'id='+ entryID;

    rating = markers[entryID].rating;
    rating++;

    markers[entryID].rating = rating;

        console.log(markers[entryID].rating);

    $('#rating').html(rating);

});

$.each(locations, function(key, data) {

    var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + data.lat + "," + data.lng + "&result_type=sublocality|political&key=AIzaSyCsY-LOPL7dGbA1ShBEu7zMO8_GaSBA3Vg";

    $.getJSON(url, function(reverseGeo) {
        var postal_town = reverseGeo.results[0].formatted_address;

        geocoder.geocode( { 'address': postal_town}, function(results, status) {
            if (status == 'OK') {

                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: data.name,
                    rating: data.rating
                });

                markers[data.id] = marker;

                console.log(markers[data.id].rating);

                marker.addListener('click', function() {
                        var contentString = '<div id="content">'+
                    '<h1>' + data.name + '</h1>' +
                    '<button id="been-there" data-rating="' + data.rating + '" data-entry-id="' + data.id + '">Been There</button>' +
                    '<p><strong>Rating: </strong><span id="rating">' + markers[data.id].rating + '</span></p>' +
                    '</div>';

                    infowindow.setContent(contentString);
                    infowindow.open(map, marker);
                });

            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    });
});

1 个答案:

答案 0 :(得分:1)

我不是仅创建一个HTML字符串,其中包含您分配给infowindow的评级数据,而是将评级值添加到Marker对象本身。

var marker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location,
    title: data.manufacturer_name,
    rating: data.rating
});

将这些标记保存在其ID上的对象中:

markers[data.id] = marker;

然后,当用户单击该按钮时,请更新该值:

$(document).on('click', '#been-there', function(event){
    var entryID = $(this).data('entry-id');
    rating = markers[entryID].rating;
    rating++;

并更新标记上的值:

markers[entryID].rating = rating;

如果它无法直接访问标记上的rating属性,则可能需要使用:

markers[entryID].get('rating');
markers[entryID].set('rating', rating);