Geolocation API用于检测用户的国家/地区?

时间:2015-07-08 07:06:27

标签: javascript php ajax api geolocation

我需要检测用户的国家/地区并向选定国家/地区的用户显示数据。

AFAIK,最准确的方法是使用API​​。但是哪一个?有很多商业报价,但只是为了获得这个国家(我不关心长/拉或城市)似乎有点矫枉过正。

如果不每年花费数百美元,最准确的方法是什么?

4 个答案:

答案 0 :(得分:0)

这取决于您需要进行地理定位的程度。如果每天不超过1000,您可以使用googleMaps和相关功能进行gecoding。

否则尝试http://www.geonames.org/export/reverse-geocoding.htmlhttp://services.gisgraphy.com/public/reverse_geocoding_worldwide.html是免费的

答案 1 :(得分:0)

使用这个简单的PHP函数,通过传递用户的IP地址获取用户所在国家,城市等的信息。

    function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
        $output = NULL;
        if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
            $ip = $_SERVER["REMOTE_ADDR"];
            if ($deep_detect) {
                if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                    $ip = $_SERVER['HTTP_CLIENT_IP'];
            }
        }
        $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
        $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
        $continents = array(
            "AF" => "Africa",
            "AN" => "Antarctica",
            "AS" => "Asia",
            "EU" => "Europe",
            "OC" => "Australia (Oceania)",
            "NA" => "North America",
            "SA" => "South America"
        );
        if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
            $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
            if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
                switch ($purpose) {
                    case "location":
                        $output = array(
                            "city"           => @$ipdat->geoplugin_city,
                            "state"          => @$ipdat->geoplugin_regionName,
                            "country"        => @$ipdat->geoplugin_countryName,
                            "country_code"   => @$ipdat->geoplugin_countryCode,
                            "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                            "continent_code" => @$ipdat->geoplugin_continentCode
                        );
                        break;
                    case "address":
                        $address = array($ipdat->geoplugin_countryName);
                        if (@strlen($ipdat->geoplugin_regionName) >= 1)
                            $address[] = $ipdat->geoplugin_regionName;
                        if (@strlen($ipdat->geoplugin_city) >= 1)
                            $address[] = $ipdat->geoplugin_city;
                        $output = implode(", ", array_reverse($address));
                        break;
                    case "city":
                        $output = @$ipdat->geoplugin_city;
                        break;
                    case "state":
                        $output = @$ipdat->geoplugin_regionName;
                        break;
                    case "region":
                        $output = @$ipdat->geoplugin_regionName;
                        break;
                    case "country":
                        $output = @$ipdat->geoplugin_countryName;
                        break;
                    case "countrycode":
                        $output = @$ipdat->geoplugin_countryCode;
                        break;
                }
            }
        }
        return $output;
    }

    ?>

答案 2 :(得分:0)

有两种免费方法可以获得IP地址。

  1. 自托管数据库 您可以从http://download.ip2location.com/lite/下载CSV文件并将其导入您自己的数据库服务器。所有查询都可以在本地完成。您需要实现查询。这并不困难,因为常见问题解答中提供了大多数样本。

  2. 远程托管API 您可以使用IPInfoDB API http://www.ipinfodb.com获取地理位置结果。无需本地查询。但是,由于网络延迟,它与方法1相比较慢。

  3. 两种方法的准确性都很好。

答案 3 :(得分:-1)

如果我没记错的话,在我们正在使用的项目中 GeoLite解决这个问题。它是免费的IP地理定位数据库,每月更新一次。

相关问题