PageSourceSearch

https://static.to8to.com/gb_js/h5/geolocation.js?202109151728

js to8to.com collected 2026-10-02 18:56:45 UTC 8,821 bytes, 280 lines download raw bytes

1/**
2 * 浏览器定位
3 * 注:pc下定位不准,请在手机模式下测试
4 *
5 * 如出现位置偏差问题, 可参考wiki:http://wiki.we.com:8090/pages/viewpage.action?pageId=60819886
6 *
7 * @author imp.zhang
8 * @date   2020-03-11
9 * @version 1.0
10 */
11
12var To8toGeo = function() {
13
14    var timeClock
15    var timeNum = 0
16
17    function timeClockFun () {
18        timeClock = setTimeout(function() {
19            timeNum ++
20            timeClockFun()
21        }, 1000)
22    }
23
24    this.options = {
25        debug: false,       // 是否开启debug 模式
26        type: "default",    // 定位方式, default 默认浏览器自带, baidu 百度定位
27        assist: "",         // 协助方式,default定位方式下,采用其他协助方式。   eg: baidu
28        callback: function() {},
29        errorCallback: function() {},
30
31        baiduAk: "UrL3fRggHKhAzvXLoGjsGxrdBZ9vdom2", // 百度地图ak
32
33        // 浏览器定位相关配置
34        enableHighAccuracy: true,
35        timeout: 8000,
36        maximumAge: 0,
37    };
38
39    this.initialized = false;
40    this.province = "";
41    this.city = "";
42    this.town = "";
43    this.street = "";
44    this.longitude = "";
45    this.latitude = "";
46    this.baiduGeoloation = null;
47
48    this.to8toLocationApi = "https://www.to8to.com/api/landpage/app.php?m=location&a=geo";
49    this.to8toRecordApi = "https://www.to8to.com/api/landpage/app.php?m=log&a=log";
50
51    // 初始化
52    this.initialize = function(options) {
53        this.options = $.extend(this.options, options);
54        this.initialized = true;
55
56        this.initLocator();
57    };
58
59    // 初始化定位器
60    this.initLocator = function() {
61        switch (this.options.type) {
62            case 'baidu':
63                this.initBaiduLocator();
64                break;
65            default:
66                this.initDefaultLocator();
67                break;
68        }
69    };
70
71    //记录日志
72    this.recordLog = function(params){
73        $.ajax({
74            type: "GET",
75            url: this.to8toRecordApi,
76            data: params,
77            success: function (res) {
78
79            },
80            error: function (res) {
81            }
82        });
83    }
84
85    // 初始化浏览器自带定位
86    this.initDefaultLocator = function() {
87        this.log("default location.");
88        var self = this;
89        var options = {
90            enableHighAccuracy: this.options.enableHighAccuracy,
91            timeout: this.options.timeout,
92            maximumAge: this.options.maximumAge,
93        };
94        if (navigator.geolocation) {
95            timeClockFun()
96            navigator.geolocation.getCurrentPosition(function (position) {
97                clearTimeout(timeClock)
98                self.pushMessage()
99                self.log("default location success. x = " + position.coords.longitude + " y: " + position.coords.latitude + " position: " + JSON.stringify(position));
100                self.longitude = position.coords.longitude; // 经度
101                self.latitude = position.coords.latitude;   // 纬度
102                self.to8toParseAddressByGEO(self.longitude, self.latitude, 'default');
103            }, function () {
104                self.log("default location error.");
105                if (self.options.assist == 'baidu') {
106                    self.initBaiduLocator();
107                    return;
108                }
109
110                self.locationError();
111            }, options);
112        } else {
113            this.errorCallback(this);
114            // alert("浏览器不支持地理定位。");
115        }
116    };
117
118
119    // to8to根据经纬度转换地址
120    this.to8toParseAddressByGEO = function(lng, lat, type) {
121        var self  = this;
122        $.ajax({
123            type: "GET",
124            url: this.to8toLocationApi,
125            dataType: "jsonp",
126            jsonpCallback: "jsonpCallback",
127            data: {
128                lng: lng,
129                lat: lat,
130            },
131            success: function (res) {
132                self.log("to8to location res: ")
133                self.log(JSON.stringify(res))
134                self.province = res.data.province;
135                self.city = res.data.city;
136                self.town = res.data.town;
137                self.street = res.data.street;
138
139                self.locationSuccess(self.getAddress());
140                var recordInfo = {
141                    location: type,
142                    longitude: lng,
143                    latitude: lat,
144                    city: self.city,
145                    town: self.town,
146                    street: self.street,
147                    province: self.province,
148                    result: 'success',
149                };
150                self.recordLog(recordInfo);
151            },
152            error: function (res) {
153                var recordInfo = {
154                    location: type,
155                    result: 'fail',
156                };
157                self.recordLog(recordInfo);
158            }
159        });
160    };
161
162
163    // 百度定位器
164    this.initBaiduLocator = function () {
165        this.log("baidu location.");
166        var self = this;
167        this.baiduGeoloation = new BMap.Geolocation();
168        this.baiduGeoloation.getCurrentPosition(function (r) {
169            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
170                self.log('baidu locaiton success. x = ' + r.point.lng + ", " + " y: " + r.point.lat);
171                self.longitude = r.point.lng;
172                self.latitude = r.point.lat;
173
174                self.to8toParseAddressByGEO(self.longitude, self.latitude, 'baidu');
175
176                // self.baiduParseAddressByGeo(r.point);
177            }
178            else {
179                self.log('baidu locaiton failed' + this.getStatus());
180                self.locationError();
181            }
182        }, {enableHighAccuracy: true})
183    };
184
185
186    // 百度详细定位
187    this.baiduParseAddressByGeo = function(point) {
188        var self = this;
189        var gc = new BMap.Geocoder();
190        gc.getLocation(point, function (rs) {
191            var addComp = rs.addressComponents;
192            self.province = addComp.province;
193            self.city = addComp.city;
194            self.town = addComp.district;
195            self.street = addComp.street + addComp.streetNumber;
196
197            self.locationSuccess(self.getAddress());
198        });
199    };
200
201    // 定位失败回调
202    this.locationError = function() {
203        var self = this;
204        this.errorCallback(self);
205    };
206
207    // 定位成功
208    // @params getAddress()
209    this.locationSuccess = function(params) {
210        this.log("location success callback.");
211        if (typeof this.options.callback == 'function') {
212            this.options.callback(params);
213        }
214    };
215
216    // 本方法作用不同于locationError, 用于定位失败后补救工作
217    this.errorCallback = function (params) {
218        // 添加定位失败日志上报
219        try {
220            this.recordLog({
221                key: 'geolocation_error'
222            })
223        } catch (error) {
224            
225        }
226        this.log("location errorCallback.");
227        if (typeof this.options.errorCallback == 'function') {
228            this.options.errorCallback(params);
229        }
230    };
231
232    // 获取地址
233    this.getAddress = function() {
234        var address = {
235            province: this.province,
236            city: this.city,
237            town: this.town,
238            street: this.street,
239            longitude: this.longitude,
240            latitude: this.latitude,
241        }
242        return address;
243    };
244
245    // 详细地址
246    this.getDetailAddress = function() {
247        return this.province + this.city + this.town + this.street;
248    };
249
250    // log
251    this.log = function(msg) {
252        if (this.options.debug && $("#debug").length == 0) {
253            var html = '<div id="debug" style=" width: 100%; background: #ccc; min-height: 50px; margin-top: 20px; line-height: 1.4;  padding: 10px; font-size: 18px; border: 3px dashed #62625e;"><B>debug</B><Br></div>';
254            $("body").append(html)
255        }
256
257        if (this.options.debug) {
258            $("#debug").append(msg + "<br/>");
259        }
260
261        // console.log(msg)
262    }
263
264    // 向接口上报内容
265    this.pushMessage = function() {
266        $.ajax({
267            url: 'https://www.to8to.com/api/landpage/app.php',
268            type: 'get',
269            data: {
270                m: 'log',
271                a: 'log',
272                source: 'geolocation',
273                time: timeNum
274            },
275            dataType: 'json',
276            success: function() {}
277        })
278    }
279
280}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.