Refreshing Google Maps API V3 Layers

前端 未结 8 1760
温柔的废话
温柔的废话 2020-12-09 03:12

I\'ve been having trouble getting Google Maps API v3 to update correctly. I\'ve got a javascript timer running that should be refreshing the traffic layer periodic

相关标签:
8条回答
  • 2020-12-09 03:46

    try this code snippet, may be this help you out

    function init() {
        var map,
        trafficLayer,
        mapTimerHandle;
    
        var mapDiv = document.getElementById('map');
    
        map = new google.maps.Map(mapDiv, {
            zoom: 14,
            center: new google.maps.LatLng(40.7127, -74.0059)
        });
    
        trafficLayer = new google.maps.TrafficLayer();
        trafficLayer.setMap(map);
    }
    
    $(document).ready(function () {
        init();
        setInterval(function () {
            google.maps.event.trigger(map, 'resize');
            map.fitBounds();
        }, 2000);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
    
    <div id="map" style="width:600px; height:450px"></div>

    0 讨论(0)
  • 2020-12-09 03:51

    I haven't work with traffic layers yet, so I'm not sure if this will work for you. I read the setZoom(getZoom()) trick but when I need to refresh the tiles (the classic one. The map itself) it didn't work either. I need to show/hide a grid in Gmaps (without reloading the page)

    After searching and testing I got it working with these three lines:

            google.maps.event.trigger(map, 'resize'); // Can't remember if really helps
            map.setZoom( map.getZoom() -1 );    
            map.setZoom( map.getZoom() +1 ); // It won't flicker or make the transition between zoom levels. GMap just ignore the zoom change but redraw the tiles :/
    

    I repeat. I don't know if this works with traffic layers but test it anyways. It kept me awake 2 days.

    0 讨论(0)
提交回复
热议问题