【问题标题】:Lat Lon through HTTP POST经纬度通过 HTTP POST
【发布时间】:2013-12-17 16:46:45
【问题描述】:

好的,所以我的 iOS 应用需要发送纬度和经度以通过 HTTP POST 方法接收郊区。

这是我获取纬度和经度的代码

float lat = locationManager.location.coordinate.latitude;
float lon = locationManager.location.coordinate.longitude;

我想用这样的 HTTP POST 方法发送它

here is the problem
NSString *post = @"key1=val1&key2=val2";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.nowhere.com/sendFormHere.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

我想知道是否可以将float 部分更改为字符串或类似于HTTP POST 方法中使用的内容

【问题讨论】:

  • 所以问题是我如何将浮点数转换为字符串?之前已经在SO上回答过。顺便说一句,坐标中的纬度和经度字段是 CLLocationDegrees 类型,它是双精度型,而不是浮点型。 float 精度不够。

标签: ios string http-post core-location


【解决方案1】:

您可以创建帖子:

NSString *post = [NSString stringWithFormat:@"lat=%f&lon=%f", lat, lon];

【讨论】:

  • 好的,现在让我们说,我有一个这样的郊区列表; <state name="ACT"> <suburb name="Acton" id="2715" latitude="-35.2773337" longitude="149.1182098"></suburb> <suburb name="Ainslie" id="2712" latitude="-35.2626801" longitude="149.1448231"></suburb></state>我如何获得一些东西来显示我的 Lat Lon 所说的我所在的郊区?
  • 其实更好,我如何以字符串的形式显示POST的结果,如果它像<geocode> <suburb>Acton</suburb> <state>ACT</state> </geocode>一样返回
  • 要解析的 xml 很乱,如果您可以控制格式,请使用字典数组。
  • 我该怎么做?
  • 在数据模型中创建数据模型和方法来解析xml并填充ivars。添加方法以提供所需的任何格式的数据。
【解决方案2】:

当然可以在body中添加float:

NSString *post = [NSString stringWithFormat:@"lat=%f,long%f",lat,lon];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2013-03-23
    • 1970-01-01
    • 2012-07-30
    • 2016-03-25
    相关资源
    最近更新 更多