gpt4 book ai didi

c# - Bing Maps Ajax API v7 地理定位回调

转载 作者:行者123 更新时间:2023-11-30 18:28:24 25 4
gpt4 key购买 nike

我使用 Bing Maps Ajax v7 API 从示例中获取用户位置 here

是否可以等到回调被执行,直到我得到用户位置的经度、纬度或者如果他/她不允许获取位置时出现错误?

    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 中,我认为您不能显式暂停执行并等待回调完成,如下所示:jQuery: wait for function to complete to continue processing?如果您有一些依赖于获取位置回调结果的逻辑,为什么不将该逻辑作为回调的一部分调用呢?您不需要明确地等待它。关于您想完成的事情,您正在寻找这样的东西吗?

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;
}
}

更新:如果你想将参数传递到你的回调中,你可以使用函数绑定(bind)来覆盖回调中的 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);
}

关于c# - Bing Maps Ajax API v7 地理定位回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com