Skip to content

Commit

Permalink
Feat: Tracking user location and fetch near marts
Browse files Browse the repository at this point in the history
- Tracking user location
- Fetch near marts within the distance that user sets
- Add info window above the marker to indicate the title of a mart
  • Loading branch information
jinios committed Jun 15, 2019
1 parent b1ff9b3 commit 923049a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 45 deletions.
7 changes: 7 additions & 0 deletions MartHoliday/MartHoliday/Utility/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension Notification.Name {
static let mapViewTapped = Notification.Name("mapViewTapped")
static let connectionStatus = Notification.Name("connectionStatus")
static let apiErrorAlertPopup = Notification.Name("apiErrorAlertPopup")
static let completeFetchNearMart = Notification.Name("completeFetchNearMart")
}

enum AppColor: CustomStringConvertible {
Expand Down Expand Up @@ -265,3 +266,9 @@ extension UIDevice {
}

}

extension Double {
func truncate(places : Int)-> Double {
return Double(floor(pow(10.0, Double(places)) * self) / pow(10.0, Double(places)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ class DetailViewController: RechabilityDetectViewController, SFSafariViewControl

private func setNavigationItem() {
self.navigationController?.navigationBar.isTranslucent = false

self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
// 브랜치이름 Title 설정
guard let branchData = self.branchData else { return }

self.navigationItem.title = branchData.branchName
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = makeTextWithAttributes(fontSize: 24)

starButton = StarBarButton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,30 @@ class LocationSearchViewController: IndicatorViewController, NMFMapViewDelegate
var searchDistance: SearchDistance?

var locationOverlay: NMFLocationOverlay?
var isFetchEnable = true


var currentState: State = .disabled

var infoWindow = NMFInfoWindow()
var defaultInfoWindowImage = NMFInfoWindowDefaultTextSource.data()

override func viewDidLoad() {
super.viewDidLoad()
self.userLocation = self.locationOverlay?.location
naverMapView.delegate = self
self.settingDistance = 3

naverMapView.addObserver(self, forKeyPath: "positionMode", options: [.new], context: nil)
NotificationCenter.default.addObserver(self, selector: #selector(showErrorAlert), name: .apiErrorAlertPopup, object: nil)
}

@objc private func showErrorAlert() {
DispatchQueue.main.async {
self.presentErrorAlert(type: .DisableNearbyMarts)
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setNavigationBar()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(changeTrackingStatus), name: .completeFetchNearMart, object: nil)

naverMapView.positionMode = .direction
naverMapView.mapView.logoAlign = .rightTop

startIndicator()

let mainQueue = DispatchQueue.main
let deadline = DispatchTime.now() + .seconds(2)

mainQueue.asyncAfter(deadline: deadline) {

let lng = self.locationOverlay?.location.lng ?? 0
Expand All @@ -85,15 +76,58 @@ class LocationSearchViewController: IndicatorViewController, NMFMapViewDelegate

}

@objc private func showErrorAlert() {
DispatchQueue.main.async {
self.presentErrorAlert(type: .DisableNearbyMarts)
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setNavigationBar()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}

@objc func changeTrackingStatus() {
naverMapView.positionMode = .disabled
}

private func setNavigationBar() {
navigationController?.navigationBar.barTintColor = UIColor.appColor(color: .mint)
navigationController?.navigationBar.isTranslucent = false
// To remove 1px line of under the navigationBar
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.prefersLargeTitles = false
self.navigationItem.title = "내 주변 마트 검색"
}

var settingDistance: Int? {
didSet {
let distanceSettingButton = UIButton(type: .custom)

let buttonTitle = NSAttributedString(string: "\(self.settingDistance ?? 2)km",
attributes: [.font: UIFont(name: "NanumSquareRoundOTF", size: 13.5)?.bold(),
.foregroundColor: UIColor.white])

distanceSettingButton.setAttributedTitle(buttonTitle, for: .normal)
distanceSettingButton.setImage(UIImage(named: "focus"), for: .normal)
distanceSettingButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -4, bottom: 0, right: 0)
distanceSettingButton.titleEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 0, right: 0)
distanceSettingButton.layer.borderColor = UIColor.white.cgColor
distanceSettingButton.layer.borderWidth = 0.5
distanceSettingButton.layer.cornerRadius = 8.0
distanceSettingButton.clipsToBounds = true

distanceSettingButton.addTarget(self, action: #selector(changeSearchDistance), for: .touchUpInside)
distanceSettingButton.frame = CGRect(x: 0, y: 0, width: 62, height: 28)

let homeBarButton = UIBarButtonItem(customView: distanceSettingButton)
self.navigationItem.setRightBarButtonItems([homeBarButton], animated: false)
}
}

@objc func changeSearchDistance() {

}

}

Expand Down Expand Up @@ -124,43 +158,62 @@ extension LocationSearchViewController {

func fetchNearMarts(from geoPoint: NMGLatLng) {
let distance = self.searchDistance ?? .near

DistanceSearch.fetch(geoPoint: geoPoint,
distance: distance) { (branchRawData) in
let branches = BranchList(branches: branchRawData)
self.showMarkers(of: branches)
DispatchQueue.main.async {
let branches = BranchList(branches: branchRawData)
self.showMarkers(of: branches) // 여기서 main thread로?
NotificationCenter.default.post(name: .completeFetchNearMart, object: nil, userInfo: nil)
}
}
}

private func showMarkers(of branches: BranchList) {
let cameraUpdate = NMFCameraUpdate(zoomTo: REDUCTION_MAP_ZOOM_MAX)
cameraUpdate.animation = .easeOut
naverMapView.mapView.moveCamera(cameraUpdate)

branches.branches.forEach({ (mart) in
let marker = NMFMarker()
marker.iconImage = NMF_MARKER_IMAGE_PINK
marker.iconImage = NMF_MARKER_IMAGE_LIGHTBLUE
marker.width = 23
marker.height = 30
marker.position = NMGLatLng(lat: mart.latitude, lng: mart.longitude)
marker.userInfo = ["branch": mart]

marker.touchHandler = { (overlay) in
marker.touchHandler = { [weak self] (overlay) in
if let marker = overlay as? NMFMarker {
print(marker.position.lat)
if let nextVC = self?.storyboard?.instantiateViewController(withIdentifier: "detailVC") as? DetailViewController {
nextVC.branchData = marker.userInfo["branch"] as? Branch
let displayName = (marker.userInfo["branch"] as? Branch)?.displayName() ?? "마트"
self?.defaultInfoWindowImage.title = displayName
self?.infoWindow.dataSource = self?.defaultInfoWindowImage
self?.infoWindow.open(with: marker, align: .top)
self?.infoWindow.touchHandler = { [weak self] (overlay) in
self?.infoWindow.close()
self?.navigationController?.pushViewController(nextVC, animated: true)
return true
}
}
}
return false // didTapMapView
}
marker.mapView = self.naverMapView.mapView
})
}


// MARK: - MapView Delegate

func didTapMapView(_ point: CGPoint, latLng latlng: NMGLatLng) {
let mapCenter = NMFCameraPosition(NMGLatLng(lat: latlng.lat, lng: latlng.lng), zoom: DEFAULT_MAP_ZOOM)
let cameraUpdate = NMFCameraUpdate(scrollTo: NMGLatLng(lat: latlng.lat, lng: latlng.lng))
cameraUpdate.animation = .easeOut
cameraUpdate.animationDuration = 0.5
DispatchQueue.main.async {
self.naverMapView.mapView.moveCamera(NMFCameraUpdate(position: mapCenter))
self.naverMapView.mapView.moveCamera(cameraUpdate)
}
}

}

extension Double {
func truncate(places : Int)-> Double {
return Double(floor(pow(10.0, Double(places)) * self) / pow(10.0, Double(places)))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit
import NMapsMap

public let DEFAULT_MAP_ZOOM: Double = 15.0
public let REDUCTION_MAP_ZOOM_MIN: Double = 12.0
public let REDUCTION_MAP_ZOOM_MAX: Double = 9.0
public let DEFAULT_MAP_MARKER_IMAGE: NMFOverlayImage = NMF_MARKER_IMAGE_LIGHTBLUE

class MapViewController: UIViewController {
Expand Down Expand Up @@ -52,13 +54,6 @@ class MartMapView: UIView {

convenience init(frame: CGRect, center: GeoPoint) {
self.init(frame: frame)
/*
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5).isActive = true
button.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
*/

mapView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(mapView)
Expand Down

0 comments on commit 923049a

Please sign in to comment.