2012-04-22 15 views
1

IユーザーBing MapsのAjaxのV7のAPIの例からhereBing MapsのAjaxのAPI v7のジオロケーションコールバック

をユーザの位置を取得するには、コールバックが実行されるまで待つことは可能ですし、私は経度を取得するまで、ために緯度ユーザーの位置、または位置を取得できない場合のエラー? JavaScriptで

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

答えて

2

、私はあなたが明示的に実行を中断し、ここに述べたように終了するコールバックを待つことができるとは思わない: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; 
    } 
} 

更新:設定した場合、例えば、

: あなたのコールバックに引数を渡したい場合は、コールバック内thisキーワードをオーバーライドし、そのように引数を渡すために結合機能を使用することができますあなたの引数を格納するためのmyObjectまで、あなたはそのようにそれを渡すことができます。そして、

geoLocationProvider.getCurrentPosition({ successCallback: onPositionReady.bind(myObject) }, { errorCallback: onError }); 

、あなたはを経由して、あなたのコールバックでmyObjectというアクセスキーワード:

+0

問題は、コールバックにいくつかの変数を設定したいと思っており、私はそれらを渡す方法が見つからなかったということです。私はそれらをグローバル変数として宣言したくありませんでした。私はそれが正しいソリューションだとは思わない。 – glarkou

+0

@salamisもしあなたが正しくあなたをコールバックに変数を渡したいと思いますか?もしそうなら、私の更新された答えを見てください。 –

+0

ありがとう。やってみます。 – glarkou

関連する問題