菜鸟教程小白 发表于 2022-12-12 15:54:11

ios - 如何将 CLLCoordinate2D 纬度/经度转换为 NSNumber


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

<p>这是我正在使用的 Yelp API:</p>

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

NSLog(@&#34;Querying the Search API with term \&#39;%@\&#39; and location \&#39;%@&#39;&#34;, term, location);

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

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

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

NSDictionary *searchResponseJSON = ;
NSArray *businessArray = searchResponseJSON[@&#34;businesses&#34;];

if ( &gt; 0) {

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

    ;
} 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];
}
</code></pre>

<p>这是参数</p>

<pre><code>- (NSURLRequest *)_searchRequestWithTerm:(NSString *)term location:(NSString *)location latitude:(NSNumber *) latitude longitude:(NSNumber *)longitude {
NSDictionary *params = @{
                     @&#34;term&#34;: term,
                     @&#34;location&#34;: location,
                     @&#34;cll&#34;: latitude,
                     @&#34;cll&#34;: longitude,
                     @&#34;limit&#34;: kSearchLimit
                     };

return ;
}
</code></pre>

<p>这是我当前从 YelpAPi 调用查询的 Swift 方法:</p>

<pre><code>func yelpApi() {
    var latitude = NSNumber(double: businessStreetAddress.latitude)
    var longitude = NSNumber(double: businessStreetAddress.longitude)
    var searchTerm: NSString = &#34;Asian Food&#34;;
    var defaultLocation: NSString = &#34;New York&#34;
    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: !, error: NSError!) -&gt; Void in
      if((error) != nil) {
            println(&#34;Error happened during the request&#34; + error.localizedDescription);
      } else if((topBusinessJSON) != nil) {
            println(&#34;Top business info&#34;,topBusinessJSON);
      } else {
            println(&#34;No business was found&#34;);
      }

      dispatch_group_leave(requestGroup);
    }

    dispatch_group_wait(requestGroup, DISPATCH_TIME_FOREVER);


}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>要将 <code>CLLocationCoordinate2D</code> 转换为 <code>NSNumber</code>,您不能将其包含在单个 <code>NSNumber</code> 中。您可以转换为两个 <code>NSNumber</code> 对象,如下所示:</p>

<pre><code> CLLocationCoordinate2D location; // This is the location you have.
NSNumber *latitude = ;
NSNumber *longitude = ;
</code></pre>

<p>使用 Modern ObjC,您可以转换为:</p>

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

<p>调用你的</p>

<pre><code>NSURLRequest *searchRequest = ;
</code></pre>

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

<p>这可能会对你有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何将 CLLCoordinate2D 纬度/经度转换为 NSNumber,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31616043/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31616043/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何将 CLLCoordinate2D 纬度/经度转换为 NSNumber