我正在尝试让ngx-mapbox-gl在有角度的应用程序中显示地图,但该地图仅显示在div的一半中。进行了多种设置和html调整,但无法显示一半以上的地图。这是map.component.html文件:
<div class="map" ng-view flex layout-fill style="height: 100%;border: solid 1px">
<mgl-map
[style]="'mapbox://styles/mapbox/streets-v9'"
[zoom]="9"
[center]="_center"
(load)="loadMap($event)"
>
</mgl-map>
</div>
和map.component.scss:
@import 'mapbox-gl/dist/mapbox-gl.css';
.mapboxgl-canvas {
position: fixed !important;
height: 100% !important;
}
map.component.ts:
import { Component, OnInit, Input, Output, EventEmitter, OnChanges } from '@angular/core';
import { LngLat, Map } from 'mapbox-gl';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.scss'],
})
export class DisplayMapComponent implements OnInit, OnChanges {
map: Map;
_center: LngLat;
//refs
_mapRef: Map;
@Output()
centerChange: EventEmitter<LngLat> = new EventEmitter;
@Input()
set zoom(z: number) {
this._zoom = z;
if(this.index === 0) {
this.zoomChange.emit(this._zoom);
}
}
@Output()
zoomChange : EventEmitter<number> = new EventEmitter;
_zoom: number;
@Input()
index: number;
constructor() { }
ngOnInit() {
this.zoom = 11;
this._center = new LngLat( -121.31209, 37.449904 );
console.log('this._center: ', this._center);
}
ngOnChanges(changes) {
console.log('changes: ', changes);
if (!changes) {
return;
}
}
loadMap( map: Map) {
console.log("Initializing map, _center: ", this._center);
console.log("map: ", map);
this._mapRef = map;
console.log("Loading map data");
this._center = map.getCenter();
console.log('this._center from map: ', this._center);
}
}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { LocalStorageService } from './services/local-storage.service';
import { MatToolbarModule, MatMenuModule, MatIconModule, MatListModule, MatGridListModule, MatButtonModule, MatCardModule } from '@angular/material';
import { MatSidenavModule } from '@angular/material/sidenav';
import { FlexLayoutModule } from '@angular/flex-layout';
import { StorageServiceModule } from 'angular-webstorage-service';
//mapbox imports
import { NgxMapboxGLModule } from 'ngx-mapbox-gl';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './components/header/header.component';
import { DisplayMapComponent } from './components/map/map.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { LayoutModule } from '@angular/cdk/layout';
import { SettingsContainerComponent } from './components/settings- container/settings-container.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
DisplayMapComponent,
NavbarComponent,
SettingsContainerComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatToolbarModule,
MatSidenavModule,
MatMenuModule,
MatButtonModule,
MatIconModule,
FlexLayoutModule,
LayoutModule,
MatListModule,
StorageServiceModule,
NgxMapboxGLModule.withConfig({
accessToken: '<token>',
}),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我所看到的屏幕截图:
如果删除地图,则div的边框将按整个窗口显示。但是我无法使地图用完整个窗口。是CSS还是html或ts我做错了?
谢谢。...
答案 0 :(得分:2)
您可以执行与ngx-mapbox-gl documentation sample中所示相同的操作。有一个入门部分,详细介绍了所有内容。这也是您需要做的摘要。
您无需在.mapboxgl-canvas
文件中指定此样式,而无需使用map.component.scss
样式:
mgl-map {
height: 100%;
width: 100%;
}
将导入的mapbox css文件@import 'mapbox-gl/dist/mapbox-gl.css';
放在应用程序的主styles.scss文件中,而不是在组件文件中。这样就可以了。
另一个提示。由于您正在使用@angular/flex-layout
库,因此可以在div上使用fxFlexFill指令来使其填充所有内容,而不是从那里的所有内容中删除。那么您的html组件就干净得多。
<div fxFlexFill style="border: solid 1px">
<mgl-map
[style]="'mapbox://styles/mapbox/streets-v9'"
[zoom]="9"
[center]="_center"
(load)="loadMap($event)"
>
</mgl-map>
</div>
我还创建了一个StackBlitz示例,您可以在其中看到它的运行情况。如果要查看地图,则需要编辑示例并将令牌添加到app.module.ts
中。
答案 1 :(得分:0)
我遵循了文档,但仍然无法使用。 @cpeddie issue on Github为我做到了:
html-
<mgl-map (load)="onMapLoad($event)"></mgl-map>
ts-
export class NgMapComponent implements OnInit, OnDestroy {
// map box
map: mapBox.Map;
...
onMapLoad(mapinstance) {
this.map = mapinstance;
this.map.resize();
console.log('resize');
}
答案 2 :(得分:0)
我在地图加载事件中调整了地图的大小,并且对我有用
loadMap( map: Map) {
console.log("Initializing map, _center: ", this._center);
console.log("map: ", map);
this._mapRef = map;
console.log("Loading map data");
this._center = map.getCenter();
console.log('this._center from map: ', this._center);
// i added
map.resize()}