Eric
Eric

Reputation: 152

Error showing traffic data with bingmaps 8

I'm programatically generating HTML to show bing maps. The following generated HTML correctly marks the address, but the showTraffic() part seems to have no effect. For the sake of privacy, I've altered the address, omitted the bing-maps credentials, and truncated the end (which I've established is working) but otherwise this is the actual html generated.

<!DOCTYPE html> 
<html> 
<head>
<title>Job Site--Map</title> 
<meta charset="utf-8" /> 
<script type="text/javascript"> 
var map, searchManager,trafficManager; 
function GetMap() { 
    map = new Microsoft.Maps.Map("#myMap", { 
        credentials: "*** VALID CREDENTIALS OMITTED HERE ***" 
    });         
    geocodeQuery("1000 Dairy Ashford, Houston, TX 77077");      
    showTraffic();      
}
function showTraffic() {
    if (!trafficManager) { 
        Microsoft.Maps.loadModule("Microsoft.Maps.Traffic", function () { 
            trafficManager = new Microsoft.Maps.Traffic.TrafficManager(map);  
        }); 
    }
    trafficManager.show();
}   
function geocodeQuery(query) { 
    if (!searchManager) { 
        Microsoft.Maps.loadModule("Microsoft.Maps.Search", function () { 
            searchManager = new Microsoft.Maps.Search.SearchManager(map); 
            geocodeQuery(query); 
        }); 
....

Upvotes: 0

Views: 54

Answers (1)

rbrundritt
rbrundritt

Reputation: 18023

The issue is that the traffic module is loaded asynchronous, but your code is synchronous, thus the trafficManager is null when you call the show function. Here is a modified version of this function for you.

function showTraffic() {
    if (!trafficManager) { 
        Microsoft.Maps.loadModule("Microsoft.Maps.Traffic", function () { 
            trafficManager = new Microsoft.Maps.Traffic.TrafficManager(map); 
            trafficManager.show(); 
        }); 
    }else{
        trafficManager.show();
    }
} 

Upvotes: 1

Related Questions