在没有核心位置的情况下获取用户位置(Google 地图地理定位?)

Getting User Location without Core Location (Google Maps Geolocation?)

关于我最近开发的一个应用,用户定位是必不可少的。为了获取用户的位置,我们使用位置管理器。不幸的是,这仅在用户启用了定位服务并授予访问权限时才有效。

如果用户不给予许可,我们只剩下几个不太准确的 ip 服务。我正在尝试找到一种方法,可以在不启用位置服务的情况下获得大致的用户位置,并且 ip 服务更好。

唯一有前途的 API 是 Google Maps Geolocation API,它声称可以使用基站和 wifi 节点获取用户位置。

我试图找到一个库或一种可以在 iOS 上使用它的方法,最好是 Swift 但没有成功。有人可以帮忙吗?

遗憾的是,没有办法做到这一点,因为操作系统不提供任何确定用户位置的替代方法(手机信号塔等)。

老实说,这是有道理的,就好像用户希望应用程序访问他的位置一样,他会授予我们权限。

这就是 Google 地理定位 API 无法在 iOS 上工作的原因。

唯一的解决方法是使用 IP 地址,但那里的准确性几乎是全国性的。

我使用用户的时区来获取大概位置,我只用它来根据日出或日落设置我的应用程序主题和地图样式(它是一个导航应用程序)。

我使用下面的函数并有一个 plist 'lookup table'。 lookup table 中的坐标是该时区的中心坐标。因此,如果用户在他自己的时区之外,它将为您提供 'wrong' 位置。

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。如果你想要它发表评论。