Commit 7b6876fc8284bf19d7c6295d6e3b18c129375974

Authored by 王强
1 parent 6be10167

定位完成~

src/common/helper.class.js
... ... @@ -196,7 +196,7 @@ export default class Helper {
196 196  
197 197 /* 数据格式处理 结束 */
198 198  
199   - /* 定位 开始 */
  199 + /* 定位相关处理 开始 */
200 200  
201 201 /**
202 202 * 获取用户地理位置坐标
... ... @@ -214,6 +214,70 @@ export default class Helper {
214 214 }
215 215 }
216 216  
217   - /* 定位 结束 */
  217 + /**
  218 + * GPS坐标转换为高德火星坐标,调用gcj_encrypt函数,传入wgsLat(纬度)和wgsLon(经度)参数
  219 + */
  220 + GPS = {
  221 + PI : 3.14159265358979324,
  222 + x_pi : 3.14159265358979324 * 3000.0 / 180.0,
  223 + delta : function (lat, lon) {
  224 + // Krasovsky 1940
  225 + //
  226 + // a = 6378245.0, 1/f = 298.3
  227 + // b = a * (1 - f)
  228 + // ee = (a^2 - b^2) / a^2;
  229 + var a = 6378245.0; // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
  230 + var ee = 0.00669342162296594323; // ee: 椭球的偏心率。
  231 + var dLat = this.transformLat(lon - 105.0, lat - 35.0);
  232 + var dLon = this.transformLon(lon - 105.0, lat - 35.0);
  233 + var radLat = lat / 180.0 * this.PI;
  234 + var magic = Math.sin(radLat);
  235 + magic = 1 - ee * magic * magic;
  236 + var sqrtMagic = Math.sqrt(magic);
  237 + dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
  238 + dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
  239 + return {'lat': dLat, 'lon': dLon};
  240 + },
  241 +
  242 + //GPS---高德
  243 + wgs84ToGcj02 : function ( wgsLat , wgsLon ) {
  244 +
  245 + // 将坐标转成浮点数
  246 + wgsLat = parseFloat(wgsLat);
  247 + wgsLon = parseFloat(wgsLon);
  248 +
  249 + if (this.outOfChina(wgsLat, wgsLon)) {
  250 + return {'lat': wgsLat, 'lon': wgsLon};
  251 + }
  252 +
  253 + var d = this.delta(wgsLat, wgsLon);
  254 + return {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon};
  255 + },
  256 + outOfChina : function (lat, lon) {
  257 + if (lon < 72.004 || lon > 137.8347) {
  258 + return true;
  259 + }
  260 + if (lat < 0.8293 || lat > 55.8271) {
  261 + return true;
  262 + }
  263 + return false;
  264 + },
  265 + transformLat : function (x, y) {
  266 + var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  267 + ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
  268 + ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
  269 + ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
  270 + return ret;
  271 + },
  272 + transformLon : function (x, y) {
  273 + var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  274 + ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
  275 + ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
  276 + ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
  277 + return ret;
  278 + }
  279 + };
  280 +
  281 + /* 定位相关处理 结束 */
218 282  
219 283 }
220 284 \ No newline at end of file
... ...
src/pages/search/search.vue
... ... @@ -95,8 +95,8 @@
95 95 borderColor: "#FC1541"
96 96 },
97 97 coordinate: { // 经纬度定位参数
98   - longitude: "0",
99   - latitude: "0",
  98 + longitude: 0,
  99 + latitude: 0,
100 100 location: false
101 101 },
102 102 titles: { // 板块标题
... ... @@ -215,9 +215,15 @@
215 215 */
216 216 positionCallback(position) {
217 217  
218   - // 设置经纬度定位参数
219   - this.$set(this.coordinate, "longitude", position.coords.longitude);
220   - this.$set(this.coordinate, "latitude", position.coords.latitude);
  218 + // 判断是否有GPS坐标转换高德坐标函数
  219 + if (typeof this.$parent.helper === "object" && typeof this.$parent.helper.GPS === "object") {
  220 +
  221 + let marsCoordinates = this.$parent.helper.GPS.wgs84ToGcj02(position.coords.latitude, position.coords.longitude);
  222 +
  223 + // 设置经纬度定位参数(火星坐标)
  224 + this.$set(this.coordinate, "longitude", marsCoordinates.lon);
  225 + this.$set(this.coordinate, "latitude", marsCoordinates.lat);
  226 + }
221 227  
222 228 // 定位执行完成,设置定位成功标识参数
223 229 this.$set(this.coordinate, "location", true);
... ... @@ -235,16 +241,16 @@
235 241 // 定位出错提示
236 242 switch(positionError.code) {
237 243 case positionError.PERMISSION_DENIED:
238   - Toast("用户拒绝定位");
  244 + // Toast("用户拒绝定位");
239 245 break;
240 246 case positionError.POSITION_UNAVAILABLE:
241   - Toast("无权获取当前位置");
  247 + // Toast("无权获取当前位置");
242 248 break;
243 249 case positionError.TIMEOUT:
244   - Toast("定位请求超时");
  250 + // Toast("定位请求超时");
245 251 break;
246 252 case positionError.UNKNOWN_ERROR:
247   - Toast("未知定位错误");
  253 + // Toast("未知定位错误");
248 254 break;
249 255 }
250 256  
... ...