客观的c – 想要获取纬度和经度值并写入GET参数

Noob问题在这里 – 我GOOGLE了,但想出了一些矛盾的答案。 我正在实例化CoreLocation,并想要写入经纬度到一个URL来做一个反对删除服务。 我有:

if([CLLocationManager locationServicesEnabled]){ self.myLocationManager=[[CLLocationManager alloc] init]; self.myLocationManager.delegate=self; self.myLocationManager.purpose = @"To provide functionality based on user's current location"; [self.myLocationManager startUpdatingLocation]; }else{ NSLog(@"Location services are not enabled"); } NSString *urlAsString=@"http://localhost:3000/"; urlAsString = [urlAsString stringByAppendingString:@"?latitude=my-latitude"]; urlAsString = [urlAsString stringByAppendingString:@"&longititude=my-longitude"]; NSURL *url=[NSURL URLWithString:urlAsString]; 

我如何正确地写入URL请求集的纬度和经度? 我需要为stringByAppendingString实例化一个新的NSString吗?

谢谢

使用string格式可能更容易:

 NSString *urlAsString = [NSString stringWithFormat:"http://localhost:3000/?latitude=%g&longitude=%g", location.coordinate.latitude, location.coordinate.longitude]; 

我已经使用了格式化说明符%g假设您从CLLocation实例中获取值。

CLLocation类(这是你从位置pipe理器得到的)有一个同时具有经度和纬度的坐标属性。

 NSLog(@"Current Latitude: %f, Current Longitude: %f",location.coordinate.latitude,location.coordinate.longitude); 

会输出这两个假设在你的代码:

 urlAsString = [urlAsString stringByAppendingString:@"?latitude=my-latitude"]; 

你想改变一个数字的纬度,所有你需要做的是

 NSString *urlAsString = [NSString stringWithFormat:@"http://localhost:3000/?latitude=%f&longitude=%f",location.coordinate.latitude,location.coordinate.longitude]; 

这是考虑到你的CCLocation对象被称为“位置”