2011-08-11 3 views
0

Google 장소 URL 문자열에 인수를 전달하고 싶습니다. 문자열은URL 문자열에 대입 전달 string objective 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; 

업데이트 지. becaue.when 내가 = @ ""9.281654854 ", 그리고 b = @"- 3.32532 ", 즉 정적 값 즉, 넣어 어떤 위치도 표시하지 않습니다.

내 질문을 업데이트하고 지금 사용하고 있습니다. 질문에 표시된 것과 같은 것과 귀하의 조언을 따르십시오. 그러나 여전히 어떤 위치도 보여주지 않습니다. 위치 데이터에 액세스하려면 확인을 클릭하라고 요청합니다. 확인을 클릭하면 위치가 표시되지 않습니다. 내 문자열은 정적 좌표를 넣고 #define googleUrl = @ "string"으로 사용할 때 괜찮습니다. 그것은 작동합니다. dynimic 좌표로하지만

+0

는 "그러나 작동하지 않는"쓰기이 무엇을 의미합니까? 항상 자신의 질문에 "내 질문에 다른 사람들이 답변 할 수 있도록 충분한 정보가 있습니까?" –

+0

내 질문에 대한 업데이트가 있습니다. plz는 모양을 가지고있다. 죄송합니다. – Mann

+0

그리고 코드를 작성하는 중에 코드를 좀 더 형식화 할 수 있습니다. –

답변

1

이것은 질문에 대한 질문보다 일반적인 매개 변수를 대체하는 일반적인 대답이지만 어쨌든 유스 케이스를 얻지는 못했습니다 ...: 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

@ 마렉, 그것은 오타입니다, 둘 다 무효하고 불필요한, 나는 그것을 편집 할 것입니다 =). – 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 = @ "string"으로 사용할 때 괜찮습니다. 그것은 작동합니다. 그러나 다이 니믹 좌표로는 작동하지 않습니다. – Mann