2

我在我的应用程序中使用 Google Place API。好吧,它工作正常并在某些地方给出响应。但是对于某些地方,它给出了“零结果”。

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%@&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text]];

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSURLConnection *connect=  [[NSURLConnection alloc]initWithRequest:request delegate:self];

谁能给我一个想法,我哪里出错了?

4

1 回答 1

0

如果位置名称无效,它将为您提供零结果,并且在某些情况下,由于字符串中有多余的空格,您将获得 URL 为空。因此,如下所述更新您的代码并检查一下。

NSString *googleApi = [NSString stringWithFormat:@"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%@&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text];

NSString *finalURL = [googleApi  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:finalURL];

NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect=  [[NSURLConnection alloc]initWithRequest:request delegate:self];

它可能对你有用。

于 2012-03-02T12:34:28.573 回答