Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors when import MapService #19

Closed
Gubernskiy opened this issue Jan 27, 2018 · 7 comments
Closed

Errors when import MapService #19

Gubernskiy opened this issue Jan 27, 2018 · 7 comments

Comments

@Gubernskiy
Copy link

Gubernskiy commented Jan 27, 2018

I am using ngx-mapbox-gl:1.0.0-beta.5 and I can't import MapService to my component. If i write like this

import {MapService} from 'ngx-mapbox-gl/src/app/lib/map/map.service';

i have an error while compiling

Module not found: Error: Can't resolve 'ngx-mapbox-gl/src/app/lib/map/map.service' 

if i change import to

import {MapService} from 'ngx-mapbox-gl/bundles/ngx-mapbox-gl.umd.js';

Compiled successfully. But i have errors in browser console like

core.js:1440 ERROR Error: Uncaught (in promise): Error: Can't resolve all parameters for MyComponent:

I think that it is an error in ng-packagr or something with it's config. Because if i use your source files in my app everything is fine and work as expected.

@Wykks
Copy link
Owner

Wykks commented Jan 28, 2018

Hi!

This is done on purpose. MapService is private. Why do you need it ?

@Gubernskiy
Copy link
Author

I have made my own custom component for app. And put it inside mgl-map to have the same instance of MapService. I am using some functions from it and mapinstance property.

@Wykks
Copy link
Owner

Wykks commented Jan 28, 2018

What are you using from MapService exactly ? It's not made to be used by something else than ngx-mapbox-gl itself.
If you want to use fonction from mapbox-gl directly you can get the map instance with the load event of mgl-map.

Edit: oh, I see, you want to add custom component working with mgl-map. Hum, I haven't thought about that yet. I don't list MapService changes as breaking change... Let me think about it !

@Gubernskiy
Copy link
Author

Gubernskiy commented Jan 28, 2018

i am using getCurrentViewportBbox(). I understand that i can calculate it from map instance directly. At first i thought to hook on load event and get map instance from there as you suggest. But i thought that it is not good idea because i would have to make a wrapper component with your map and my component and put some logic there using hooks. I just want my component to be like mgl-layer and others. They know nothing about the map and get all nesessary informathing inside theyself. If you think it's not good i'll think about another solluting.

Edit: Ok. Let me know if you find a solution.

@Wykks
Copy link
Owner

Wykks commented Jan 28, 2018

Yup, it's fine, I wanted to do a proper way to add custom component, in order to do some extension like a ngx-mapbox-gl-draw for example. For this use case, exposing the MapService seems correct.
I do want to make a stable documented api for that someday.

@eraigosa
Copy link

eraigosa commented Apr 26, 2019

you can access the internal mapInstance:MapboxGl.Map like this

....
@ViewChild(MapComponent) mapComponent: MapComponent;
....
fitToKenya() { this.mapComponent.mapInstance.fitBounds([[32.958984, -5.353521], [43.50585, 5.615985]]);  }

For this example the wrapper provides the input fitBounds, so to access the internal map instance is not necessary and it is not recommended, but is always good to have alternatives.

<==My Original comment/question ==>
Hi, in my component I injected the MapService but the mapInstance is empty. how should I inject it?
@Wykks thanks for sharing the ngx-mapbox-gl wrapper and thanks for your help.

I'm doing this.
-----my mapComponent
....
constructor(private MapService: MapService) { }

fitToKenya() {
this.MapService.mapInstance.fitBounds([[32.958984, -5.353521], [43.50585, 5.615985]]); // ERROR!! mapInstance is null
}

----------app.module
.....
NgxMapboxGLModule.withConfig({ accessToken: '...this is ok..', geocoderAccessToken: '......' }), ],
entryComponents: [......],
providers: [MapService, ...........],
bootstrap: [AppComponent]

EDIT1: PD: I know to add a layer or fit bounds there are some directives (mgl-layer/fitBounds)... but I need the reference to mapInstance to do a custom modification, but is easier to exemplify with something basic. note: I also saw #25.

@Wykks
Copy link
Owner

Wykks commented Apr 27, 2019

@shadower01 You should use the load event to get the Map instance. See this example :
https://github.com/Wykks/ngx-mapbox-gl/wiki/API-Documentation#example
this.MapService.mapInstance is not available when the map is not loaded yet (and you can't rely on angular lifecycle there).
(Also never provide MapService elsewhere than in a component meant to be used inside a mgl-map).

I won't say that it's not recommended. It's just about whenever or not you want to do something declaratively (using template) or imperatively (calling mapbox-gl function directly). And sometimes, things "feels" better imperatively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants