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

Bug: fitBounds counts old marker's positions #1512

Closed
ghost opened this issue Sep 27, 2018 · 8 comments · Fixed by #1710
Closed

Bug: fitBounds counts old marker's positions #1512

ghost opened this issue Sep 27, 2018 · 8 comments · Fixed by #1710

Comments

@ghost
Copy link

ghost commented Sep 27, 2018

When marker's position has been changed and it has [agmFitBounds]='true' directive, google.maps.LatLngBounds gets extended by new and old position. So agm-map is fitting both positions.

Steps to reproduce:

  1. Create instance with [fitBounds]="true" flag
  2. Insert inside agm-map with [agmFitBounds] directive
  3. Change programmatically latitude and longitude of marker

Current behavior:

  • fitsBounds uses new and old positions

Expected/desired behavior:

  • count only new position of observable marker

Versions:
Angular 6 & @agm/core: 1.0.0-beta.5

@massic80
Copy link

I'm having the same issue: in particular,

<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom" [fitBounds]="fitBounds" [styles]="styles" id="map-place" (mapReady)="mapIsReady()">
	<agm-marker *ngIf="myLocation!==undefined" [agmFitBounds]="true" [latitude]="myLocation.lat" [longitude]="myLocation.lng" [iconUrl]="'/assets/images/marker-mylocation.svg'" [markerClickable]="false"></agm-marker>
	<agm-marker *ngFor="let m of markers; index as i" [agmFitBounds]="m.fitBounds" [latitude]="m.coords.lat" [longitude]="m.coords.lng" [iconUrl]="getIcon(i)" (markerClick)="markerClicked(i)" [markerClickable]="true"></agm-marker>
</agm-map>

On landing I feed the agm-map with [latitude], [longitude] and [zoom] in order to set the map to the default location. fitBoundsis false.
As soon as I get the first "myLocation" marker, I set the fitBounds to true (I figure out fitBoundshas a priority on latitude, longitude and zoom, am I wrong?) and anything works perfectly when I add other markers around.
As @warcatz76 reported, the problem occurs when I remove the markers (this.markers=[]) and I add new ones elsewhere. Older ones aren't removed from fitBounds limits. And a big useless area is kept inside the map.

@stale
Copy link

stale bot commented Feb 26, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Feb 26, 2019
@massic80
Copy link

Dear Bot, I don't know whether @SebastianM did solve this, but I didn't see any new release coming in :)

@stale stale bot removed the stale label Feb 27, 2019
@stale
Copy link

stale bot commented May 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Shred13
Copy link

Shred13 commented Jul 4, 2019

I am also receiving this error, is there any new developments on how we can fix this?

@massic80
Copy link

massic80 commented Jul 5, 2019

I did it manually.
<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom" [styles]="styles" id="map-place" (mapReady)="mapIsReady($event)">

mapIsReady(map: google.maps.Map): void { this.map = map; }

and then
this.map.fitBounds({ south: 0, west: 0, north: 50, east: 50 });
whenever necessary.

@ghost
Copy link
Author

ghost commented Jul 5, 2019

I have a workaround for this bug:
<agm-map [fitBounds]="true"></agm-map>

<agm-marker *ngFor="let m of [source]" [agmFitBounds]="true" [latitude]="m.lat" [longitude]="m.lng"></agm-marker>

Just loop over an array of one element (source of marker data). As soon as the source will be changed old marker will be destroyed and a new one created. Probably not the best solution from the performance point of view, but works.

@ghost ghost closed this as completed in #1710 Aug 26, 2019
ghost pushed a commit that referenced this issue Aug 26, 2019
when a FitBoundsAccessor emits new bounds,  remove the old ones from
the map's bounds array

fixes: #1512
@Pman143
Copy link

Pman143 commented Apr 29, 2020

<agm-map (mapReady)="onMapReady($event)">

onMapReady(event) {
this.mapReady = event;
}

const bounds: LatLngBounds = new google.maps.LatLngBounds();
for (const pol of this.polyLinePoints) {
bounds.extend(new google.maps.LatLng(pol.lat, pol.lng));
}
this.mapReady.fitBounds(bounds);

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

Successfully merging a pull request may close this issue.

3 participants