작성중....

 tabBar.unselectedItemTintColor = .systemCyan
 tabBar.tintColor = .systemMint
 tabBar.backgroundColor = .black

 

 

'IOS > Thoughts' 카테고리의 다른 글

Thoughts) GeoCoder.geocodeAddressString() 정확히 사용하기!  (0) 2022.09.11

안녕하세요~

 

지도 기능을 추가하면서 경험한 것을 공유하려고 해요.

 

제가 운영하는 앱은 공공데이터 API를 사용해요.

 

근데, 공공 데이터 API에서 위도와 경도 값을 제공하지 않기에, 주소를 입력받아 위도, 경도로 변환하는 과정을 수행하기 위해

위와 같은 함수를 사용해요.

 

위 함수를 사용해서, 성공적으로 지도에 위치를 그려주었어요.

(근데 혹시 버그가 있을 수도 있으니깐 다른 지역도 보았습니다.)

 

 

 

....국내에 있어야할 명심근린공원이 하노이에 있는 결과를 보게 되었어요.

 

 

그래서, 생각을 하게 되었어요.

 

실재로 로그를 찍어보니깐, 경도가 한국이랑 5도에서 6도가 차이가 나더라구요.

 

그래서, 처음에는 If 문을 사용해서 대응을 하려고 했습니다. 

 

 

If 경도<26{

 경도 + 5 
 위도 + 10 

}

 

 

실재로는 대응이 대부분 되었어요. 근데 문제는 "대부분"이라는 것이었어요.

 

그래서, 다른 방법을 모색하기로 했어요.

 

아래는 Apple 문서에서 제공하는 소스코드인데요. 다시 읽어보고 고민을 해보았는데, 아이디어가 떠오르지 않더라구요.

 

심지어, 애플에 건의를 해볼까 생각도 해보았어요 ㅎㅎ

 

 

func getCoordinate( addressString : String, 
        completionHandler: @escaping(CLLocationCoordinate2D, NSError?) -> Void ) {
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(addressString) { (placemarks, error) in
        if error == nil {
            if let placemark = placemarks?[0] {
                let location = placemark.location!
                    
                completionHandler(location.coordinate, nil)
                return
            }
        }
            
        completionHandler(kCLLocationCoordinate2DInvalid, error as NSError?)
    }
}

 

 

그래서, 다른 메소드를 찾기 시작했어요.

그러다가 아래 메소드를 찾았는데요!

 

 

func geocodeAddressString(_ addressString: String, in region: CLRegion?, completionHandler: @escaping CLGeocodeCompletionHandler)
Submits a forward-geocoding request using the specified string and region information.

 

 

해결법을 찾은 것 같은데요!

"특별한 지역을 사용하여" 이 부분이 눈이 확들어왔습니다!!

 

그래서, 저 메소드를 활용해서 지역을 지금 사용자의 위치로 설정을 하니 성공적으로 모두 국내에 표시되었어요!

 

 

 

 

소스 코드)

    func makeAnotation(){
        
        for i in 1..<list.count{
            
            let geoCoder = CLGeocoder()
            let title = list[i].value(forKey: "spotnameforLocation") as? String
            let address = list[i].value(forKey: "addressforLocation") as! String
            let region = CLCircularRegion(center: (locationManager.location?.coordinate)!, radius: 200, identifier: "Place")
            geoCoder.geocodeAddressString(address, in: region) { [self]
                placemarks, error in
                guard let placemarks = placemarks,
                      let longtitude = placemarks.first?.location?.coordinate.longitude,
                      let latitude = placemarks.first?.location?.coordinate.latitude else {return}
                
                let annotation = CustomPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)
                annotation.title = title
                annotation.subtitle = address
                
                self.mapView.addAnnotation(annotation)
                
            }
        }
        
    }

 

 

좋은 하루 보내세요~ 

참고)

 

.

https://developer.apple.com/documentation/corelocation/clgeocoder/1423591-geocodeaddressstring

 

Apple Developer Documentation

 

developer.apple.com

 

'IOS > Thoughts' 카테고리의 다른 글

Thoughts) 역시.. 실기기 테스트를 해야해요.  (0) 2022.10.02

+ Recent posts