gpt4 book ai didi

ios - 如何将 CLLCoordinate2D lat/long 转换为 NSNumber

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:42 25 4
gpt4 key购买 nike

我正在尝试使用 Yelp API,并且我正在尝试将纬度/经度作为 API 搜索的参数。但是,它不采用 double 类型,它只接受 Objective-C 对象。不了解 Objective-C,您建议 lat 和 long 的参数类型是什么?我尝试了 NSNumber,但是当我尝试将 CLLocationCoordinate2D 类型的纬度/经度坐标转换为采用 double 的 NSNumber 时,其值为 nil

这是我正在使用的 Yelp API:

- (void)queryTopBusinessInfoForTerm:(NSString *)term location:(NSString *)location latitude:(NSNumber *)latitude longitude:(NSNumber *)longitude completionHandler:(void (^)(NSDictionary *topBusinessJSON, NSError *error))completionHandler {

NSLog(@"Querying the Search API with term \'%@\' and location \'%@'", term, location);

//Make a first request to get the search results with the passed term and location
NSURLRequest *searchRequest = [self _searchRequestWithTerm:term location:location latitude:latitude longitude:longitude];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:searchRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

if (!error && httpResponse.statusCode == 200) {

NSDictionary *searchResponseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray *businessArray = searchResponseJSON[@"businesses"];

if ([businessArray count] > 0) {

NSDictionary *firstBusiness = [businessArray firstObject];
NSString *firstBusinessID = firstBusiness[@"id"];
NSLog(@"%lu businesses found, querying business info for the top result: %@", (unsigned long)[businessArray count], firstBusinessID);

[self queryBusinessInfoForBusinessId:firstBusinessID completionHandler:completionHandler];
} else {
completionHandler(nil, error); // No business was found
}
} else {
completionHandler(nil, error); // An error happened or the HTTP response is not a 200 OK
}
}] resume];
}

这是参数

- (NSURLRequest *)_searchRequestWithTerm:(NSString *)term location:(NSString *)location latitude:(NSNumber *) latitude longitude:(NSNumber *)longitude {
NSDictionary *params = @{
@"term": term,
@"location": location,
@"cll": latitude,
@"cll": longitude,
@"limit": kSearchLimit
};

return [NSURLRequest requestWithHost:kAPIHost path:kSearchPath params:params];
}

这是我当前从 YelpAPI 调用查询的 Swift 方法:

func yelpApi() {
var latitude = NSNumber(double: businessStreetAddress.latitude)
var longitude = NSNumber(double: businessStreetAddress.longitude)
var searchTerm: NSString = "Asian Food";
var defaultLocation: NSString = "New York"
var APISample:YPAPISample = YPAPISample();

var requestGroup:dispatch_group_t = dispatch_group_create();


APISample.queryTopBusinessInfoForTerm(searchTerm as String, location: defaultLocation as String, latitude: latitude, longitude: longitude) { (topBusinessJSON: [NSObject: AnyObject]!, error: NSError!) -> Void in
if((error) != nil) {
println("Error happened during the request" + error.localizedDescription);
} else if((topBusinessJSON) != nil) {
println("Top business info",topBusinessJSON);
} else {
println("No business was found");
}

dispatch_group_leave(requestGroup);
}

dispatch_group_wait(requestGroup, DISPATCH_TIME_FOREVER);


}

最佳答案

要将 CLLocationCoordinate2D 转换为 NSNumber,您不能将它放在单个 NSNumber 中。您可以转换为两个 NSNumber 对象,如下所示:

 CLLocationCoordinate2D location; // This is the location you have.
NSNumber *latitude = [NSNumber numberWithDouble:location.latitude];
NSNumber *longitude = [NSNumber numberWithDouble:location.longitude];

使用 Modern ObjC,您可以转换为:

 CLLocationCoordinate2D location; // This is the location you have.
NSNumber *latitude = @(location.latitude);
NSNumber *longitude = @(location.longitude);

然后调用你的

NSURLRequest *searchRequest = [self _searchRequestWithTerm:term location:location latitude:latitude longitude:longitude];

不要与名为 location 的变量混淆,我刚刚给它命名。因为您的函数 searchRequestWithTerm:location:latitude:longitude: 有一个名为 location 的参数,它接受 NSString

这可能对你有帮助。

关于ios - 如何将 CLLCoordinate2D lat/long 转换为 NSNumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616043/

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