我正在开发基于iPhone网络的应用程序

问题描述 投票:-2回答:2

因此,当我在我的应用程序的搜索栏中输入内容时,它应该将我的经度和经度发送到Web服务器,这样它就可以返回到我可以获取搜索到的标记的最近的位置。我是Titanium的新手,所以有人可以帮助我吗?

iphone json geolocation titanium
2个回答
0
投票

您基本上是通过HTTP向您的Web服务发送请求并使用结果(从@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子句以按标签过滤。


0
投票

如果您使用TiStudio而不是TiDeveloper,您可以使用示例项目GPS开始。它已捆绑在下载中,因此只需启动它并将该代码用作工作副本即可学习。

© www.soinside.com 2019 - 2024. All rights reserved.