添加边界到谷歌地图搜索字符串会导致零结果

时间:2013-05-26 07:40:33

标签: ios google-maps google-maps-sdk-ios

我在我的地图应用程序中添加了一个搜索框,这是一个非常简单的地址搜索,只需要很少的选项。

http://maps.googleapis.com/maps/api/geocode/json?address=sydney+grove&sensor=true

我刚刚使用当前的屏幕视口添加了边界参数

http://maps.googleapis.com/maps/api/geocode/json?address=sydney+grove&bounds=51.198083,-0.830125|51.799930,0.576125&sensor=true

它会在粘贴到浏览器时返回结果,但如果在代码中输入则总是nil结果(jsonResponse总是等于nil)

-(void)  doGeocodingBasedOnStringUsingGoogle:(NSString*) searchString {

    GMSCoordinateBounds* bounds=[[self datasource] searchBounds];

    //CREATE LOOKUP STRING
    NSString *lookUpString = [NSString
    stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?
                       address=%@&bounds=%f,%f|%f,%f&sensor=true",
                              searchString,
                              bounds.southWest.latitude,
                              bounds.southWest.longitude,
                              bounds.northEast.latitude,
                              bounds.northEast.longitude];
    lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" "
                                                           withString:@"+"];

    //SEARCH FOR RESULTS
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSError *error = nil;
        NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];
        if (jsonResponse) {

        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];

        self.searchResults = [jsonDict valueForKey:@"results"];

        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableview reloadData];
        });
    });

}

在我添加边界条件之前这段代码没问题,如果我删除就可以了,所以我真的没有想法

1 个答案:

答案 0 :(得分:1)

我认为你需要更换|使用%7C,例如见:

How to make an NSURL that contains a | (pipe character)?

正如在答案的评论中所提到的,您可以考虑使用stringByAddingPercentEscapesUsingEncoding为您转义URL的方法(例如,您不需要用+等替换空格)。< / p>