高德地图 Vue2

#1-站点下加载多个车辆在地图显示

```js

//gps2Amap 自己封装高德文档api转换坐标

        this.gps2Amap(position, function(location) {

              try {

                var markerMini = new AMap.Marker({

                  position: [location[0].lng, location[0].lat],

                  extData: {

                    id: carId

                  },

                  zIndex: 100

                });

                markerMini.setContent(

                  `<div class="bubble-box"><div  class="car-inactive"></div></div>`

                );

                  markerMini.on("click", function(e) {

                    if (_this.clickedMarker === e.target) return;

                  ⭐  _this.clickShowRoute(carSn);

                    _this.clickedMarker = e.target;

                  });

                }

                _this.markersMap.set(carSn, markerMini); //使用映射方便socket传输操作         

                  } catch (err) {

              }

            });

//注意异步操作

```

 2-点击车辆绘制路线防止重复点击

```js

    const promise = new Promise((resolve, reject) => {

        try {

          this._getCarAndTrackRoute(code);

        } catch (err) {

        }

        resolve();

      });

      promise.then(() => {

        setTimeout(() => {

          overlayGroups = new AMap.OverlayGroup(markers); //此处覆盖物添加到高德地图

          _Map.add(overlayGroups);

          _Map.setFitView();

        }, 2000);

      });

```

 3-动态显示信息窗体内数据

> https://lbs.amap.com/api/subway-api/guide/station-information/infowindow/

```js

      infoWindow = new AMap.InfoWindow({

        isCustom: true,

        draggable: true, //是否可拖动

        offset: new AMap.Pixel(0, -18),

        content: ""

      });

      infoWindow.setContent(

        `<div class="bubble-box">

      目的地:${this.carDrivingInfo

          .stationName || "暂无"}</div>`

      );

      if (e.lnglat !== undefined) {

        infoWindow.open(_Map, e.lnglat);

      } else {

        infoWindow.open(_Map, e.getPosition());

      }

```

 4-点击覆盖物绘制路线

```js

    path = [...];

        const options = {

          path: path,

          isOutline: true,

          outlineColor: "#0554F2",

          borderWeight: 1,

          strokeColor: "#0554F2",

          strokeOpacity: 1,

          strokeWeight: 3,

          strokeStyle: "solid",

          strokeDasharray: [10, 10],

          lineJoin: "round",

          lineCap: "round",

          zIndex: 50

        };

        carBezierCurve = new AMap.BezierCurve(options);

        _Map.add(carBezierCurve);

```

## 4-根据经纬度变化实时移动覆盖物

```js

              currentMarker.setPosition([location[0].lng, location[0].lat]);

              infoWindow.setPosition([location[0].lng, location[0].lat]);

              currentMarker.setExtData({ isOnce: true });

```

 last-绘制路线建议此js文件转换

```

/**

*  判断经纬度是否超出中国境内

*/

function isLocationOutOfChina(latitude, longitude) {

    if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271)

      return true;

    return false;

  }



  /**

  *  将WGS-84(国际标准)转为GCJ-02(火星坐标):

  */

  function transformFromWGSToGCJ(latitude, longitude) {

    var lat = "";

    var lon = "";

    var ee = 0.00669342162296594323;

    var a = 6378245.0;

    var pi = 3.14159265358979324;


    if (isLocationOutOfChina(latitude, longitude)) {

      lat = latitude;

      lon = longitude;

    }

    else {

      var adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0);

      var adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0);

      var radLat = latitude / 180.0 * pi;

      var magic = Math.sin(radLat);

      magic = 1 - ee * magic * magic;

      var sqrtMagic = Math.sqrt(magic);

      adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);

      adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);

      latitude = latitude + adjustLat;

      longitude = longitude + adjustLon;

    }

    return { latitude: latitude, longitude: longitude };


  }


  /**

  *  将GCJ-02(火星坐标)转为百度坐标:

  */

  function transformFromGCJToBaidu(latitude, longitude) { 

    var pi = 3.14159265358979324 * 3000.0 / 180.0;


    var z = Math.sqrt(longitude * longitude + latitude * latitude) + 0.00002 * Math.sin(latitude * pi);

    var theta = Math.atan2(latitude, longitude) + 0.000003 * Math.cos(longitude * pi);

    var a_latitude = (z * Math.sin(theta) + 0.006);

    var a_longitude = (z * Math.cos(theta) + 0.0065);


    return { latitude: a_latitude, longitude: a_longitude };

  }


  /**

  *  将百度坐标转为GCJ-02(火星坐标):

  */

  function transformFromBaiduToGCJ(latitude, longitude) {

    var xPi = 3.14159265358979323846264338327950288 * 3000.0 / 180.0;


    var x = longitude - 0.0065;

    var y = latitude - 0.006;

    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);

    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);

    var a_latitude = z * Math.sin(theta);

    var a_longitude = z * Math.cos(theta);


    return { latitude: a_latitude, longitude: a_longitude };

  }


  /**

  *  将GCJ-02(火星坐标)转为WGS-84:

  */

  function transformFromGCJToWGS(latitude, longitude) {

    var threshold = 0.00001;


    // The boundary

    var minLat = latitude - 0.5;

    var maxLat = latitude + 0.5;

    var minLng = longitude - 0.5;

    var maxLng = longitude + 0.5;


    var delta = 1;

    var maxIteration = 30;


    while (true) {

      var leftBottom = transformFromWGSToGCJ(minLat, minLng);

      var rightBottom = transformFromWGSToGCJ(minLat, maxLng);

      var leftUp = transformFromWGSToGCJ(maxLat, minLng);

      var midPoint = transformFromWGSToGCJ((minLat + maxLat) / 2, (minLng + maxLng) / 2);

      delta = Math.abs(midPoint.latitude - latitude) + Math.abs(midPoint.longitude - longitude);


      if (maxIteration-- <= 0 || delta <= threshold) {

        return { latitude: (minLat + maxLat) / 2, longitude: (minLng + maxLng) / 2 };

      }


      if (isContains({ latitude: latitude, longitude: longitude }, leftBottom, midPoint)) {

        maxLat = (minLat + maxLat) / 2;

        maxLng = (minLng + maxLng) / 2;

      }

      else if (isContains({ latitude: latitude, longitude: longitude }, rightBottom, midPoint)) {

        maxLat = (minLat + maxLat) / 2;

        minLng = (minLng + maxLng) / 2;

      }

      else if (isContains({ latitude: latitude, longitude: longitude }, leftUp, midPoint)) {

        minLat = (minLat + maxLat) / 2;

        maxLng = (minLng + maxLng) / 2;

      }

      else {

        minLat = (minLat + maxLat) / 2;

        minLng = (minLng + maxLng) / 2;

      }

    }


  }


  function isContains(point, p1, p2) {

    return (point.latitude >= Math.min(p1.latitude, p2.latitude) && point.latitude <= Math.max(p1.latitude, p2.latitude)) && (point.longitude >= Math.min(p1.longitude, p2.longitude) && point.longitude <= Math.max(p1.longitude, p2.longitude));

  }


  function transformLatWithXY(x, y) {

    var pi = 3.14159265358979324;

    var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));

    lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;

    lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;

    lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;

    return lat;

  }


  function transformLonWithXY(x, y) {

    var pi = 3.14159265358979324;

    var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));

    lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;

    lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;

    lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;

    return lon;

  }




  module.exports = {

    isLocationOutOfChina: isLocationOutOfChina,

    transformFromWGSToGCJ: transformFromWGSToGCJ,

    transformFromGCJToBaidu: transformFromGCJToBaidu,

    transformFromBaiduToGCJ: transformFromBaiduToGCJ,

    transformFromGCJToWGS: transformFromGCJToWGS

  }

```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,513评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,312评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,124评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,529评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,937评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,913评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,084评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,816评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,593评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,788评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,267评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,601评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,265评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,158评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,953评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,066评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,852评论 2 277

推荐阅读更多精彩内容