0


天地图 自定义覆盖物(能用Marker,最好还是用)

地图

  1. 在天地图中,无法直接将Marker点,变成html标签,所以就需要自定义覆盖物去处理html标签

如果对于开发任务来说,Marker对于地图来说是必不可少的,但是有时候又需要一些自定义的html标签。

  1. 因此这个时候,就需要自定义处理。

例如:坐标系为(EPSG:4326)

const data = [

{

  1. name: '郑州市',
  2. lat: 34.757975,
  3. lng: 113.665412,
  4. value: 300,
  5. color: '#fff',
  6. background: 'rgb(126, 0, 35)'

},

{

  1. name: '开封市',
  2. lat: 34.797049,
  3. lng: 114.341447,
  4. value: 50,
  5. color: '#fff',
  6. background: 'rgb(255, 0, 0)'

},

{

  1. name: '洛阳市',
  2. lat: 34.663041,
  3. lng: 112.434468,
  4. value: 90,
  5. color: '#303133',
  6. background: 'rgb(255, 255, 0)'

}

];

const map:any = ref()

map.value = new window.T.Map('map', {

  1. projection: 'EPSG:4326'

});

data.map((item: any) => {

  1. var definedOverlay = window.T.Overlay.extend({
  2. initialize: function (lnglat: any, options: any) {
  3. this.lnglat = lnglat;
  4. this.setOptions(options);
  5. },
  6. onAdd: function (map: any) {
  7. this.map = map;
  8. var div = (this._div = document.createElement('div'));
  9. div.style.position = 'absolute';
  10. div.style.backgroundColor = item.color;
  11. div.style.borderRadius = '50%';
  12. div.style.color = 'white';
  13. div.style.width = '20px';
  14. div.style.height = '20px';
  15. div.style.whiteSpace = 'nowrap';
  16. div.style.userSelect = 'none';
  17. div.style.fontSize = '12px';
  18. div.style.zIndex = '999';
  19. div.style.cursor = 'pointer';
  20. var that = this;
  21. //点击事件
  22. div.onclick = function () {
  23. var pos = that.map.lngLatToLayerPoint(that.lnglat);
  24. posLocation.x = pos.x - 200;
  25. posLocation.y = pos.y + 20;
  26. popShow.value = true;
  27. console.log('9999', pos);
  28. };
  29. map.getPanes().overlayPane.appendChild(this._div);
  30. this.update(this.lnglat);
  31. },
  32. onRemove: function () {
  33. var parent = this.div.parentNode;
  34. if (parent) {
  35. parent.removeChild(this.div);
  36. this.map = null;
  37. this.div = null;
  38. }
  39. },
  40. setLnglat: function (lnglat: any) {
  41. this.lnglat = lnglat;
  42. this.update();
  43. },
  44. getLnglat: function () {
  45. return this.lnglat;
  46. },
  47. setPos: function (pos: any) {
  48. this.lnglat = this.map.layerPointToLngLat(pos);
  49. this.update();
  50. },
  51. /**
  52. * 更新位置
  53. */
  54. update: function () {
  55. var pos = this.map.lngLatToLayerPoint(this.lnglat);
  56. this._div.style.top = pos.y + 'px';
  57. this._div.style.left = pos.x + 'px';
  58. }
  59. });
  60. var point = new window.T.LngLat(item.lng, item.lat);
  61. var pdefinedOverlay = new definedOverlay(point, {});
  62. map.value.addOverLay(pdefinedOverlay);

});

标签: 前端 typescript

本文转载自: https://blog.csdn.net/weixin_62364503/article/details/140692364
版权归原作者 梦雨生生 所有, 如有侵权,请联系我们删除。

“天地图 自定义覆盖物(能用Marker,最好还是用)”的评论:

还没有评论