【问题标题】:I'm developing iPhone web based app我正在开发基于 iPhone 网络的应用程序
【发布时间】:2011-06-28 05:18:06
【问题描述】:
所以当我在我的应用程序的搜索栏中输入内容时,它应该将我的纬度和经度发送到网络服务器,以便它可以返回最近的位置,我可以在其中获取搜索到的标签。我是 Titanium 新手,谁能帮助我?
【问题讨论】:
标签:
iphone
json
geolocation
titanium
【解决方案1】:
您基本上是通过 HTTP 向您的网络服务发送请求并使用结果(从@Muhammad Zeeshan 获取经度/纬度的函数):
var xhr = Titanium.Network.createHTTPClient();
// write file on success
xhr.onload = function(){
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,this.apiFile);
f.write(this.responseData);
};
// error handling
xhr.onerror = function(){
Ti.API.error(this.status + ' - ' + this.statusText);
};
// open the client (and test HTTPS)
xhr.open('GET','http://example.com/api/?longitude=' + longitude + '&latitude=' + latitude);
// send the data
xhr.send();
// read file and return json
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, this.apiFile);
var contents = f.read();
var yourJson = JSON.parse(contents);
在服务器端,您需要一些 Web 服务与之通信(您没有指定在服务器上使用的语言),但我假设您通过 MySQL 数据库获取数据(其他应该类似):
SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($latitude * PI() / 180) * COS(lat * PI() / 180) * COS(($longitude – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `locations` HAVING `distance`<=’10′ ORDER BY `distance` ASC
添加额外的 WHERE 子句以按标签过滤。
【解决方案2】:
如果您使用TiStudio 而不是 TiDeveloper,您可以使用示例项目 GPS 开始。它捆绑在下载中,因此只需启动它并将该代码用作工作副本以供学习。