我正在使用用户的时区来获取大致位置,我仅使用它来根据日出或日落设置我的应用程序主题和地图样式(它是一个导航应用程序)。
我使用下面的函数并有一个 plist '查找表'。查找表中的坐标是该时区的中心坐标。所以,如果用户在他自己的时区之外,它会给你“错误”的位置。
func getLastKnownLocationFromTimeZone() {
var timeZonesArray: NSMutableArray!
if let path = Bundle.main.path(forResource: "NSTimeZoneCoordinates", ofType: "plist") {
timeZonesArray = NSMutableArray(contentsOfFile: path)
for timeZoneDictionary in timeZonesArray {
if let tzDic = timeZoneDictionary as? NSDictionary {
if let timeZoneName = tzDic["NSTimeZoneName"] as? String {
if timeZoneName == TimeZone.autoupdatingCurrent.identifier {
let lat = tzDic["Lat"] as! String
let lon = tzDic["Lon"] as! String
lastKnownLocation = CLLocation(latitude: lat.doubleValue(), longitude: lon.doubleValue())
return
}
}
}
}
}
}
plist 示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Lat</key>
<string>5.316667</string>
<key>Lon</key>
<string>-4.033333</string>
<key>NSTimeZoneName</key>
<string>Africa/Abidjan</string>
</dict>
<dict>
<key>Lat</key>
<string>5.55</string>
<key>Lon</key>
<string>-0.2</string>
<key>NSTimeZoneName</key>
<string>Africa/Accra</string>
</dict>
<dict>
<key>Lat</key>
<string>8.980603</string>
<key>Lon</key>
<string>38.757761</string>
<key>NSTimeZoneName</key>
<string>Africa/Addis_Ababa</string>
</dict>
<dict>
<key>Lat</key>
<string>36.752887</string>
<key>Lon</key>
<string>3.042048</string>
<key>NSTimeZoneName</key>
<string>Africa/Algiers</string>
</dict>
<dict>
<key>Lat</key>
<string>15.333333</string>
<key>Lon</key>
<string>38.933333</string>
<key>NSTimeZoneName</key>
<string>Africa/Asmara</string>
</dict>
<dict>
<key>Lat</key>
<string>12.65</string>
<key>Lon</key>
<string>-8</string>
<key>NSTimeZoneName</key>
<string>Africa/Bamako</string>
</dict>
<dict>
<key>Lat</key>
<string>4.366667</string>
<key>Lon</key>
<string>18.583333</string>
<key>NSTimeZoneName</key>
<string>Africa/Bangui</string>
</dict>
<dict>
<key>Lat</key>
<string>13.453056</string>
<key>Lon</key>
<string>-16.5775</string>
<key>NSTimeZoneName</key>
<string>Africa/Banjul</string>
</dict>
<dict>
<key>Lat</key>
<string>11.881655</string>
<key>Lon</key>
<string>-15.617794</string>
<key>NSTimeZoneName</key>
<string>Africa/Bissau</string>
</dict>
<dict>
<key>Lat</key>
<string>-15.786111</string>
<key>Lon</key>
<string>35.005833</string>
<key>NSTimeZoneName</key>
<string>Africa/Blantyre</string>
</dict>
<dict>
<key>Lat</key>
<string>-4.267778</string>
<key>Lon</key>
<string>15.291944</string>
<key>NSTimeZoneName</key>
<string>Africa/Brazzaville</string>
</dict>
<dict>
<key>Lat</key>
<string>-3.383333</string>
<key>Lon</key>
<string>29.366667</string>
<key>NSTimeZoneName</key>
<string>Africa/Bujumbura</string>
</dict>
<dict>
<key>Lat</key>
<string>30.119799</string>
<key>Lon</key>
<string>31.537</string>
<key>NSTimeZoneName</key>
<string>Africa/Cairo</string>
</dict>
<dict>
<key>Lat</key>
<string>33.57311</string>
<key>Lon</key>
<string>-7.589843</string>
<key>NSTimeZoneName</key>
<string>Africa/Casablanca</string>
</dict>
<dict>
<key>Lat</key>
<string>35.889387</string>
<key>Lon</key>
<string>-5.321346</string>
<key>NSTimeZoneName</key>
<string>Africa/Ceuta</string>
抱歉,在这里粘贴整个 plist 太长了。如果你想要它留下评论。