如何模拟从其他位置浏览?

时间:2013-10-21 15:59:25

标签: browser geolocation ip

不确定这是否可能......我们如何测试从其他位置浏览?

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

有没有办法测试这个,然后从另一个位置进行物理浏览?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以通过覆盖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>