gpt4 book ai didi

string - 获取用户位置并将坐标用作 didUpdateLocations 之外的字符串

转载 作者:行者123 更新时间:2023-12-04 15:56:54 26 4
gpt4 key购买 nike

我在用户位置和其余代码方面遇到问题...

我开发了一个代码,可以从 Foursquare API 检索某个位置的餐馆。我要解决的问题是:

这是我用来检索信息的 URL 字符串(纬度和经度是硬编码的,只是为了让事情开始):

https://api.foursquare.com/v2/venues/search?ll=51.51,-0.13&categoryId=4bf58dd8d48988d1c4941735&radius=3000&oauth_token=A5ZDWL2DLXPZCQ3ZJESVOAKDMPQHSNNVWC3UMVOUOXPQHWRT&v=20121105

我将 URL 字符串分为三部分,其中第二部分是当前用户位置。

NSString *firstStringPart = @"https://api.foursquare.com/v2/venues/search?ll=";
NSString *secondStringPart = currentLocationString;
NSString *thirdStringPart = @"&categoryId=4bf58dd8d48988d1c4941735&radius=3000&oauth_token=A5ZDWL2DLXPZCQ3ZJESVOAKDMPQHSNNVWC3UMVOUOXPQHWRT&v=20121105";

然后我想将字符串附加在一起

NSString *connectedUrlString = [[firstStringPart stringByAppendingString:secondStringPart] stringByAppendingString:thirdStringPart];

如果我像这样为第二个字符串部分放置坐标,一切都有效:

NSString *secondStringPart = @"51.51,-0.13";

我还做了一个小测试项目来获取用户位置,它正在使用以下代码:

@interface ViewController ()

@end

@implementation ViewController

@synthesize locationManager, location;


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self setLocationManager:[[CLLocationManager alloc] init]];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];

NSString *lat = [NSString stringWithFormat:@"%.2f", currentLocation.coordinate.latitude];
NSString *lon = [NSString stringWithFormat:@"%.2f", currentLocation.coordinate.longitude];

location = [NSString stringWithFormat:@"%@,%@", lat, lon];

NSLog(@"%@", location);
[locationManager stopUpdatingLocation];

}

使用 NSLog 我得到了我想要的正确输出。

最后,我的问题是,如何在变量中获取用户位置并在 View DidLoad 方法中将其用作 NSString *secondStringPart = currentLocationString;在 [locationManager startUpdatingLocation] 之后

还是我做错了什么?

非常感谢您的帮助,谢谢...

最佳答案

已解决...

我已经解决了这个问题...

我必须将我的代码移动到 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 方法并在其中执行所有操作...

关于string - 获取用户位置并将坐标用作 didUpdateLocations 之外的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13251898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com