1

I am new to Google Maps API. I have an HTML page which call up an event handler and then using the XML return data to population the markers and label. The issue is I can't seem to be able to refresh the labels value. The label just over write itself and sooner or later IE will crash and saying "Stop running this script? A script on this page is causing your web browser to run slowly......."

<!--<script type="text/javascript" src="/js/labeledmarker.js"></script>-->

<script type="text/javascript">

 var map;
      function initialize() {
        var mapDiv = document.getElementById('map-canvas');
        map = new google.maps.Map(mapDiv, {
          center: new google.maps.LatLng(27.896415, -81.843137),
          zoom: 9,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        google.maps.event.addListenerOnce(map, 'tilesloaded', addMarkers);

      }

      function addMarkers() 
 {

     downloadUrl("get_waittime_feed.ashx", function (data) {

         var markers = data.documentElement.getElementsByTagName("marker");
         for (var i = 0; i < markers.length; i++) {
             var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("latitude")),
                                    parseFloat(markers[i].getAttribute("longitude")));
             var markericons = markers[i].getAttribute("markericons");
            var waitER = markers[i].getAttribute("waitER");

             var customIcons = { 0: { icon: new google.maps.MarkerImage('../img/greenbb.png', new google.maps.Size(30, 30)) },
                 1: { icon: new google.maps.MarkerImage('../img/redbb.png', new google.maps.Size(30, 30)) },
                 2: { icon: new google.maps.MarkerImage('../img/hospital.png', new google.maps.Size(35, 35))}
             };
             var icon = {}; if (markericons == '0') {
                 icon = customIcons[0];
             }
             else if (markericons == '1') {
                 icon = customIcons[1];
             }
             else if (markericons == '2') {
                 icon = customIcons[2];
             };

             var marker = new MarkerWithLabel({
                position: latlng,
                map: map,
                icon: icon.icon,
                labelContent: waitER,
                labelAnchor: new google.maps.Point(3, 30),
                labelClass: "labels", // the CSS class for the label
                labelInBackground: false
                });

         }
     });

};

setInterval(addMarkers, 5000);
4

3 回答 3

4

这对我有用

marker.labelContent = 'New Label';

marker.label.setContent();

于 2014-06-02T15:17:41.067 回答
4
markers[i].label.setStyles(); // Force label to redraw (makes update visible)

不要为我工作,但

markers[i].label.draw(); // redraw the label for me
于 2013-05-15T22:08:02.523 回答
3

IE 发出该警告的原因是您实际上并没有更新标记 - 您每次调用时都会创建一组新的标记addMarkers()。如此有效地,地图需要越来越长的时间来刷新,直到超过 IE 对 JavaScript 的时间限制。

与其每次都创建一个新的var marker = new MarkerWithLabel(),不如在第一次调用函数时创建它们,然后在之后的每次调用中更新它们。例如:

totalmarkers = markers.length;
for (i = 0; i < totalmarkers; i++) {
    if (markers[i] == null) { // Create your marker here
        markers[i] = new MarkerWithLabel({
            position: latlng,
            map: map,
            icon: icon.icon,
            labelContent: waitER,
            labelAnchor: new google.maps.Point(3, 30),
            labelClass: "labels", // the CSS class for the label
            labelInBackground: false
            });
    } else { // Update your marker here
        markers[i].labelContent = 'label content';
        markers[i].labelClass = 'label class';
        markers[i].label.setStyles(); // Force label to redraw (makes update visible)
        markers[i].setPosition(googlemapslatlng); // Move the marker to a new position
        markers[i].icon = iconurl; // If you want to change the icon
        markers[i].setShape(); // Force the marker icon to redraw
    }

希望有帮助。另请查看此线程以更新标记标签。

于 2013-04-22T01:34:17.723 回答