ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Google Maps SDK for iOS 정리
    iOS/Swift 2022. 1. 22. 21:03

    안녕하세요!! 이번엔 Swift에서 Google Maps SDK를 사용할 때 어떤 기능이 있는지 정리했습니다.

    이번 글도 노션에서 옮겨와 말이 짧습니다!! 양해해주세용😽


    지도를 사용하는 앱을 출시하기 위해 어떤 지도를 사용할지 고민했다.

     

    국내에서만 사용할 것이라면 카카오나 네이버의 지도를 사용하면 되지만, 내가 생각하고 있는 앱은 해외 여행까지 기록해야 하기에

    Apple Map이나 Google Map중에 하나를 선택해야했다.

     

    Apple Map이 해외에선 좋지만 국내에서의 평은 그다지 좋지 않기에

    국내, 해외 모두 평이 무난한 Google Map을 사용하여 앱을 만들기로 했다.

     

    이 페이지는 앱을 만들기 위해 내가 사용할 레퍼런스들을 정리한 것이다.

    지도

    내 위치 버튼

    mapView.settings.myLocationButton = true
    

    오른쪽 하단의 사용자 위치를 업데이트하는 버튼을 보이게 해준다.

    mapView.isMyLocationEnabled = true
    

    사용자의 현재 위치를 나타낸다.

    지도 제스처

    override func loadView() {
      let camera = GMSCameraPosition.camera(
        withLatitude: 1.285,
        longitude: 103.848,
        zoom: 12
      )
    
      let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
      mapView.settings.scrollGestures = false
      mapView.settings.zoomGestures = false
      self.view = mapView
    }
    
    • scrollGesture - true 일 경우, 사용자는 스와이프 해서 지도 이동 가능
    • zoomGesture - true 일 경우, 사용자는 두 번 탭하거나 두 손가락으로 탭하거나 핀치하여 카메라를 확대 및 축소 할 수 있다. scrollGesture가 활성화 되어있다면, 두 번 탭하거나 핀치하면 카메라를 지정된 지점으로 이동 가능하다.
    • tiltGesture - 카메라를 기울일 수 있을지에 대한 여부
    • rotateGesture - 카메라를 회전할 수 있을지에 대한 여부

    마커

    마커 추가하기

    // 위도와 경도를 담아놓는 positon
    let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
    
    let marker = GMSMarker(position: position)
    marker.title = "Hello World"
    marker.map = mapView
    

    marker.appearAnimation 속성을 kGMSMarkerAnimationPop로 설정해서 지도에 새 마커를 추가하는 애니메이션을 만들 수 있다.

    마커 제거하기

    let camera = GMSCameraPosition.camera(
      withLatitude: -33.8683,
      longitude: 151.2086,
      zoom: 6
    )
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    // ...
    mapView.clear() <- 현재 지도에 있는 모든 오버레이(마커를 포함)를 제거
    

    마커 하나하나를 제거하려면 그냥 그 마커에 nil을 넣으면 된다.

    let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
    let marker = GMSMarker(position: position)
    marker.map = mapView
    // ...
    marker.map = nil
    

    마커 색상 변경

    marker.icon = GMSMarker.markerImage(with: .black)
    

    이벤트

    이벤트를 처리하기 위해서 GMSMapViewDelegate 프로토콜을 채택해야 한다.

    override func loadView() {
      super.loadView()
      let camera = GMSCameraPosition.camera(
        withLatitude: 1.285,
        longitude: 103.848,
        zoom: 12
      )
      let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
      mapView.delegate = self
      self.view = mapView
    }
    
    // MARK: GMSMapViewDelegate
    
    // 탭한 곳의 위도와 경도를 나타낼 수 있는 메소드
    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
      print("You tapped at \\(coordinate.latitude), \\(coordinate.longitude)") 
    }
    
    • mapView:didTapMarker: 마커를 탭한 후 호출되는 메소드
    • mapView:didLongPressAtCoordinate: 특정 좌표에서 길게 누르기 제스처 후에 호출되는 메소드
    • mapView:markerInfoWindow: 마커가 선택되려고 할 때 호출됨. UIView를 반환하는 경우 해당 마커에 사용할 정보 창을 제공한다.
    • mapView:didLongPressInfoWindowOfMarker: 마커의 정보 창을 길게 누른 후 호출되는 메소드
    • idleAtCameraPosition: 미해결 제스처 또는 애니메이션이 완료된 후(또는 카메라가 명시적으로 설정된 후) 호출되는 메소드

    댓글

Designed by Tistory.