Browser 如何模拟从不同位置浏览?

Browser 如何模拟从不同位置浏览?,browser,geolocation,ip,Browser,Geolocation,Ip,不确定这是否有可能。。。我们如何测试从不同位置浏览 这是为了测试使用GeoPlugin的web应用程序。此插件根据用户的IP提供地理信息(城市、纬度、经度…),而web应用程序则使用这些信息生成自定义主页 除了从另一个位置进行物理浏览之外,还有什么方法可以测试这一点吗 谢谢大家! 您可以通过覆盖navigator.geolocation对象来模拟Webkit浏览器中的位置。在Google Chrome中尝试以下示例: <html> <head> &

不确定这是否有可能。。。我们如何测试从不同位置浏览

这是为了测试使用GeoPlugin的web应用程序。此插件根据用户的IP提供地理信息(城市、纬度、经度…),而web应用程序则使用这些信息生成自定义主页

除了从另一个位置进行物理浏览之外,还有什么方法可以测试这一点吗


谢谢大家!

您可以通过覆盖navigator.geolocation对象来模拟Webkit浏览器中的位置。在Google Chrome中尝试以下示例:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        <script type="text/javascript" language="JavaScript">
            /* Replace navigator.geolocation with mock */

            var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
            if (typeof navigator == "undefined" || navigator === null) {
              window.navigator = {};
            }

            // Replace real geolocation with mock geolocation
            delete navigator.geolocation;
            navigator.geolocation = {
              isMock: true,
              paused: true,
              delay: 1000,
              shouldFail: false,
              failsAt: -1,
              unFailsAt: -1,
              errorMessage: "There was an error retrieving the position!",
              currentTimeout: -1,
              lastPosReturned: 0,
              overrideAccuracy: 0, // accuracy in m to return for all positions (overrides any existing accuracies defined in the waypoints)
              useOverrideAccuracy: false, // Whether to override acurracies defined in the waypoints with the above override accuracy

              _geoCall: function(method, success, error, repeat) {

                  return this.currentTimeout = window[method].call(null, __bind(function() {

                    var nextPos;

                    if(!this.paused && this.lastPosReturned < this.waypoints.length - 1){
                        nextPos = this.lastPosReturned++;
                    }else{
                        this.lastPosReturned = nextPos = 0;
                    }


                    if(!this.shouldFail && nextPos == this.failsAt) this.shouldFail = true;
                    if(this.shouldFail && nextPos == this.unFailsAt) this.shouldFail = false;

                    if(repeat) this._geoCall("setTimeout", success, error, true);

                    if (this.shouldFail && (error != null)) {
                        return error(this.errorMessage);
                    }
                    else if (success != null && (!this.paused || !repeat)) {
                        var result = this.waypoints[nextPos];
                        result.isMock = true;
                        if(this.useOverrideAccuracy) result.coords.accuracy = this.overrideAccuracy;
                        success(result);
                        return nextPos;
                    }
                  }, this), this.delay);

              },
              getCurrentPosition: function(success, error) {
                return this._geoCall("setTimeout", success, error, false);
              },
              watchPosition: function(success, error) {
                this._geoCall("setTimeout", success, error, true);
                return this.currentTimeout;
              },
              clearWatch: function(id) {
                return clearInterval(this.currentTimeout);
              },
              waypoints: []
            };
        </script>

        <script type="text/javascript" language="JavaScript">
            navigator.geolocation.waypoints = [
                {coords:{latitude:50.001, longitude:-4.001}},
                {coords:{latitude:50.001, longitude:-4.002}},
                {coords:{latitude:50.001, longitude:-4.003}},
                {coords:{latitude:50.001, longitude:-4.004}},
                {coords:{latitude:50.001, longitude:-4.005}},
                {coords:{latitude:50.001, longitude:-4.006}},
                {coords:{latitude:50.001, longitude:-4.007}},
                {coords:{latitude:50.001, longitude:-4.007}},
                {coords:{latitude:50.001, longitude:-4.009}},
                {coords:{latitude:50.001, longitude:-4.010}},
                {coords:{latitude:50.001, longitude:-4.011}},
                {coords:{latitude:50.001, longitude:-4.012}},
                {coords:{latitude:50.001, longitude:-4.013}},
                {coords:{latitude:50.001, longitude:-4.014}},
                {coords:{latitude:50.001, longitude:-4.015}},
                {coords:{latitude:50.001, longitude:-4.016}},
                {coords:{latitude:50.001, longitude:-4.017}},
                {coords:{latitude:50.001, longitude:-4.017}},
                {coords:{latitude:50.001, longitude:-4.019}},
                {coords:{latitude:50.001, longitude:-4.020}}
            ];

            function runTests(){
                navigator.geolocation.paused = false; // run geomocking
                test1();
            }

            function test1(){
                console.log("Test 1: Get current position");
                navigator.geolocation.getCurrentPosition(
                    function(position){
                        console.log("Position updated: latitude="+position.coords.latitude+", longitude="+position.coords.longitude);
                        test2();
                    },
                    function(error){
                        console.error("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
                    }
                );
            }

            function test2(){
            console.log("Test 2: Watch position");
                navigator.geolocation.watchPosition(
                    function(position){
                        console.log("Position updated: latitude="+position.coords.latitude+", longitude="+position.coords.longitude);
                    },
                    function(error){
                        console.error("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
                    }
                );
            }

            document.addEventListener('DOMContentLoaded', runTests, false);
        </script>
    </head>
    <body>

    </body>
</html>

/*将navigator.geolocation替换为mock*/
var_uubind=function(fn,me){return function(){return fn.apply(me,arguments);};};
如果(导航器类型==“未定义”| |导航器===null){
window.navigator={};
}
//将真实地理定位替换为模拟地理定位
删除navigator.geolocation;
navigator.geolocation={
伊斯莫克:是的,
是的,
延误:1000,
应该失败:错误,
故障卫星:-1,
不公平:1,
errorMessage:“检索位置时出错!”,
当前超时:-1,
lastPosReturned:0,
overrideAccuracy:0,//返回所有位置的精度(以m为单位)(覆盖航路点中定义的任何现有精度)
useOverrideAccuracy:false,//是否使用上述覆盖精度覆盖航路点中定义的精度
_geoCall:函数(方法、成功、错误、重复){
返回this.currentTimeout=window[method]。调用(null,uu绑定(函数(){
var-nextPos;
如果(!this.paused&&this.lastPosReturned