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

Load only those markers/clusters which fit current window size/zoom - Angular 6, agm

Open a-k-holod opened this issue 8 years ago • 19 comments

Issue description I have over thousand markers (one city) on my map so app is loading super slow. When I tied them in clusters things are a bit better but it's still not acceptable.

I want to load only those markers/clusters that fit in current window size/zoom, so if I have zoom like 13, app loads only 10 markers in 5 clusters instead of all thousand and when I move my map then another clusters are loaded.

Steps to reproduce and a minimal demo of the problem

I have no demo as I have no idea for this issue :(

_Use https://plnkr.co or similar -- try this template as a starting point: http://plnkr.co/edit/YX7W20?p=preview

What steps should we try in your demo to see the problem?

Current behavior

All markers are loading on init and kill efficiency.

Expected/desired behavior

Only markers/clusterers which are located in area of window view should be loaded. After moving map another pack of them should be rendered etc.

angular2 & angular-google-maps version Angular 6 "@agm/core": "^1.0.0-beta.3", "@agm/js-marker-clusterer": "^1.0.0-beta.5", "js-marker-clusterer": "^1.0.0",

Other information

a-k-holod avatar Oct 04 '18 11:10 a-k-holod

I saw some solitions using <sebm-google-map-markers [markers]="myMarkersArray"/ fit="true"/other options> but I'm not sure how to use this tag... Isn't it from some old version?

a-k-holod avatar Oct 05 '18 10:10 a-k-holod

https://googlemaps.github.io/js-marker-clusterer/examples/advanced_example.html This is basically what I want but I can't figure out how to put it in TypeScript to work with Angular

a-k-holod avatar Oct 05 '18 12:10 a-k-holod

I think agm has it's own version already, I could be wrong, but you might want to look here if you're interested in a marker clusterer to improve performance: https://github.com/SebastianM/angular-google-maps/blob/master/packages/js-marker-clusterer/services/managers/cluster-manager.ts

Also, though, to help with your original question, it should be pretty straightforward, just set up a listener when the map bounds change, to iterate your markers, and hide them if the bounds does not contain the point. (although depending on your marker, make sure you add a buffer area to the bounds, so if a marker is only partially off the map, it doesn't get turned off.

meadowsjared avatar Oct 06 '18 07:10 meadowsjared

how can i implement same approach as Booking.com does. for example if i have 1000 hotels in specific area, and in map i show only 100 hotel, and when i zoom in over the map i bring new hotels portion and show them. i don't want to load all hotels from the first time to not face performance issue.

malek-itani avatar Dec 31 '18 14:12 malek-itani

@malek-itani did you find something?

AncaElena10 avatar Mar 01 '19 09:03 AncaElena10

@malek-itani @AncaElena10 I am also wondering about this.

mrjoedang avatar May 17 '19 18:05 mrjoedang

<agm-map (boundsChange)="updateMarkers($event)">
 <agm-marker *ngFor="let marker of displayedMarkers" [latitude]="marker.latitude" [longitude]="marker.longitude" />
</agm-map>
markers: LatLngLiteral[];
displayedMarkers: LatLngLiteral[];

updateMarkers(newBounds: LatLngBounds) {
  this.displayedMarkers = this.markers.filter(marker => newBounds.contains(marker));
}

ghost avatar Jul 18 '19 05:07 ghost

We're unlikely to implement this logic inside AGM, however the snippet I provided above should work and do what is required.

ghost avatar Jul 18 '19 05:07 ghost

markers: LatLngLiteral[];
displayedMarkers: LatLngLiteral[];
updateMarkers(newBounds: LatLngBounds) {
 this.displayedMarkers = this.markers.filter(marker => newBounds.contains(marker));
}

Can you explain a bit on this? And how to use this on the latest AGM with typescript?

weilinzung avatar Sep 15 '19 17:09 weilinzung

Yes. You will have two arrays of markers: all markers, and displayed markers. Displayed markers are all markers which are inside the bounds of the map.

Every time the bounds of the map changes, the array of displayed markers is recalculated by filtering the all-markers array, to leave only the ones that are in the map bounds.

ghost avatar Sep 16 '19 14:09 ghost

@doom777

So am I correct as this example which they just loop the same values?

markers: LatLngLiteral[
 {
  lat: 1.111
  lng: 2.22
 },
 {
  lat: 42.111
  lng: 132.22
 },
...
];

displayedMarkers: LatLngLiteral[
 {
  lat: 1.111
  lng: 2.22
 },
 {
  lat: 42.111
  lng: 132.22
 },
...
];

Also I try to use in TS, but it gave me this error: Cannot find name 'LatLngBounds'.ts(2304)?

"typescript": "~3.4.3"

Thank you so much!!!

weilinzung avatar Sep 17 '19 13:09 weilinzung

No, displayed markere array is dynamically calculated every times the bounds change

Sent from Yahoo Mail on Android

On יום ג׳, ספט' 17, 2019 at 16:19, Wei[email protected] wrote:
@doom777

So am I correct as this example which they just loop the same values? Also I try to use in TS, but it gave me this error: Cannot find name 'LatLngBounds'.ts(2304)? markers: LatLngLiteral[ { lat: 1.111 lng: 2.22 }, { lat: 42.111 lng: 132.22 }, ... ];

displayedMarkers: LatLngLiteral[ { lat: 1.111 lng: 2.22 }, { lat: 42.111 lng: 132.22 }, ... ];

Thank you so much!!!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ghost avatar Sep 17 '19 13:09 ghost

@doom777

Thank you. Can you create a simple example of how this works? Maybe just 3-5 different locations.

weilinzung avatar Sep 17 '19 22:09 weilinzung

You want to display some markers. The locations where you want the markers to be, comes from some data source as a list of coordinates.

This list of coordinates will be set to the markers array in the component class.

This issue is about display not ALL of the markers, but only the markers inside the current window size/zoom. Another name for current window size/zoom, is 'bounds'. What I proposed, is that on load, + every time the bounds change, you take the entire list of markers, stored in markers property, run it through a filter which will keep all the markers that are in the bounds, and discard all the markers which are outside bounds, and store this new filtered array in displayedMarkers. Then, you only display markers from displayedMarkers array.

ghost avatar Sep 18 '19 16:09 ghost

@doom777 I understand your IDEA and I have ~400 markers, so this is probably a solution for me. I am new to angular with AGM, SO I want to see if you have a working demo.

Here are my codes, and seems only allow use to lat & lng keys and also some errors on this line with TS: marker => newBounds.contains(marker)

(parameter) marker: LatLngLiteral
Argument of type 'LatLngLiteral' is not assignable to parameter of type 'LatLng'.
  Property 'toJSON' is missing in type 'LatLngLiteral' but required in type 'LatLng'.ts(2345)
google-maps-types.d.ts(19, 5): 'toJSON' is declared here.

  markers: LatLngLiteral[] = [
  {
    lat: 51.673858,
    lng: 7.815982,
    label: 'A',
    draggable: true
  },
  {
    lat: 51.373858,
    lng: 7.215982,
    label: 'B',
    draggable: false
  },
  {
    lat: 51.723858,
    lng: 7.895982,
    label: 'C',
    draggable: true
  }
];

  displayedMarkers: LatLngLiteral[];

  updateMarkers(newBounds: LatLngBounds) {
    console.log('bounds changed');
    this.displayedMarkers = this.markers.filter(
      marker => newBounds.contains(marker)
    );
  }

weilinzung avatar Sep 18 '19 18:09 weilinzung

Hi @weilinzung, I have solved this issue by override the contains method. From: contains(latLng: LatLng): boolean; To: contains(latLng: LatLngLiteral): boolean; Than: updateMarkers(newBounds: LatLngBounds) { this.displayedMarkers = this.markers.filter(marker =>newBounds.contains(marker.latLng)); } Where: interface marker { latLng: LatLngLiteral; lat: number; lng: number; label?: {}; icon?: {}; controllerInfo?: {}; controllerStaus?: {}; }

If you have any questions you welcome to ask.

MishaBeskin avatar Jan 12 '20 13:01 MishaBeskin

Hi @weilinzung, I have solved this issue by override the contains method. From: contains(latLng: LatLng): boolean; To: contains(latLng: LatLngLiteral): boolean; Than: updateMarkers(newBounds: LatLngBounds) { this.displayedMarkers = this.markers.filter(marker =>newBounds.contains(marker.latLng)); } Where: interface marker { latLng: LatLngLiteral; lat: number; lng: number; label?: {}; icon?: {}; controllerInfo?: {}; controllerStaus?: {}; }

If you have any questions you welcome to ask.

Hi. Can you help showing me how to override contains(latLng: LatLng) with contains(latLng: LatLngLiteral) ?

alinsondonato avatar Apr 23 '20 18:04 alinsondonato

I have solved this issue by override the contains method. From: contains(latLng: LatLng): boolean;

Hi. I have the same problem that @weilinzung. How can i override contains(latLng: LatLng) with contains(latLng: LatLngLiteral) ?

alinsondonato avatar Apr 23 '20 18:04 alinsondonato

Hope this helps someone else out there. I was hitting the same error trying to understand how to override "contains". After some trial and error, this is what I came up with:

markers: EnrichedLatLngLiteral[] = [];
displayedMarkers: EnrichedLatLngLiteral[] = [];

type EnrichedLatLngBounds = Omit<LatLngBounds, "contains"> & {
  contains(latLng: LatLngLiteral): boolean;
};

export class EnrichedLatLngLiteral implements LatLngLiteral {
  entityId: string;
  addressId: string;
  name: string;
  line1?: string;
  line2?: string;
  city?: string;
  state?: string;
  postalCode?: string;
  lat: number;
  lng: number;
}

updateMarkers(newBounds: EnrichedLatLngBounds) {
  this.displayedMarkers = this.markers.filter((marker) =>
    newBounds.contains(marker)
  );
}

chrisjmccrum avatar Jul 04 '20 04:07 chrisjmccrum