angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

Fit Map Bounds to Makers/Elements Automatically?

Open jongunter opened this issue 9 years ago • 38 comments

Can we add a Boolean input binding to the SebmGoogleMap component that automatically fits the map to the aggregate bounds of the markers/shapes/other map elements it contains?

If I'm not mistaken, it's pretty easy to do in the native Google Maps JS API..you just create a new bounds, then loop through all the elements on the map and extend the bounds, the finally tell the map object for fit the bounds.

Currently, its a pain to manually update the map bounds every time a marker changes. Thanks.

jongunter avatar Dec 07 '16 21:12 jongunter

Yeah I think also that this is a "must have" feature. But I'm struggling with a good API design. Do you have a proposal?

sebholstein avatar Dec 15 '16 18:12 sebholstein

I'm no expert in Angular2 API design, but what if it looked like this? Rather than specifying a zoom level and center lat/lng, you just give a fit-contents boolean?

<sebm-google-map [fitContents]="true">
...
</sebm-google-map>

...or maybe a an attribute directive?

<sebm-google-map sebmGoogleMapFitContents>
...
</sebm-google-map>

jongunter avatar Dec 22 '16 03:12 jongunter

Support of bound to markers would be nice. Would prefer [fitContents]="true" as attribute

FabianCernota avatar Jan 11 '17 21:01 FabianCernota

I would definitely favor [fitContents]="true" (or a similar name) over an attribute directive. It's impossible (as far as I know) to toggle presence of a directive easily, and with a boolean input it's simple to switch between the two.

It's also potentially better autocomplete in IDEs to have an input than a directive (eg. right now in WebStorm you type [ and get suggestions of all inputs -- directives aren't handled that well at the moment).

lazarljubenovic avatar Jan 18 '17 08:01 lazarljubenovic

I'm doing research on the below requirement,

I need to make the map auto zoom / center based on the map points to be shown. based on the google api and this library it looks like i need to make a "bounds" object and call .extend for each marker i need to show and then set or bind the fitBound property. where i am stuck right now is how to populate the bounds object with the data. i am having issues with how to create the LatLng object . in the code i see an interface that has methods but no class that implements the interface and the latlng interface says the lat and lng are methods not properties. what am i missing in this picture ??

Could you please provide some sample solution or code snippet to zoom the markers area by using fitbounds?

VaseemMalik avatar Jan 24 '17 14:01 VaseemMalik

Been thinking about this. "Fit content" sounds more like something of an action to me, rather than a property which can be turned on or off.

Some brainstorming:

  • A service with a method .fitContent() which would trigger latitude/longitude/zoom change (which can be regularly caught as an event from their respective outputs centerChange/zoomChange). But how do we specify which map to apply that on? My DI got rusty. We might need to get access to component instance with @ViewChild.
  • If we need @ViewChild anyway, we could make a public .fitContent() inside the component itself, without a service.
  • We could pass in an observable which the component would subscribe to and internally call .fitContent() whenever fired.

The most common use-case would be to always have all markers fit on the map. For that we could supply a directive/input which would execute .fitContent() whenever a change is detected in markers.

Feel free to discuss the API/behaviour here. I'll do some prototyping this week for this to see where it takes me.

lazarljubenovic avatar Jan 24 '17 16:01 lazarljubenovic

A colleague and I have solved this myself with a directive. The directive is added to the sebm-google-map element like this (where [fitContents] is our custom directive):

<sebm-google-map
                [fitContents]="fitContents"
                #bounds=fitContentsDirective
                [fitBounds]="bounds.mapBounds">
</sebm-google-map>

The fitContents directive has an input that is a Subject<Coordinates[]>. Every time there's an update to the map markers, the subject is updated with fitContents.next(coordinates) where "coordinates" is the full list of coordinates that should fit in the map. The directive has a public property called mapBounds. This property contains a google LatLngBounds object that is passed into the [fitBounds] input of sebm-google-map.

The html template might not look pretty, but this works fine until some kind of official support should arrive.

The directive is available here: https://gist.github.com/glenngr/8b64194b86e4101386db22999813af10 Input, comments and improvements are welcome.

If a similar concept had been introduced into the angular2-google-maps component, some of the less pretty html syntax could perhaps be eliminated.

glenngr avatar Jan 24 '17 19:01 glenngr

@jongunter @VaseemMalik @lazarljubenovic thx guys for the great discussion here!

I actually started to work on it a few days ago. It's not working yet, but you should get the idea: https://github.com/SebastianM/angular2-google-maps/pull/868

Comments/ideas are very welcome!

sebholstein avatar Jan 25 '17 07:01 sebholstein

any update on this ?

Anoxy avatar May 26 '17 08:05 Anoxy

Any update ?

craftpip avatar May 31 '17 19:05 craftpip

Any update?

jimmykane avatar Jun 21 '17 14:06 jimmykane

Will be the next thing I'll finish once #1044 landed

sebholstein avatar Jun 22 '17 19:06 sebholstein

Great!

jimmykane avatar Jun 22 '17 20:06 jimmykane

Here for single polygon

public findCenterBounds(): LatLngBoundsLiteral {
    let mostEast  = this.paths.reduce( (acc, v) => (acc.lng < v.lng) ? v : acc );
    let mostWest  = this.paths.reduce( (acc, v) => (acc.lng > v.lng) ? v : acc );
    let mostNorth = this.paths.reduce( (acc, v) => (acc.lat < v.lat) ? v : acc );
    let mostSouth = this.paths.reduce( (acc, v) => (acc.lat > v.lat) ? v : acc );
    return <LatLngBoundsLiteral>{east: mostEast.lng, west: mostWest.lng, north: mostNorth.lat, south: mostSouth.lat };
  }

this._mapsApiWrapper.fitBounds(this.findCenterBounds());

ghost avatar Jun 27 '17 12:06 ghost

@piotrmach Top! great!

jimmykane avatar Jun 27 '17 16:06 jimmykane

If you only need two points, simplification based on @piotrmach solution:

public findCenterBounds(): LatLngBoundsLiteral {

    let a = this.origin;
    let b = this.destination;

    let mostEast  = (a.longitude > b.longitude) ? a.longitude : b.longitude;
    let mostWest  = (a.longitude < b.longitude) ? a.longitude : b.longitude;
    let mostNorth = (a.latitude > b.latitude) ? a.latitude : b.latitude;
    let mostSouth = (a.latitude < b.latitude) ? a.latitude : b.latitude;
    return <LatLngBoundsLiteral>{east: mostEast, west: mostWest, north: mostNorth, south: mostSouth };
  }

And don't forget to properly inject the native API, see https://stackoverflow.com/questions/44315771/setcenter-not-working-in-angular2-google-maps

It took me some time to figure out why fitBounds is not doing anything. AGM docs are nonexistent. (sadface)

cen1 avatar Jul 25 '17 13:07 cen1

Any update on this?

penniath avatar Aug 31 '17 12:08 penniath

Any update ?

BayardSmart avatar Sep 04 '17 09:09 BayardSmart

Any update ?

Bhargav-Narayan avatar Oct 18 '17 12:10 Bhargav-Narayan

Any update?

mmeendez8 avatar Oct 19 '17 08:10 mmeendez8