我使用用戶的時區來獲取大概的位置,我只用它來設置我的應用程序主題和地圖樣式(這是一個導航應用程序)基於日出或日落。
我使用下面的函數,並有一個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。如果你想讓它留下評論。
如果您在用戶的位置數據拒絕提供給您之後基本上闖入了用戶的位置數據,那麼您的應用將無法訪問該應用商店。如果您需要通用信息,請使用用戶設置的區域設置。 – Quantaliinuxite
如果位置服務被禁用,那麼您在iOS中的最佳做法是IP地址。 Google API需要iOS上無法顯示的可見蜂窩塔和/或WiFi網絡的詳細信息,因此它也會回落到ip,以及@Quantaliinuxite說,如果用戶希望您的應用擁有自己的位置,它給你 – Paulw11
@Quantaliinuxite我不想闖入用戶的位置數據,因爲我知道應用程序將被拒絕。我只是試圖找出是否有比IP地址更好的解決方案。 Google Geolocation API看起來很有前途,但由於您無法獲取所需的詳細信息(如Paulw11所述),因此無法使用它。 – zirinisp