简体   繁体   English

如何在 angular 中正式创建 leaflet map

[英]How to create the leaflet map in angular formly

My requirement is to create the leaflet map in angular formly,but I am new to this formly我的要求是在 angular 中正式创建 leaflet map,但我对这个正式陌生

and I know how to use integrate the map with normal html in angular as below我知道如何在 angular 中使用 map 与普通 html 集成,如下所示

map.component.ts map.component.ts

  ngOnInit() {
        const map = L.map('map').setView([51.509865,-0.118092], 10);

        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '© OpenStreetMap contributors'
        }).addTo(map);
    }

map.component.html map.component.html

<div id="map"></div>

map.component.css map.component.css

div{
    height: 500px;
    width: 500px;
}

But,Now I have to place the leaflet map in angular formly, can anyone help me regarding this但是,现在我必须将 leaflet map 正式放置在 angular 中,任何人都可以帮我解决这个问题

you can create a custom component of leaflet map and use it wherever in your project:您可以创建 leaflet map 的自定义组件并在项目中的任何位置使用它:

LeafletMapComponent.html LeafletMapComponent.html

<div class="col-md-12">
        <div id="map" leaflet
        [leafletOptions]="Options"
        (leafletMapReady)="onMapReady($event)" >
        </div>
</div>

LeafletMapComponent.css LeafletMapComponent.css

#map {
  width: 100%;
  height: 300px;
  }

LeafletMapcomponent.ts LeafletMapcomponent.ts

 constructor() { }
  map;
  @Input() markers;
  @Output() LMarkerclicked: EventEmitter<any> = new EventEmitter<any>();
  @Output() setPositon: EventEmitter<any> = new EventEmitter<any>();

  Options = {
    layers: [
      tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')
    ],
    zoom: 7,
    center: latLng(29.6060218, 52.5378041)
  };
 ngOnInit() {
  }
  onMapReady(map: L.Map) {
    setTimeout(() => {
      this.map = map;
      map.invalidateSize();
      this.addMarker();
    }, 0);
    map.on('click', (e)=>{this.onMapClick(e)});
  }
addMarker() {
    this.markers.forEach((mapMarker) => {
      let m = L.marker([mapMarker.lat, mapMarker.lng], {
        draggable: true,
        icon: new L.DivIcon({
          className: 'my-div-icon',
          html: '<img class="my-div-image" src="' + mapMarker.icon +'" />'+
                '<label class="MarkerLabel">'+ mapMarker.label +'</label>'
      })
      }).addTo(this.map).bindPopup("<b>" + mapMarker.info + "</b><br>" + mapMarker.label).on('click', this.markerClick, this).on('dragend', this.markerDragend, this);
    });
  }
  markerClick(e) {
    this.LMarkerclicked.emit(e.latlng);
  }
  markerDragend(e){
   this.LMarkerclicked.emit(e.target._latlng);
  }

Then for using it:然后使用它:

<app-leaflet-map [markers]="markers" (LMarkerclicked)="onClickLMarker($event)" (setPositon)="onClickLMarker($event)"></app-leaflet-map>

and in your ts file:并在您的 ts 文件中:

markers: MapmarkerVm[] = [
  {
    lat: 29.60602,
    lng: 52.53780,
    label: '',
    draggable: true,
    icon: '../assets/images/marker-active.png',
    info: 'cityName'
  },
];
onClickLMarker(e) {
  this.addEditForm.controls["Latitude"].reset(e.lat);
  this.addEditForm.controls["Longtude"].reset(e.lng);
}



   export class MapmarkerVm {
            lat: number;
            lng: number;
            label?: string;
            draggable: boolean;
            icon:string;
            info:String;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ngx-leaflet(Angular方式)创建矢量切片贴图? - How to create a vector tiles map with ngx-leaflet (Angular way)? 如何在Angular和ngx-leaflet编写的传单地图中添加图例 - How to add legend to leaflet map written by Angular and ngx-leaflet 如何在角度组件中删除Leaflet地图的标记 - How to remove marker to Leaflet map in angular component 如何更改角度传单的语言? - How to change language of angular leaflet-map? 如何在Angular组件中向Leaflet地图添加标记 - How to add marker to leaflet map in Angular component 如何在Angular传单绘制中创建自定义控件? - How to create custom control in leaflet draw for Angular? 如何在angular页面上创建多个leaflet映射 - How to create the multiple leaflet maps on the page in angular 我们如何创建 JSON 模式来构建基于 forms 的 angular 正式 forms 在普通 Z0ECD11C1D7A287401DZDA8 的基础上? - How can we create JSON schema to build a angular formly based forms on basis of plain JSON? 如何将动态物体均匀地推入角度? - How to push dynamic objects into angular formly? 如何在 Angular 上显示超链接形式 - How to display hyperlink on Angular Formly form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM