【问题标题】:Geolocation is showing different city not very accurate地理位置显示不同的城市不是很准确
【发布时间】:2019-10-23 18:53:39
【问题描述】:

我正在使用地理定位来检测未显示确切城市的当前城市,例如我在班加罗尔,它显示的是钦奈或有时是特里凡得琅。请让我知道如何准确显示当前城市。 下面是我的代码-

function fetch($host) {

    if ( function_exists('curl_init') ) {

        //use cURL to fetch data
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.1');
        $response = curl_exec($ch);
        curl_close ($ch);

    } else if ( ini_get('allow_url_fopen') ) {

        //fall back to fopen()
        $response = file_get_contents($host, 'r');

    } else {

        trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
        return;

    }

    return $response;
}

function convert($amount, $float=2, $symbol=true) {

    //easily convert amounts to geolocated currency.
    if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
        trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
        return $amount;
    }
    if ( !is_numeric($amount) ) {
        trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
        return $amount;
    }
    if ( $symbol === true ) {
        return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
    } else {
        return round( ($amount * $this->currencyConverter), $float );
    }
}

【问题讨论】:

  • 你能显示你当前的代码吗?如果您向我们展示您在做什么,就更容易找到这本书。
  • 请使用edit 按钮将其添加到问题中。使用正确的格式会更易读
  • 请将代码附加到问题中,而不是作为 cmets。可以应用代码格式以提高可读性。
  • 当然,添加了代码。请建议修复。提前致谢

标签: php geolocation ip-geolocation


【解决方案1】:

问题不在于您的代码,而在于信息来源。 IP 地理定位并非 100% 准确,您需要忍受它。最好的补救措施是使用诸如Ipregistry(免责声明:我运行该服务)之类的服务来关联来自多个来源的数据,以减少误报并减轻您的体验。

根据您的代码,您似乎在猜测用户的货币。在这种情况下,您可以对以下端点执行 HTTP 获取:

https://api.ipregistry.co/?key=tryout

并查看响应字段currency

【讨论】:

    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多