I user Bing Maps Ajax v7 API to get user’s location from the example here
Is it possible to wait until the callback is executed and until I get a longitude, latitude for the user’s position or an error if he/she does not allow to get the position?
function test() {
//Call GetMap() to get position
GetMap();
//Wait until callback is finished
//Do something with user's location (result of the callback)
}
function GetMap()
{
// Set the map options
var mapOptions = {credentials:"Bing Maps Key"};
// Initialize the map
var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), mapOptions);
// Initialize the location provider
var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);
// Get the user's current location
geoLocationProvider.getCurrentPosition({successCallback:displayCenter});
//Execute some more code with user's location
}
function displayCenter(args)
{
// Display the user location when the geo location request returns
alert("The user's location is " + args.center);
}
In JavaScript, I don’t think you can explicitly suspend execution and wait for a callback to finish as noted here: jQuery: wait for function to complete to continue processing? If you have some logic that depends on the result of the get position callback, why not just invoke that logic as part of the callback? You shouldn’t need to explicitly wait for it. In regards to what you want to accomplish, is something like this what you are looking for?
Update:
If you want to pass arguments into your callbacks, you can use function binding to override the
thiskeyword inside your callback and pass arguments that way:For example, if set up
myObjectto contain your arguments, you can pass it like so:Then, you access myObject in your callback via the
thiskeyword: