Reputation: 7101
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);
}
Upvotes: 1
Views: 2723
Reputation: 5789
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?
var map;
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
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},{errorCallback:onError});
}
function onPositionReady(position) {
// Execute some more code with user's location
// For Example...
// Apply the position to the map
var location = new Microsoft.Maps.Location(position.coords.latitude,
position.coords.longitude);
map.setView({ zoom: 18, center: location });
// Add a pushpin to the map representing the current location
var pin = new Microsoft.Maps.Pushpin(location);
map.entities.push(pin);
}
function onError(err) {
switch (err.code) {
case 0:
alert("Unknown error");
break;
case 1:
alert("The user said no!");
break;
case 2:
alert("Location data unavailable");
break;
case 3:
alert("Location request timed out");
break;
}
}
Update:
If you want to pass arguments into your callbacks, you can use function binding to override the this
keyword inside your callback and pass arguments that way:
For example, if set up myObject
to contain your arguments, you can pass it like so:
geoLocationProvider.getCurrentPosition({ successCallback: onPositionReady.bind(myObject) }, { errorCallback: onError });
Then, you access myObject in your callback via the this
keyword:
function onPositionReady(position) {
var myProperty = this.yourProperty; // this now refers to myObject
// Execute some more code with user's location
// For Example...
// Apply the position to the map
var location = new Microsoft.Maps.Location(position.coords.latitude,
position.coords.longitude);
map.setView({ zoom: 18, center: location });
// Add a pushpin to the map representing the current location
var pin = new Microsoft.Maps.Pushpin(location);
map.entities.push(pin);
}
Upvotes: 2