Appcelerator - 获取地理位置信息并变成 URL
Appcelerator - Getting geolocation information and turning into a URL
我对 appcelerator 还是很陌生,但我正在尝试用地理定位做一个小实验。我有一些类似于下面的代码,returns 控制台的 long 和 lat。我想要的是获取 long 和 lat 并将它们附加到 URL,例如 http://www.mywebsite.com/lat/long.
我已经尝试创建一个简单的警报来向我显示当前位置,但它只显示警报:[object GeolocationModule]。
有人可以指出正确的方向,以便我可以学到更多吗?谢谢
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');
}
这就是您需要遵循 API 文档的方式:
您可以查看 LocationResults 页面:https://docs.appcelerator.com/platform/latest/#!/api/LocationResults which leads you to LocationCoordinates: https://docs.appcelerator.com/platform/latest/#!/api/LocationCoordinates
在那里你可以看到,你可以使用 e.coords.latitude
或 longitude
来获取值。或者查看控制台输出。它应该向您显示带有键值对的 JSON 输出。
获得值后,您可以创建 HTTP 请求(演示:https://docs.appcelerator.com/platform/latest/#!/guide/HTTPClient_and_the_Request_Lifecycle)并打开您的页面:
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),这是一个很棒的库,可以轻松创建 API 请求
我对 appcelerator 还是很陌生,但我正在尝试用地理定位做一个小实验。我有一些类似于下面的代码,returns 控制台的 long 和 lat。我想要的是获取 long 和 lat 并将它们附加到 URL,例如 http://www.mywebsite.com/lat/long.
我已经尝试创建一个简单的警报来向我显示当前位置,但它只显示警报:[object GeolocationModule]。
有人可以指出正确的方向,以便我可以学到更多吗?谢谢
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');
}
这就是您需要遵循 API 文档的方式:
您可以查看 LocationResults 页面:https://docs.appcelerator.com/platform/latest/#!/api/LocationResults which leads you to LocationCoordinates: https://docs.appcelerator.com/platform/latest/#!/api/LocationCoordinates
在那里你可以看到,你可以使用 e.coords.latitude
或 longitude
来获取值。或者查看控制台输出。它应该向您显示带有键值对的 JSON 输出。
获得值后,您可以创建 HTTP 请求(演示:https://docs.appcelerator.com/platform/latest/#!/guide/HTTPClient_and_the_Request_Lifecycle)并打开您的页面:
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),这是一个很棒的库,可以轻松创建 API 请求