C# Bing映射Ajax API v7地理位置回调
I用户Bing映射Ajax v7 API以从示例中获取用户的位置 如果用户不允许获取位置,是否可以等待回调执行,直到我获取用户位置的经度、纬度或错误C# Bing映射Ajax API v7地理位置回调,c#,javascript,jquery,bing-maps,C#,Javascript,Jquery,Bing Maps,I用户Bing映射Ajax v7 API以从示例中获取用户的位置 如果用户不允许获取位置,是否可以等待回调执行,直到我获取用户位置的经度、纬度或错误 function test() { //Call GetMap() to get position GetMap(); //Wait until callback is finished //Do something with user's location (r
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);
}
在JavaScript中,我不认为您可以显式地挂起执行并等待回调完成,如这里所述:如果您有一些依赖于get position回调结果的逻辑,为什么不将该逻辑作为回调的一部分来调用呢?你不需要明确地等待它。关于你想要完成的事情,像这样的事情是你想要的吗
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;
}
}
更新:
如果要将参数传递到回调中,可以使用函数绑定覆盖回调中的this
关键字,并以这种方式传递参数:
例如,如果将myObject
设置为包含参数,则可以按如下方式传递:
geoLocationProvider.getCurrentPosition({ successCallback: onPositionReady.bind(myObject) }, { errorCallback: onError });
然后,通过this
关键字访问回调中的myObject:
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);
}
问题是我想在回调中包含一些变量,但我没有找到传递它们的方法。我不想将它们声明为全局变量。我认为这不是正确的解决方案。@salamis如果我理解正确,您想将变量传递到回调中吗?如果是的话,请看我的最新答案。非常感谢你的朋友。我试试看。