Rf-10 學習利用 iOS SDK 無view垃圾車回報

做一個小小垃圾車位置回報

放上練習的東西~~

存粹練習用 iOS SDK!!

程式使用

// 定義一個結構來保存垃圾車和人員的座標資訊
struct CreateTruckAndPeopleCoordinate {
var truckLatitude = 0.0, truckLongitude = 0.0 // 垃圾車的緯度和經度
var peopleLatitude = 0.0, peopleLongitude = 0.0 // 人員的緯度和經度
}

先定義一個垃圾車與人員的緯度和經度初始~~

// 計算兩個座標之間的距離
func distanceConversion(pLa peopleLatitude: Double, pLo peopleLongitude: Double, tLa truckLatitude: Double, tLo truckLongitude: Double) -> String {
let conversion = sqrt(pow(peopleLatitude - truckLatitude, 2) + pow(peopleLongitude - truckLongitude, 2)) * 1000

return String(format: "%.1f", conversion) // 將計算結果格式化為字串並返回
}

創建一個算兩點座標的func這邊只為了計算兩點距離,所以並未每條馬路計算,這邊我使用String(format: “%.1f”, conversion) 來做小數點後幾位format之前都使用round()~~

let peopleCoordinate1 = CreateTruckAndPeopleCoordinate(peopleLatitude: 25.04886, peopleLongitude: 121.45409) // 人員座標
let truckCoordinate1 = CreateTruckAndPeopleCoordinate(truckLatitude: 25.047902, truckLongitude: 121.452562) // 垃圾車座標

再來這邊先創造peopleCoordinate1 和 truckCoordinate1因為是無view,所以沒有做任何輸入的元件,一開始創就先寫入座標~~

// 計算垃圾車和人員之間的距離
var conversionInput = distanceConversion(pLa: peopleCoordinate1.peopleLatitude, pLo: peopleCoordinate1.peopleLongitude, tLa: truckCoordinate1.truckLatitude, tLo: truckCoordinate1.truckLongitude)

創完垃圾車truckCoordinate1跟peopleCoordinate1,就可以開始使用剛剛創的function distanceConversion()計算兩點距離~~

// 創建一個MKMapView實例,用於顯示地圖
let MapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))

// 創建MKPointAnnotation實例來標記人員和垃圾車的位置
let people1Point = MKPointAnnotation()
let truck1Point = MKPointAnnotation()

這時先創立個MKMapView與MKPointAnnotation(),MKMapView的width跟height 都為500,看起來會比較大一點~~

MapView.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: peopleCoordinate1.peopleLatitude, longitude: peopleCoordinate1.peopleLongitude), latitudinalMeters: 1000, longitudinalMeters: 1000)


// 設定人員和垃圾車的標題和座標
people1Point.title = "等丟垃圾人員1"
people1Point.coordinate = CLLocationCoordinate2D(latitude: peopleCoordinate1.peopleLatitude, longitude: peopleCoordinate1.peopleLongitude)
truck1Point.title = "垃圾車1"
truck1Point.coordinate = CLLocationCoordinate2D(latitude: truckCoordinate1.truckLatitude, longitude: truckCoordinate1.truckLongitude)


// 將標記加入地圖
MapView.addAnnotation(people1Point)
MapView.addAnnotation(truck1Point)

PlaygroundPage.current.liveView = MapView

再來先定義個MKCoordinateRegion我設定latitudinalMeters和longitudinalMeters各一公里範圍~~

使用CLLocationCoordinate2D給予people1Point.coordinate座標和truck1Point.coordinate座標~~

最後把people1Point和truck1Point圖標使用.addAnnotation()加到MapView上

使用PlaygroundPage.current.liveView 來顯示地圖再Playground右邊~~

let TodayNow = Date()
let TodayDateFormatter = DateFormatter()
TodayDateFormatter.dateFormat = "今日為 MM月dd日 HH點mm分ss秒"

let TodayDateString = TodayDateFormatter.string(from: TodayNow)
let soundNotify = AVSpeechUtterance(string: "現在時間\(TodayDateString)垃圾車距離您大約\(conversionInput)公尺")
let NotifySynthesizer = AVSpeechSynthesizer()
soundNotify.voice = AVSpeechSynthesisVoice(language: "zh-TW") // 設定通知語音的語言為中文
soundNotify.pitchMultiplier = 0.9 // 設定通知語音的音調
soundNotify.rate = 0.6 // 設定通知語音的速率

// 系統語音講話
NotifySynthesizer.speak(soundNotify)

我使用以下網站把時間Format成,”今日為 MM月dd日 HH點mm分ss秒”~~

把這些字串跟距離都放置AVSpeechUtterance,但很無奈這版本好像有bug沒辦法播放語音好想聽一下報時報距離~~

/ 播放垃圾車駛來的音樂
let url = URL(string: "https://drive.google.com/uc?export=download&id=1PklN84VvWzTMH80hzk1Ny2X9aXCUJoTr")
let player = AVPlayer(url: url!)
player.play()

// 列印出垃圾車和人員之間的距離和現在的時間
print("現在時間 \(TodayDateString) 垃圾車距離您大約 \(conversionInput) 公尺")

最後就是垃圾車快到的時候要放播放垃圾車來的音樂~~

這邊原本是想做成座標移動快到人的地方時後,才播放垃圾車音樂,但哈哈一直想一直加這樣寫不完加上還沒學到迴圈,雖然語法知道但先慢慢一步一步來不要一直想登天先打好基礎,最後顯示時間跟距離

等到後面學到元件的連結使用和迴圈感覺可以回來把code改的完整一點^^

GitHub 連結

--

--