Remove off-screen markers

381 views
Skip to first unread message

ma...@designagency.co.uk

unread,
Jan 14, 2014, 11:40:37 AM1/14/14
to leafl...@googlegroups.com
Hopefully this is something simple..

Basically, I want only markers that are visible in the current viewport to be on the page, and then be removed when scrolled away from.

Is this possible, and if so, what obvious thing have I missed?

Thanks,

Mark

Morten Fuglsang

unread,
Jan 15, 2014, 3:02:28 AM1/15/14
to leafl...@googlegroups.com
You can do it using php pretty straight forward, only loading the markers that are currently within your bbox to you application, and removing them if they fall outside.

/Morten

ma...@designagency.co.uk

unread,
Jan 15, 2014, 11:57:27 AM1/15/14
to leafl...@googlegroups.com
Ah, php isn't an option for this one, it's just a very basic and pretty small custom map on a html page. I was wondering if there was any config options I could hook into using js.

It basically stems from having  number of animated markers which can cause fairly heavy CPU usage depending on the device. It's not a deal breaker, but any gain is a good one :-)

Thanks for the thought though.

Mark

Sylvio Ruiz

unread,
Jan 15, 2014, 3:14:36 PM1/15/14
to leafl...@googlegroups.com
Off course, you have it:

// Get the CurrentView
var CurrentView = map.getBounds();

// Check if the Current View contains your marker
if ( CurrentView.contains(marker.getLatLng()) ) { /* inside the bound */ } else { /* outside the bound */ }

ma...@designagency.co.uk

unread,
Jan 16, 2014, 8:47:19 AM1/16/14
to leafl...@googlegroups.com
That is exactly what I was looking for, many thanks.

One query though, when adding my markers back to the map, some are not being put back in the correct place. They are all divIcons, each has a size defined and it's lat/lng. The ones that are going back wrong happen to have child html elements (again, just divs).

I'll possibly add this as a new topic as well, but just in case you have any ideas..

Thanks.

Mark

Sylvio Ruiz

unread,
Jan 16, 2014, 3:43:55 PM1/16/14
to leafl...@googlegroups.com
Hello Mark,

Just a question ...

Are you using "getLatLng()" to store the location for that marker when you remove it from the layer?

If the answer is "Yes", the accuracy of the point may be different from the originally used, because the float precision.

Check both LatLng, using the console to log the inital LatLng and the LatLng stored.

Regards;
Sylvio

ma...@designagency.co.uk

unread,
Jan 17, 2014, 4:01:39 AM1/17/14
to leafl...@googlegroups.com
Hi Sylvio,

I'm actually creating an array of marker when the page loads which I'm using to both populate the initial group of markers (all in one go) and also re-add the markers when they come back into the bounds, so the lat/lng values should be identical.

It's strange because sometimes if I zoom back out of the map a bit, the markers go to the correct position, but zooming in again re-adds them to the wrong place.

I'll check that the 2 LatLng values are the same as suggested anyway and report back if that is somehow the issue..

Many thanks,

Mark

ma...@designagency.co.uk

unread,
Jan 17, 2014, 5:22:11 AM1/17/14
to leafl...@googlegroups.com
Ok, I think I've answered my own question here..

It's happening on the markers which have html children because of the positioning I was giving to the markers, overriding the absolute with relative so that I could absolutely position the children elements. I have now added a relatively positioned child container for these markers and it appears to be solving the issue.

Thanks for all the help!

Mark
Reply all
Reply to author
Forward
0 new messages