2010-01-25 5 views
3

iphone에서 Google지도에서 검색하려는 위치 이름을 입력하는 옵션을 사용자에게 제공하고 싶습니다. 사용자가 특정지도가 있어야하는 위치 이름을 입력 할 때.텍스트의 nsstring 값을 CLLocationCoordinate2D로 바꾸는 방법 google지도

지금은 해당 객체의 위도와 경도에 coordiante의 값을 고정하여이 작업을 수행하고 있습니다.

CLLocationCoordinate2D location=mapView.userLocation.coordinate; 

location.latitude=19.14; 
location.longitude=73.10; 

이 좌표 값을 텍스트에 입력하고이를 CLLocationCoordinate2D 값으로 변환하는 방법은 무엇입니까?

답변

3

나는 당신의 질문에 완전히 명확하지,하지만 당신은 CLLocationCoordinate2D 내로 있는 NSString 변환하고자하는 경우 다음과 같은 사용할 수 있습니다

{ 
    [self useLocationString:@"19.14,73.10"]; 
} 

- (void) useLocationString:(NSString*)loc 
{ 
    // the location object that we want to initialize based on the string 
    CLLocationCoordinate2D location; 

    // split the string by comma 
    NSArray * locationArray = [loc componentsSeparatedByString: @","];   

    // set our latitude and longitude based on the two chunks in the string 
    location.latitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:0] doubleValue]] autorelease]; 
    location.longitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:1] doubleValue]] autorelease]; 

    // do something with the location 
} 

이 코드는 유효성을 검사하지 않습니다 당신이 할 수있는 문자열의 NSArraycomponentsPeparatedByString에서 돌아 오면 확인할 수 있습니다.

+0

내가 거기서 생각하는 언어가 일치하지 않습니다. '[self useLocationString : @ "19.14,73.10"]' – dreamlax

+0

@dreamlax 고마워 ;;) 내가 자주 내가 어떻게하는지 우스꽝 스럽다. – RedBlueThing

+0

당신은 또한 마지막 줄에 = 대신 ==를 붙입니다;) –

0

다음은보다 현대적인 접근 방식입니다.

- (CLLocationCoordinate2D) get2DCoordFromString:(NSString*)coordString 
{ 
    CLLocationCoordinate2D location; 
    NSArray *coordArray = [coordString componentsSeparatedByString: @","]; 
    location.latitude = ((NSNumber *)coordArray[0]).doubleValue; 
    location.longitude = ((NSNumber *)coordArray[1]).doubleValue; 

    return location; 
} 
관련 문제