#9 用RESTful API來訂杯珍奶喝吧!Part-6-手機定位、分店位址及地圖

Tai
彼得潘的 Swift iOS / Flutter App 開發教室
7 min readNov 19, 2022

選好想喝的飲料後,想知道最近的分店離我們所在的位置有多遠,也免驚~

可以透過Google Map API定位我們現在所在位置,列出最近的幾間分店,方便購買飲及拿取飲料哦~

  1. 首先在Storyboard中使用Table View Controller、MKMapView、Label建立我們要的頁面

2. 地圖頁面型別中import MapKit及CoreLocation這2個framework,將型別Conform to Protocol: CLLocationManagerDelegate,設定locationManager的delegate

class StoreInfoTableViewController: UITableViewController, CLLocationManagerDelegate {

locationManger?.delegate = self

3. 在APP設定頁面Info設定使用者位置的存取政策,這一定要設置,不然就是唯一閃退

4. 撰寫手機定位Code

   func fetchUserLocation() {
//定位
locationManger = CLLocationManager() //指派locationManger取得CLLocationManager()功能
locationManger?.requestWhenInUseAuthorization() //尋求使用者是否授權APP得知位置
locationManger?.delegate = self //若是user有移動,可以將透過delegate知道位置顯示
locationManger?.desiredAccuracy = kCLLocationAccuracyBest //user位置追蹤精確程度,設置成最精確位置
locationManger?.activityType = .automotiveNavigation //設定使用者的位置模式,手機會去依照不同設定做不同的電力控制
locationManger?.startUpdatingLocation() //user有移動會啟動追蹤位置(CLLocationManagerDelegate)

//授權同意後取得使用者位置後指派給hereForNow
if let hereForNow = locationManger?.location?.coordinate {
let scaleForX: CLLocationDegrees = 0.01 //精度緯度設置為0.01
let scaleForY: CLLocationDegrees = 0.01

//指派span為MKCoordinateSpan後加入精度緯度的比例
let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: scaleForY, longitudeDelta: scaleForX)
//指派regineForNow為MKCoordinateRegion加入現在的user位置,顯示比例為span
let _ = MKCoordinateRegion(center: hereForNow, span: span)
}
}

5. 有了定位點後,依此點透過locationManager的delegate呼叫Google Map API找出1000m內的分店

   func fetchStoreInfo(_ lat: Double, _ lng: Double) {

let orgUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(lat),\(lng)&radius=1000&keyword=春水堂&language=zh-TW&key=Your APK_KEY"
//網址有中文字,需進行編碼
let strUrl = orgUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let request = URLRequest(url: URL(string: strUrl!)!)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data {
do {
let decoder = JSONDecoder()
let storeInfoDecode = try decoder.decode(StoreInfo.self, from: data)
self.storeInfo = []
if storeInfoDecode.results.count != 0 {
for i in 0...(storeInfoDecode.results.count - 1){
self.storeInfo.append(storeInfoDecode.results[i])
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
if self.storeInfo.count == 0 {
let controller = UIAlertController(title: "店家訊息通知", message: "您所在附近沒有店家", preferredStyle: .alert)
let okAction = UIAlertAction(title: "好的", style: .default)
controller.addAction(okAction)
self.present(controller, animated: true)
}
}
} catch {
print(error)
}
}
}.resume()
}

6. 有了以上2個資料後即可描繪地圖,將定位點及分店位址Marker在地圖上

以上是這個APP小故事,謝謝各位觀賞到最後~

#4 用RESTful API來訂杯珍奶喝吧!Part-1-畫面及功能簡介 — 重來 就從 APPLE APP 開始吧 — Medium

#5 用RESTful API來訂杯珍奶喝吧!Part-2-Web官網整合介紹 — 重來 就從 APPLE APP 開始吧 — Medium

#6 用RESTful API來訂杯珍奶喝吧!Part-3-menu呈現及banner輪播 — 重來 就從 APPLE APP 開始吧 — Medium

#7 用RESTful API來訂杯珍奶喝吧!Part-4-訂單上傳及查詢 — 重來 就從 APPLE APP 開始吧 — Medium

#8 用RESTful API來訂杯珍奶喝吧!Part-5-訂單修改及刪除 — 重來 就從 APPLE APP 開始吧 — Medium

--

--

Tai
彼得潘的 Swift iOS / Flutter App 開發教室

跌跌撞撞走了編碼人生的前半段,一切又得重來~但勇敢站起來,決定從 IOS APP 作為下一個人生段的起點,好好的再走他一段編碼人生!