2011-08-11 50 views
0

我想将参数传递到google位置url字符串。 该字符串是将参数传递到url字符串目标c

@"https://maps.googleapis.com/maps/api/place/search/xml?location=52.577798767,-2.124885567&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcbfhjfghf6bBZe80" 

我没有得到任何答复。虽然我可以获取位置更新。但我想使用这个字符串在

-(void)ParseXML_of_Google_PlacesAPI { NSURL *googlePlacesURL = [NSURL 

URLWithString:googleUrl]; NSData *xmlData = [NSData 

dataWithContentsOfURL:googlePlacesURL]; xmlDocument = [[GDataXMLDocument 

alloc]initWithData:xmlData options:0 error:nil]; NSArray *arr = 

[xmlDocument.rootElement elementsForName:@"result"]; placesOutputArray=[[NSMutableArray 

alloc]init]; for(GDataXMLElement *e in arr) { [placesOutputArray addObject:e]; } 

但不工作。它没有回应。

请建议

更新:

这是我的原代码:

- (void)locationUpdate:(CLLocation *)location { 



googleUrl= [[NSString alloc ]initWithFormat:@"https://maps.googleapis.com/maps/api/place 

/search/xml?location=%f,%f&radius=500&name=money&sensor=false& 
key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcdfsab6bBZe80",location.coordinate.latitude,location.coordinate.longitude]; 

}

更新2

googleUrl= @"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&name=the%20money&sensor=false&key=AIzaSyCcC9pmrisgd9XGOgyhjoHQq37cmcb6bBZe80",a,b; 

这个字符串也不会干活G。因为我把a = @“9.281654854”和b = @“ - 3.32532”,即a和b中的静态值并使用它也不显示任何位置

我已更新我的问题,现在使用问题中所示的同样的事情,并遵循你的建议。但它仍然没有显示任何位置。尽管它要求我点击“确定”以访问位置数据。当我点击确定它不显示任何位置。我的字符串是好的,因为当我把静态坐标,并用作#define googleUrl = @“字符串”。有用。但dynimic坐标它不工作

+0

当你写 “但不工作” 是什么意思?总是问自己,“我的问题是否有足够的信息来帮助别人回答它?” –

+0

我已更新我的问题。看看。并为此而感到遗憾 – Mann

+0

而且,当您处于这种状态时,您可以更好地格式化您的代码。 –

回答

1

这是一个普遍的答案,以替换任何URL上的参数,可能比问题要求的更一般,但是并没有完全得到用例...:P

-(NSURL*) replaceLocation:(NSURL*)url latitude:(float)latitude longitude:(float)longitude { 

    // query to dictionary 
    NSMutableDictionary *query = [NSMutableDictionary dictionary]; 
    for (NSString *param in [[url query] componentsSeparatedByString:@"&"]) { 
     NSArray *queryParam = [param componentsSeparatedByString:@"="]; 
     if([queryParam count] < 2) continue; 
     [query setObject:[queryParam objectAtIndex:1] forKey:[queryParam objectAtIndex:0]]; 
    } 

    // override location 
    [query setObject:[NSString stringWithFormat:@"%f,%f",latitude,longitude] forKey:@"location"]; 

    // encode and reassemble 
    NSMutableArray *queryComponents = [NSMutableArray array]; 
    for (NSString *key in query) { 
     NSString *value = [query objectForKey:key]; 
     key = [key stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
     value = [value stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
     NSString *component = [NSString stringWithFormat:@"%@=%@", key, value]; 
     [queryComponents addObject:component]; 
    } 

    NSString *queryString = [queryComponents componentsJoinedByString:@"&"]; 

    NSMutableString *newUrl = [NSMutableString string]; 
    [newUrl appendFormat:@"%@://%@",[url scheme],[url host]]; 
    if ([url port]!=nil){ 
     [newUrl appendFormat:@":%@",[url port]]; 
    } 
    [newUrl appendFormat:@"%@",[url path]]; 
    if ([url parameterString]!=nil){ 
     [newUrl appendFormat:@";%@",[url parameterString]]; 
    } 
    if (queryString!=nil && [queryString length]>0){ 
     [newUrl appendFormat:@"?%@",queryString]; 
    }  
    if ([url fragment]!=nil){ 
     [newUrl appendFormat:@"#%@",[url fragment]]; 
    } 

    return [NSURL URLWithString:newUrl]; 
} 

用法:

NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/xml?location=52.577798767,-2.124885567&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcbfhjfghf6bBZe80"]; 
NSLog(@"from \n%@ \nto\n%@", [url absoluteString], [[self replaceLocation:url latitude:0.54 longitude:0.56] absoluteString]); 
0

试试这个:

googleUrl = [[NSString alloc] initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcb6bBZe80", location.coordinate.latitude, location.coordinate.longitude]; 

你不会需要指定ab了=)!

BTW,我不确定,但我想你试图显示一个以当前位置为中心的地图视图。你应该使用MKMapView这个。

+0

是'%f,%f'需要和有效的空间吗? –

+0

我也试过这个。但不工作 – Mann

+0

@Marek,这是一个错字,它既无效也不需要,我会编辑=)。 – elslooo

0

这听起来像你想:

googleUrl = [[NSString alloc] initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=bank&sensor=false&key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcb6bBZe80",location.coordinate.latitude, location.coordinate.longitude]; 
+0

我也试过这个。但不能正常工作 – Mann

+0

你会得到什么错误?请详细描述“不工作”的含义。 –

+0

@Manjinder S:它肯定会工作,打印日志和检查。 –

1

使用字符串时,您可以使用%@格式说明,而不是%f。另外不要忘记发布ab

您还需要位置=。所以你可以把它改成

googleUrl= [[NSString alloc ]initWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f &radius=500&types=bank&sensor=false&key=api_key",location.coordinate.latitude,location.coordinate.longitude]; 

别忘了发布googleUrl。

PS:

还可以使用更好的变量名更好的可读性。

+0

我把同样的代码写在这里。但它仍然没有显示任何错误,但也没有显示任何位置。我有更新我的问题,你可以看到我想要做什么 – Mann

+0

@Parveen S:我已经更新了我的问题,现在使用相同的问题,并按照您的建议。但它仍然没有显示任何位置。尽管它要求我点击“确定”以访问位置数据。当我点击确定它不显示任何位置。我的字符串是好的,因为当我把静态坐标,并用作#define googleUrl = @“字符串”。有用。但用动态坐标不起作用 – Mann