2016-09-30 7 views
1

navigator.geolocation.getcurrentpositionを使用して現在のユーザの場所(緯度/経度)を取得しています。ここに私の関数があります:コントローラのナビゲータからユーザの位置を取得する

function userLocation(){ 

    var response = []; 
    if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(p){ 
      // on success 
      response['latitude'] = p.coords.latitude; 
      response['longitude'] = p.coords.longitude; 

      console.log(response); 

     }, function(e){ 
      // on error 
      console.log(e); 
     }); 

    } else { 
     console.log("Geolocation is not supported by this browser."); 
    } 

    return response; 

} 

それは、常に空の配列を返し、私は応答を返すことはできませんのでgetCurrentPosition関数は非同期呼び出しを行うことを理解しています。

私はGoogleのgeocode apiを使用して場所を取得しようとしましたが、それはユーザーのIPアドレスに基づいており、正確ではありません。

これらの緯度/経度を返す関数が必要です。私のコントローラの関数でgetCurrentPositionからの応答を得る方法はありますか?

+0

グローバル変数 '(var pos = {};)を宣言します。 ) '、その変数を設定し、' promises'を使って非同期ジオロケーション呼び出しから値を返しますか? – RamRaider

+0

この関数は非同期です。それはバックグラウンドで実行され、位置を決定します。それが成功した(または失敗した)ときに、あなたのコードが 'return response;'にヒットしてから数秒後に、あなたが渡した無名関数があなたのresponse.latitudeと.longitudeを設定します。あなたは 'console.log(response)'を持っているところでAJAX呼び出しを実行し、簡単なGET/POSTリクエストを使ってコードをサーバに送る必要があります。 –

+0

@ChrisGはい、コントローラのlat/longを取得する方法ですが、userLocation()関数は何を返しますか?navigator.geolocation.getCurrentPosition()からの応答を待つ方法を教えてください。リクエストが成功したかどうかはどのようにわかりますか? – manoj

答えて

0

第3パラメータgetCurrentPositionを使用できます。 以下の例を参照してください。

var response = []; 
var options = { 
    enableHighAccuracy: true, 
    timeout: 5000, 
    maximumAge: 0 
}; 

function success(pos) { 
    response['latitude'] = pos.coords.latitude; 
    response['longitude'] = pos.coords.longitude; 
}; 

function error(err) { 
    // 
}; 

navigator.geolocation.getCurrentPosition(success, error, options); 

また、次のリンクを参照することもできます。

https://www.tutorialspoint.com/html5/geolocation_getcurrentposition.htm

+0

私はコントローラでこれらのlong longを使いたいです。どうやってやるの? – manoj

+0

PHPコントローラでjavascript変数を使用しようとしていますか? – KinjalMistry

+0

私はちょうどこの機能からの応答を私のコントローラで使いたいと思っています。助言がありますか? – manoj

1

のonloadウィンドウに入れgetLocation()機能と1つのポップアップがあなたの場所を許可するように要求されます、あなたが許可した場合、あなたのlattitudeと経度がgrabedされます。

以下のコードを使用してください。

function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } else { 
     console.log("Geolocation is not supported by this browser."); 
    } 
} 

function showPosition(position) { 
    console.log("Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude); 
    $.ajax({ 
     url: 'abc.php', 
     type: 'POST', 
     data: 'latitude='+position.coords.latitude+'&longitude='+position.coords.longitude, 
     success: function(data) { 
     console.log(data); 
     }, 
     error: function(e) { 
     //called when there is an error 
     //console.log(e.message); 
     } 
    }); 
} 

http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

+0

コンソールでレスポンスを取得していますが、コントローラのレスポンスにアクセスする必要があります – manoj

+0

その関数からコントローラのメソッドにajaxを呼び出すことができ、ajaxのリクエストからLatitudeとLongitudeを取得できますコール。 –

+0

これはコントローラーのlat/longを取得する方法ですが、userLocation()関数は何を返しますか?navigator.geolocation.getCurrentPosition()からの応答を待つ方法を教えてください。リクエストが成功したかどうかはどのようにわかりますか? – manoj

-2

response.push()

var response = []; 
if (navigator.geolocation) { 

    navigator.geolocation.getCurrentPosition(function(p){ 

     response.push(p.coords.latitude, p.coords.longitude); 

     console.log(response); 

    }); 

} else { 
    console.log("Geolocation is not supported by this browser."); 
} 
1

を使用して、現在位置の値をプッシュしようと多くのアイデアを得るために、このリンクを参照してくださいここでの結果で何かをする方法は次のとおりです。

function userLocation(cb) { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(p){ 
      // on success 
      cb(null, p.coords); 
     }, function(e){ 
      // on error 
      cb(e, null); 
     }); 
    } else { 
     cb("Geolocation is not supported by this browser.", null); 
    } 
} 

今このような関数を呼び出します:

userLocation(function(err, coords) { 
    if (err) console.log(err); 
    else { 
     // do something with coords 
    } 
}); 
関連する問題