Noob的问题-我用Google搜索,但提出了几个矛盾的答案。我正在实例化CoreLocation,并想将纬度和经度写入url中以获取针对remove服务的信息。我有:

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?

谢谢

最佳答案

使用字符串格式可能会更容易:

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

假设您从 %g 实例获取值,我使用了格式说明符CLLocation

关于objective-c - objective-c -想要获取经度和纬度值并写入GET参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9730707/

10-12 06:06