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

时间:2010-02-16 12:37:22

标签: geolocation

如果我们能够在到达我们网站的主页时显示与用户位置相对应的信息,那将对我的业务产生重大影响。我知道,尝试从IP地址获取地理位置并不是一门精确的科学,但我们愿意接受我们所能得到的东西。

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

3 个答案:

答案 0 :(得分:5)

你最好的选择是使用谷歌api。您可以在提交的表单字段中设置IP。或者您可以使用.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;
    }

}

答案 1 :(得分:2)

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

我只是为它而Binged,我得到了:http://www.ip2location.com/

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

答案 2 :(得分:1)

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