Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标c-希望获得纬度和经度值并写入以获得参数_Objective C_Ios - Fatal编程技术网

Objective c 目标c-希望获得纬度和经度值并写入以获得参数

Objective c 目标c-希望获得纬度和经度值并写入以获得参数,objective-c,ios,Objective C,Ios,这里没有问题——我在谷歌上搜索了一下,但找到了两个相互矛盾的答案。我正在实例化CoreLocation,并希望将纬度和经度写入url,以针对移除服务执行get。我有: if([CLLocationManager locationServicesEnabled]){ self.myLocationManager=[[CLLocationManager alloc] init]; self.myLocationManager.delegate=self; self.myLocationM

这里没有问题——我在谷歌上搜索了一下,但找到了两个相互矛盾的答案。我正在实例化CoreLocation,并希望将纬度和经度写入url,以针对移除服务执行get。我有:

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请求集?是否需要通过AppendingString为StringBy实例化一个新的NSString


thx

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

NSString *urlAsString = [NSString
    stringWithFormat:"http://localhost:3000/?latitude=%g&longitude=%g", 
    location.coordinate.latitude,
    location.coordinate.longitude];
我使用了格式化说明符
%g
,假设您从实例获取值。

CLLocation类(您从位置管理器获取)有一个坐标属性,该属性同时具有纬度和经度

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对象被称为“location”

cool,类似这样的东西看起来很不错。在didUpdateToLocation之后,我需要这样做,对吗?