2016-05-25 15 views
0

私はappceleratorにはまだまだ新しいですが、私はジオロケーションを使って小さな実験をしようとしています。私は以下のようないくつかのコードを持っています。これはコンソールにlongとlatを返します。私が望むのは、longとlatを取得し、それらをURLに追加することです(例:http://www.mywebsite.com/lat/long)。Appcelerator - ジオロケーション情報を取得してURLに変換する

シンプルなアラートを作成して、現在の場所を表示しようとしましたが、警告:[オブジェクトGeolocationModule]だけです。

誰かが私を正しい方向に向けることができるので、もっと学ぶことができますか?

あなたはLocationResultsページを見持つことができます:LocationCoordinatesにあなたを導きhttps://docs.appcelerator.com/platform/latest/#!/api/LocationResultsを:そこhttps://docs.appcelerator.com/platform/latest/#!/api/LocationCoordinates

あなたが見ることができます。これは、あなたはAPIドキュメントを追跡する必要があるかであるあなたに

if (Ti.Geolocation.locationServicesEnabled) { 
Titanium.Geolocation.purpose = 'Get Current Location'; 
Titanium.Geolocation.getCurrentPosition(function(e) { 
    if (e.error) { 
     Ti.API.error('Error: ' + e.error); 
    } else { 
     Ti.API.info(e.coords); 
    } 
}); 
} else { 

    alert('Please enable location services'); 
} 

答えて

2

ありがとうございました値を取得するにはe.coords.latitudeまたはlongitudeを使用できます。または、コンソールの出力を見てください。これは、キーと値のペアを持つJSON出力を表示するはずです。ある

var url = "https://www.appcelerator.com/"+e.coords.longitude+"/"+e.coords.latitude; 
var xhr = Ti.Network.createHTTPClient({ 
    onload: function(e) { 
     // this function is called when data is returned from the server and available for use 
     // this.responseText holds the raw text return of the message (used for text/JSON) 
     // this.responseXML holds any returned XML (including SOAP) 
     // this.responseData holds any returned binary data 
     Ti.API.debug(this.responseText); 
     alert('success'); 
    }, 
    onerror: function(e) { 
     // this function is called when an error occurs, including a timeout 
     Ti.API.debug(e.error); 
     alert('error'); 
    }, 
    timeout:5000 /* in milliseconds */ 
}); 
xhr.open("GET", url); 
xhr.send(); // request is actually sent with this statement 

またはあなたがより多くの要求を使用する場合RESTe(https://github.com/jasonkneen/RESTe)を見て:

あなたが値を持ったらあなたはHTTPリクエスト(デモ:https://docs.appcelerator.com/platform/latest/#!/guide/HTTPClient_and_the_Request_Lifecycle)を作成することができますし、あなたのページを開きますAPIリクエストを簡単に作成できるすばらしいライブラリ

+0

ありがとうございました。私は正しい場所に挿入された緯度/経度で必要なURLで警告を出力する場所関数内に変数を作成することができました。関数の外からこの変数にアクセスする方法はありますか?私はそれを使用してwebviewを読み込む必要があります。私はTi.App.myvariableの名前を使用しようとしましたが、それは機能の外で動作するように見えません。 – Rob88991

+0

もちろん、関数の外側(1行目の上)に変数を作成し、それをgetcurrentpositionの内側に設定することができます。または、 'callURL(long、lat)'のような外部の関数を呼び出し、 'function callURL(long、lat){}'の中にxhrの要素を入れてください – miga

+0

ありがとう、 、 ごめんなさい。どのように変数をgetcurrentposition内に設定するのですか?私はさまざまな方法で試してみたが、どれも動作していないようだ。 – Rob88991

関連する問題