• 彻底解决vue接入高德地图不显示公司名字问题


    问题描述

    最近突然发现,高德地图放大后居然不显示公司名字,还以为是没有交钱做地图标注。然后告知IT部门在对接地图标注的事情。但是开发这边还是觉得有问题,毕竟是接入API,怎么会其余的公司显示呢,经过测试发现是版本问题,版本对照表如下

    高德官方API地址

    1. // V2.0 API地址
    2. https://lbs.amap.com/api/javascript-api-v2/documentation#map
    3. // 注意2.0版本不要添加lang或者this.map.setLang("en");
    4. // 否则会影响下面的地图代码,导致marker无法显示
    5. // V1.4.15 API地址
    6. https://lbs.amap.com/demo/javascript-api/example/map/map-english
    7. // lang的可选值如下
    8. <script>
    9. //初始化地图
    10. var map = new AMap.Map('container', {
    11. resizeEnable: true,
    12. center: [121.498586, 31.239637],
    13. lang: "en" //可选值:en, zh_en, zh_cn
    14. });
    15. // en=英文版
    16. // zh_en=中英双语显示
    17. // zh_cn=中文简体
    18. script>

    接入教程

    amap-jsapi-loader插件安装

    1. npm install @amap/amap-jsapi-loader --save
    2. # or
    3. yarn add @amap/amap-jsapi-loader

    VUE源码

    1. // 以下代码是使用的ruoyi-vue框架
    2. <template>
    3. <div class="app-container" style="padding-bottom: 0;">
    4. <el-row>
    5. <el-col :span="24">
    6. <div id="mapDiv" ref="mapDiv">div>
    7. el-col>
    8. el-row>
    9. div>
    10. template>
    11. <script type="text/javascript">
    12. window._AMapSecurityConfig = {
    13. securityJsCode:'18a3c4536975dd465f6134e1b2eaf38b',//替换自己的
    14. }
    15. script>
    16. <script>
    17. import AMapLoader from '@amap/amap-jsapi-loader';
    18. import request from '@/utils/request'
    19. import { formatDateStr } from "@/api/time";
    20. import { getDeviceNum } from "@/api/index";
    21. export default {
    22. name: "Index",
    23. data() {
    24. return {
    25. tableTimer:null,
    26. tableListSize:10,
    27. visibleSize:4,
    28. tableTop: 0,
    29. lineHeight: 28,
    30. // 版本号
    31. version: "3.8.6",
    32. map: null,
    33. allNum: 0,
    34. onlineNum: 0,
    35. warningNum: 0,
    36. remoteNum: 0,
    37. markerdom: null,
    38. marker: [],
    39. markernum: [[-15.8, 21.8]],
    40. infoWindow: null,
    41. tableHeight: 117,
    42. };
    43. },
    44. created() {
    45. },
    46. methods: {
    47. showInfoClickTag(data){
    48. let id = data;
    49. request({
    50. url: '/index/getIndexDeviceInfoByOne?id='+id,
    51. method: 'get'
    52. }).then(response => {
    53. if(response != null && response.data != null){
    54. if(this.infoWindow != null){
    55. this.infoWindow.close();
    56. }
    57. let onlineStatus = this.$i18n.t('wordOffline');
    58. if(response.data.online == 0){
    59. onlineStatus = this.$i18n.t('wordOnline');
    60. }
    61. let deviceStatus = this.$i18n.t('wordFault');
    62. if(response.data.devStatus == 0){
    63. deviceStatus = this.$i18n.t('wordNormal');
    64. }
    65. let deviceContent = "
      ";
    66. deviceContent += "
      "+this.$i18n.t('indexTableDeviceInfo')+"
      "
      ;
    67. deviceContent += "
      "+this.$i18n.t('indexTableCorporation')+":"+response.data.companyName+"
      "
      ;
    68. deviceContent += "
      "+this.$i18n.t('indexTablePrimaryKey')+":"+response.data.devId+"
      "
      ;
    69. deviceContent += "
      "+this.$i18n.t('indexTableDeviceName')+":"+response.data.name+"
      "
      ;
    70. deviceContent += "
      "+this.$i18n.t('indexTableDeviceOnlineStatus')+":"+onlineStatus+"
      "
      ;
    71. deviceContent += "
      "+this.$i18n.t('indexTableDeviceStatus')+":"+deviceStatus+"
      "
      ;
    72. deviceContent += "
      ";
  • this.infoWindow = new AMap.InfoWindow({
  • isCustom: true, //使用自定义窗体
  • content: deviceContent,
  • closeWhenClickMap: true,
  • offset: new AMap.Pixel(16, -45)
  • });
  • this.infoWindow.open(this.map, [response.data.longitude,response.data.latitude]);
  • }
  • }
  • );
  • },
  • initMap() {
  • let _this = this;
  • let language = localStorage.getItem("language");
  • let mapVersion = "1.4.15";
  • if(language == "zh-CN"){
  • mapVersion = "2.0";
  • }else{
  • //lang=en,zh_en,zh_cn
  • // v2.0情况下,lang不会报错也不起作用
  • // 只有1.4.15才有英文版地图,setLang在2.0时候会报错,影响后面的代码,所以增加判断
  • if(language == "zh-TW"){
  • this.map.setLang("zh_en");
  • }else {
  • this.map.setLang("en");
  • }
  • }
  • AMapLoader.load({
  • key: "36cf857a028b0a296c6ba86b3a56fb32", //替换自己的
  • version: mapVersion,
  • resizeEnable: true,
  • }).then((AMap) => {
  • // 全局map定义在main.js文件中
  • this.map = new AMap.Map("mapDiv", {
  • lang: "en",
  • zoom: 19,
  • viewMode: '2D',
  • buildingAnimation: false,
  • center: [121.33406, 31.327717],
  • // mapStyle: "amap://styles/fresh"
  • });
  • request({
  • url: '/index/listMap',
  • method: 'get',
  • params: this.queryParams
  • }).then(response => {
  • if(response != null && response.rows != null){
  • _this.marker = [];
  • for (let i = 0; i < response.rows.length; i++) {
  • let pngColorName = "map_pink";
  • if(response.rows[i].onlineFlag == 0){
  • pngColorName = "map_gree";
  • }
  • let pngColorPath = require("../assets/images/"+pngColorName+".png");
  • _this.markerdom = "" +
  • "
    " +
  • " +pngColorPath+"\" style=\"width:26px;\" onclick=\"showInfoFun('"+response.rows[i].id+"')\" >" +
  • "
    ";
  • _this.marker.push(new AMap.Marker({
  • position: new AMap.LngLat(response.rows[i].longitude, response.rows[i].latitude),
  • content: _this.markerdom,
  • offset: new AMap.Pixel(-13, -30)
  • }));
  • }
  • _this.map.add(_this.marker);
  • _this.map.setFitView();
  • }
  • }
  • );
  • }).catch(e => {
  • console.log(e);
  • })
  • },
  • },
  • mounted() {
  • this.initMap();
  • // 很重要,否则marker点击事件无法触发
  • window.showInfoFun = (data) => {
  • this.showInfoClickTag(data);
  • }
  • },
  • };
  • script>
  • <style scoped lang="scss">
  • #mapDiv {
  • padding: 0px;
  • margin: 0px;
  • width: 100%;
  • height: 48vh;
  • }
  • ::v-deep .map_div_css{
  • background: #304156;
  • padding: 7px;
  • font-size: 14px;
  • color: #ffffff;
  • border-radius: 6px;
  • font-family: initial;
  • }
  • style>
  • 效果展示

    结束

    -----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

    -----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

    -----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

    1. package cn.renkai721.bean.vo;
    2. import lombok.extern.slf4j.Slf4j;
    3. @Slf4j
    4. public class MakeUpTheWordCount {
    5. private String make_up_the_word_count_column_999999999_1;
    6. private String make_up_the_word_count_column_999999999_2;
    7. private String make_up_the_word_count_column_999999999_3;
    8. private String make_up_the_word_count_column_999999999_4;
    9. private String make_up_the_word_count_column_999999999_5;
    10. private String make_up_the_word_count_column_999999999_6;
    11. private String make_up_the_word_count_column_999999999_7;
    12. private String make_up_the_word_count_column_999999999_8;
    13. private String make_up_the_word_count_column_999999999_9;
    14. private String make_up_the_word_count_column_999999999_10;
    15. private String make_up_the_word_count_column_999999999_11;
    16. private String make_up_the_word_count_column_999999999_12;
    17. private String make_up_the_word_count_column_999999999_13;
    18. private String make_up_the_word_count_column_999999999_14;
    19. private String make_up_the_word_count_column_999999999_15;
    20. private String make_up_the_word_count_column_999999999_16;
    21. private String make_up_the_word_count_column_999999999_17;
    22. private String make_up_the_word_count_column_999999999_18;
    23. private String make_up_the_word_count_column_999999999_19;
    24. private String make_up_the_word_count_column_999999999_20;
    25. public String getMake_up_the_word_count_column_999999999_1() {
    26. return make_up_the_word_count_column_999999999_1;
    27. }
    28. public void setMake_up_the_word_count_column_999999999_1(String make_up_the_word_count_column_999999999_1) {
    29. this.make_up_the_word_count_column_999999999_1 = make_up_the_word_count_column_999999999_1;
    30. }
    31. public String getMake_up_the_word_count_column_999999999_2() {
    32. return make_up_the_word_count_column_999999999_2;
    33. }
    34. public void setMake_up_the_word_count_column_999999999_2(String make_up_the_word_count_column_999999999_2) {
    35. this.make_up_the_word_count_column_999999999_2 = make_up_the_word_count_column_999999999_2;
    36. }
    37. public String getMake_up_the_word_count_column_999999999_3() {
    38. return make_up_the_word_count_column_999999999_3;
    39. }
    40. public void setMake_up_the_word_count_column_999999999_3(String make_up_the_word_count_column_999999999_3) {
    41. this.make_up_the_word_count_column_999999999_3 = make_up_the_word_count_column_999999999_3;
    42. }
    43. public String getMake_up_the_word_count_column_999999999_4() {
    44. return make_up_the_word_count_column_999999999_4;
    45. }
    46. public void setMake_up_the_word_count_column_999999999_4(String make_up_the_word_count_column_999999999_4) {
    47. this.make_up_the_word_count_column_999999999_4 = make_up_the_word_count_column_999999999_4;
    48. }
    49. public String getMake_up_the_word_count_column_999999999_5() {
    50. return make_up_the_word_count_column_999999999_5;
    51. }
    52. public void setMake_up_the_word_count_column_999999999_5(String make_up_the_word_count_column_999999999_5) {
    53. this.make_up_the_word_count_column_999999999_5 = make_up_the_word_count_column_999999999_5;
    54. }
    55. public String getMake_up_the_word_count_column_999999999_6() {
    56. return make_up_the_word_count_column_999999999_6;
    57. }
    58. public void setMake_up_the_word_count_column_999999999_6(String make_up_the_word_count_column_999999999_6) {
    59. this.make_up_the_word_count_column_999999999_6 = make_up_the_word_count_column_999999999_6;
    60. }
    61. public String getMake_up_the_word_count_column_999999999_7() {
    62. return make_up_the_word_count_column_999999999_7;
    63. }
    64. public void setMake_up_the_word_count_column_999999999_7(String make_up_the_word_count_column_999999999_7) {
    65. this.make_up_the_word_count_column_999999999_7 = make_up_the_word_count_column_999999999_7;
    66. }
    67. public String getMake_up_the_word_count_column_999999999_8() {
    68. return make_up_the_word_count_column_999999999_8;
    69. }
    70. public void setMake_up_the_word_count_column_999999999_8(String make_up_the_word_count_column_999999999_8) {
    71. this.make_up_the_word_count_column_999999999_8 = make_up_the_word_count_column_999999999_8;
    72. }
    73. public String getMake_up_the_word_count_column_999999999_9() {
    74. return make_up_the_word_count_column_999999999_9;
    75. }
    76. public void setMake_up_the_word_count_column_999999999_9(String make_up_the_word_count_column_999999999_9) {
    77. this.make_up_the_word_count_column_999999999_9 = make_up_the_word_count_column_999999999_9;
    78. }
    79. public String getMake_up_the_word_count_column_999999999_10() {
    80. return make_up_the_word_count_column_999999999_10;
    81. }
    82. public void setMake_up_the_word_count_column_999999999_10(String make_up_the_word_count_column_999999999_10) {
    83. this.make_up_the_word_count_column_999999999_10 = make_up_the_word_count_column_999999999_10;
    84. }
    85. public String getMake_up_the_word_count_column_999999999_11() {
    86. return make_up_the_word_count_column_999999999_11;
    87. }
    88. public void setMake_up_the_word_count_column_999999999_11(String make_up_the_word_count_column_999999999_11) {
    89. this.make_up_the_word_count_column_999999999_11 = make_up_the_word_count_column_999999999_11;
    90. }
    91. public String getMake_up_the_word_count_column_999999999_12() {
    92. return make_up_the_word_count_column_999999999_12;
    93. }
    94. public void setMake_up_the_word_count_column_999999999_12(String make_up_the_word_count_column_999999999_12) {
    95. this.make_up_the_word_count_column_999999999_12 = make_up_the_word_count_column_999999999_12;
    96. }
    97. public String getMake_up_the_word_count_column_999999999_13() {
    98. return make_up_the_word_count_column_999999999_13;
    99. }
    100. public void setMake_up_the_word_count_column_999999999_13(String make_up_the_word_count_column_999999999_13) {
    101. this.make_up_the_word_count_column_999999999_13 = make_up_the_word_count_column_999999999_13;
    102. }
    103. public String getMake_up_the_word_count_column_999999999_14() {
    104. return make_up_the_word_count_column_999999999_14;
    105. }
    106. public void setMake_up_the_word_count_column_999999999_14(String make_up_the_word_count_column_999999999_14) {
    107. this.make_up_the_word_count_column_999999999_14 = make_up_the_word_count_column_999999999_14;
    108. }
    109. public String getMake_up_the_word_count_column_999999999_15() {
    110. return make_up_the_word_count_column_999999999_15;
    111. }
    112. public void setMake_up_the_word_count_column_999999999_15(String make_up_the_word_count_column_999999999_15) {
    113. this.make_up_the_word_count_column_999999999_15 = make_up_the_word_count_column_999999999_15;
    114. }
    115. public String getMake_up_the_word_count_column_999999999_16() {
    116. return make_up_the_word_count_column_999999999_16;
    117. }
    118. public void setMake_up_the_word_count_column_999999999_16(String make_up_the_word_count_column_999999999_16) {
    119. this.make_up_the_word_count_column_999999999_16 = make_up_the_word_count_column_999999999_16;
    120. }
    121. public String getMake_up_the_word_count_column_999999999_17() {
    122. return make_up_the_word_count_column_999999999_17;
    123. }
    124. public void setMake_up_the_word_count_column_999999999_17(String make_up_the_word_count_column_999999999_17) {
    125. this.make_up_the_word_count_column_999999999_17 = make_up_the_word_count_column_999999999_17;
    126. }
    127. public String getMake_up_the_word_count_column_999999999_18() {
    128. return make_up_the_word_count_column_999999999_18;
    129. }
    130. public void setMake_up_the_word_count_column_999999999_18(String make_up_the_word_count_column_999999999_18) {
    131. this.make_up_the_word_count_column_999999999_18 = make_up_the_word_count_column_999999999_18;
    132. }
    133. public String getMake_up_the_word_count_column_999999999_19() {
    134. return make_up_the_word_count_column_999999999_19;
    135. }
    136. public void setMake_up_the_word_count_column_999999999_19(String make_up_the_word_count_column_999999999_19) {
    137. this.make_up_the_word_count_column_999999999_19 = make_up_the_word_count_column_999999999_19;
    138. }
    139. public String getMake_up_the_word_count_column_999999999_20() {
    140. return make_up_the_word_count_column_999999999_20;
    141. }
    142. public void setMake_up_the_word_count_column_999999999_20(String make_up_the_word_count_column_999999999_20) {
    143. this.make_up_the_word_count_column_999999999_20 = make_up_the_word_count_column_999999999_20;
    144. }
    145. }

  • 相关阅读:
    【MySQL】 Java的JDBC编程
    资源释放的方式(try - with - resource 和 try - catch - finally)
    封装v-loading全局自定义指令
    [SQL]数据查询(一)
    EMERSON艾默生变频器维修M600/M701/M702
    Codeforces Round 905 (Div. 3)ABCDEF
    如何离线安装和使用pymysql操作mysql数据库
    Docker(第四部分)
    面试题 01.03.URL 化
    AI对网络安全的影响与挑战
  • 原文地址:https://blog.csdn.net/renkai721/article/details/136387246