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

fitBounds not supported? #36

Open
nicksav opened this issue Sep 15, 2019 · 6 comments
Open

fitBounds not supported? #36

nicksav opened this issue Sep 15, 2019 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@nicksav
Copy link

nicksav commented Sep 15, 2019

Hey
Looks like fitBounds is not supported as I am adding new overlays after the map was loaded and nothing happens?

Am I correct that it is not supported?

Thanks
Nick

@AckerApple
Copy link
Owner

If you are trying to keep an overlay the same size regardless of zoom, see here:
https://github.com/AckerApple/agm-overlays#zoom-sizing

We just called it bounds, and the boundaries are typically a small x/y grouping of numbers indicating in all directions of how to size the overlay at all times.

The demo code has one overlay with a fixed size definition as well:
https://github.com/AckerApple/agm-overlays/blob/master/example/src/app.template.ts#L44

@JulienBourgain
Copy link

He was talking about this : https://github.com/SebastianM/angular-google-maps/blob/master/docs/content/guides/implement-auto-fit-bounds.md

With these feature, you can put juste put markers on the map and it will set the center and the zoom level automaticaly.

It's so awesome !

In agm-marker, in ngOnChange, if latitude or longitude change, they just emit new values on a subject

@MaxBatt
Copy link

MaxBatt commented Jan 20, 2020

Hi,
I also would like to have the fit-bounds feature, meaning the zoom level is set automatically to see all available overlays. So this feature is not supported natively by agm-overlays?
@JulienBourgain I don't quite understand how to use your solution with agm-overlays. Could you please explain in a little more detail?

Thanks!

@AckerApple AckerApple added the help wanted Extra attention is needed label Feb 9, 2020
@d-silvas
Copy link

d-silvas commented Oct 2, 2020

There is a quick workaround in the line of what @JulienBourgain suggested: wrapping the agm-overlay component with a custom component that implements FitBoundsAccessor.

The custom component would look like:

import { FitBoundsAccessor, FitBoundsDetails } from '@agm/core';
import { Component, forwardRef, Input, OnInit } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

@Component({
    selector: 'app-map-bounds',
    template: ` <ng-content></ng-content> `,
    providers: [{ provide: FitBoundsAccessor, useExisting: forwardRef(() => MapBoundsComponent) }],
})
export class MapBoundsComponent implements OnInit, FitBoundsAccessor {
    @Input() latitude: number;
    @Input() longitude: number;

    fitBoundsDetails$ = new BehaviorSubject<FitBoundsDetails>(null);

    getFitBoundsDetails$(): Observable<FitBoundsDetails> {
        return this.fitBoundsDetails$.asObservable();
    }

    ngOnInit() {
        const latLng: google.maps.LatLngLiteral = { lat: this.latitude, lng: this.longitude };
        this.fitBoundsDetails$.next({ latLng });
    }
}

And then you would use it to wrap markers in the template where you define the map:

<agm-map [fitBounds]="true" style="height:400px;">
    <app-map-bounds [agmFitBounds]="true" *ngFor="let marker of markers" [latitude]="marker.latitude" [longitude]="marker.longitude">
        <agm-overlay [latitude]="marker.latitude" [longitude]="marker.longitude">
            <! -- Your content... -->
        </agm-overlay>
    </app-map-bounds>
</agm-map>

@egarkavy
Copy link

@d-silvas you have just made my day
So easy and so cool!
One little thing is that I would change ngOnInit to ngOnChanges, this way you will react on every lat/lon change if marker is dynamic
Also you can make component be OnPush

@ricavir11
Copy link

ricavir11 commented Dec 27, 2021

You can just create a marker collection that is not visible.

<agm-map [fitBounds]="true" style="height:400px;">
        <agm-overlay [latitude]="marker.latitude" [longitude]="marker.longitude">
            <! -- Your content... -->
        </agm-overlay>
         <agm-marker[latitude]="marker.latitude" [longitude]="marker.longitude" [visible]="false" [agmFitBounds]="true">
            <! -- Your content... -->
        </agm-marker>
</agm-map>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants