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

标签 c# javascript jquery bing-maps

我使用 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/

相关文章:

javascript - 除了 Window.location.href 之外还有其他方法吗?

php - 在 MySQL php 中使用 jQuery

jquery - 这个事件处理程序有什么问题?

javascript - 当在 AJAX、Jquery 中使用 GET 更新数据库时,如何反射(reflect)表上的更新

c# - 将两列的值合并到第三列

c# - 使用 Shibboleth 的 asp.net MVC 身份验证

javascript - 如何用D3添加一个简单的圆弧

c# - 如何从 msbuild 获取构建日志?

c# - 如何使用 Microsoft Graph .NET 客户端库清除字段

javascript - Mobx 返回 ObservableObjectAdministration 而不是我的对象