如何获取网络用户的大致地理位置?

发布于 2024-08-21 12:22:52 字数 129 浏览 2 评论 0原文

如果我们能够在用户访问我们网站的主页时显示与用户位置相对应的信息,这将对我的业务带来重大好处。我知道尝试仅从 IP 地址获取地理定位并不是一门精确的科学,但我们愿意接受我们能得到的一切。

有哪些技术可用于确定网络用户的大致位置?

It would be of significant benefit to my business if we could display information corresponding to a user's location when they reach our website's home page. I understand that trying to obtain geolocation from only an IP address is not an exact science, but we're willing to take what we can get.

What techniques are there available for determining approximate locations of a web user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-08-28 12:22:52

最好的选择是使用 google api。您可以在提交的表单字段中设置 IP。或者您可以使用 .net api

google.loader.ClientLocation 获取一个人使用其 IP 地址的纬度/经度


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAALDWeTDQHOJCbCf0JnUqL8BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQA7AE8xB9MyWgHECPY2qimOp7BUQ"></script>
    <script src="scripts/clientLocation.js" type="text/javascript"></script>
    <script type="text/javascript">
        function $g(id) {
            return document.getElementById(id);
        }

        function displayLocation(latitudeEl, longitudeEl, cityEl, regionEl, countryEl, country_codeEl) {
            var cloc = new ClientLocation.Location(google.loader.ClientLocation);
            if (latitudeEl) latitudeEl.innerHTML = cloc.latitude;
            if (longitudeEl) longitudeEl.innerHTML = cloc.longitude;
            if (cityEl) cityEl.innerHTML = cloc.address.city;
            if (regionEl) regionEl.innerHTML = cloc.address.region;
            if (country) country.innerHTML = cloc.address.country;
            if (country_codeEl) country_codeEl.innerHTML = cloc.address.country_code;
        }

        function init() {
            displayLocation($g("latitude"), $g("longitude"), $g("city"), $g("region"), $g("country"), $g("country_code"));
        }
    </script>

</head>
<body onload="init()">
    <form id="form1" runat="server">
    <div>
        latitude : <span id="latitude"></span>
        <br />
        longitude : <span id="longitude"></span>
        <br />
        city : <span id="city"></span>
        <br />
        region : <span id="region"></span>
        <br />
        country : <span id="country"></span>
        <br />
        country_code : <span id="country_code"></span>
        <br />
    </div>
    </form>
</body>
</html>

// <copyright file="clientLocation.js" company="Sky Sanders">
// This source is placed in the Public Domain.
// http://skysanders.net/subtext
// Attribution is appreciated.
// </copyright>


/*
object literal format for google.loader.clientlocation  
{
"latitude": 33.324,
"longitude": -111.867,
"address": {
"city": "Chandler",
"region": "AZ",
"country": "USA",
"country_code": "US"
}
}
*/

var ClientLocation = {};

ClientLocation.Address = function() {
    /// <field name="city" type="String" />
    /// <field name="region" type="String" />
    /// <field name="country" type="String" />
    /// <field name="country_code" type="String" />
    /// <returns type="ClientLocation.Address"/>
    if (arguments.length > 0) {
        this.city = arguments[0].city;
        this.region = arguments[0].region;
        this.country = arguments[0].country;
        this.country_code = arguments[0].country_code;
        return;
    }
    else {
        this.city = "";
        this.region = "";
        this.country = "";
        this.country_code = "";
    }

}
ClientLocation.Location = function() {
    /// <field name="latitude" type="Number" />
    /// <field name="longitude" type="Number" />
    /// <field name="address" type="ClientLocation.Address" />
    if (arguments.length > 0) {

        this.latitude = arguments[0].latitude;
        this.longitude = arguments[0].longitude;
        this.address = arguments[0].address;

    }
    else {
        this.latitude = 0;
        this.longitude = 0;
        this.address = undefined;
    }

}

your best bet is to use the google apis. You can have the IP set in a form field that is submitted. Or you could use a .net api

google.loader.ClientLocation to get a person's lat/long using their IP address


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAALDWeTDQHOJCbCf0JnUqL8BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQA7AE8xB9MyWgHECPY2qimOp7BUQ"></script>
    <script src="scripts/clientLocation.js" type="text/javascript"></script>
    <script type="text/javascript">
        function $g(id) {
            return document.getElementById(id);
        }

        function displayLocation(latitudeEl, longitudeEl, cityEl, regionEl, countryEl, country_codeEl) {
            var cloc = new ClientLocation.Location(google.loader.ClientLocation);
            if (latitudeEl) latitudeEl.innerHTML = cloc.latitude;
            if (longitudeEl) longitudeEl.innerHTML = cloc.longitude;
            if (cityEl) cityEl.innerHTML = cloc.address.city;
            if (regionEl) regionEl.innerHTML = cloc.address.region;
            if (country) country.innerHTML = cloc.address.country;
            if (country_codeEl) country_codeEl.innerHTML = cloc.address.country_code;
        }

        function init() {
            displayLocation($g("latitude"), $g("longitude"), $g("city"), $g("region"), $g("country"), $g("country_code"));
        }
    </script>

</head>
<body onload="init()">
    <form id="form1" runat="server">
    <div>
        latitude : <span id="latitude"></span>
        <br />
        longitude : <span id="longitude"></span>
        <br />
        city : <span id="city"></span>
        <br />
        region : <span id="region"></span>
        <br />
        country : <span id="country"></span>
        <br />
        country_code : <span id="country_code"></span>
        <br />
    </div>
    </form>
</body>
</html>

// <copyright file="clientLocation.js" company="Sky Sanders">
// This source is placed in the Public Domain.
// http://skysanders.net/subtext
// Attribution is appreciated.
// </copyright>


/*
object literal format for google.loader.clientlocation  
{
"latitude": 33.324,
"longitude": -111.867,
"address": {
"city": "Chandler",
"region": "AZ",
"country": "USA",
"country_code": "US"
}
}
*/

var ClientLocation = {};

ClientLocation.Address = function() {
    /// <field name="city" type="String" />
    /// <field name="region" type="String" />
    /// <field name="country" type="String" />
    /// <field name="country_code" type="String" />
    /// <returns type="ClientLocation.Address"/>
    if (arguments.length > 0) {
        this.city = arguments[0].city;
        this.region = arguments[0].region;
        this.country = arguments[0].country;
        this.country_code = arguments[0].country_code;
        return;
    }
    else {
        this.city = "";
        this.region = "";
        this.country = "";
        this.country_code = "";
    }

}
ClientLocation.Location = function() {
    /// <field name="latitude" type="Number" />
    /// <field name="longitude" type="Number" />
    /// <field name="address" type="ClientLocation.Address" />
    if (arguments.length > 0) {

        this.latitude = arguments[0].latitude;
        this.longitude = arguments[0].longitude;
        this.address = arguments[0].address;

    }
    else {
        this.latitude = 0;
        this.longitude = 0;
        this.address = undefined;
    }

}
念三年u 2024-08-28 12:22:52

您无法确定,而且可能不准确,但确实存在将 IP 地址映射到其地理位置的服务。

我只是疯狂地寻找它,我得到了: http://www.ip2location.com/

他们往往需要不过,订阅他们的服务

You can't tell for sure and it can be inaccurate but services do exist to map IP addresses to their geographical location.

I just Binged for it and I got: http://www.ip2location.com/

They tend to need subscriptions to their service, though

月亮坠入山谷 2024-08-28 12:22:52

最好的选择是从像 MaxMind 这样的人那里获得数据许可 - 他们使用了很多不同的方法,其中一些非常聪明,可以找出与 IP 地址相关的位置,根据我的经验,它们的数据也很容易集成。

The best option is to licence the data from someone like MaxMind - they use a lot of different methods, some of them pretty clever, to find out the location associated with an IP address and in my experience their data is pretty easy to integrate with as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文