Centering Leaflet map with getbounds and related issues - javascript

I ran into a leaflet issue but I can't solve it. May be there's a easy solution I don't know.
So basically centering a map of grouped markers isn't so difficult and can be done like the following:
var markerLayer = L.featureGroup(marker)
.addTo(map);
var bounds = markerLayer.getBounds();
map.fitBounds(bounds);
where marker is an array of markers. But my problem is that the marker array information I send to this code are generated by another system. So basically the marker can be far apart in the map or they can be really close.
When the markers are far apart the code above works perfectly and centers the map. However if the markers are really close (for example if they are from the same street), or better yet if there is only one marker problems arise. This is because getbounds will give me a rectangle that is so small that leaflet breaks (and this is of no use for the user as well). I mean the map basically tries to zoom in to that marker. So my question is how do I limit the zoom. For example if there is only marker in the map I want the map to show the marker and few streets around it.

You can give a maxZoom to the map and the fitBounds will not override it.

The method map.fitBounds takes a parameter called maxZoom. This way you can have two maxZooms, one for the map and one for fitBounds.

Related

Is there a way to check if any part of a data layer in google maps is within the bounds of the map?

I have a list of geojson polygons that i am showing on my map, i am trying to determine, based on the current bounds of the map, if any part of the polygon is on the map. Any ideas as far as functions in the api to determine this?
I have tried the bounds.contains() method but it looks like that needs a specific point rather than an area.
Eric, I think you can get a center point of each polygon and set marker to it. So you can use it to represent a polygon.

CFGMap Google map API V3 always center map on long/lat

I'm using CFGMap solution to place markers on a google map (API V3) but I want the map to always be centered on a certain long/latitude. Right now it seems to center on the last marker placed on the map so if the majority are where I want the long/lat centered then one random marker will take view miles away.
Any help on this is appreciated.
Thanks!
CFGMap is infuriating for it's lack of documentation. I don't use it myself; I only tried to track down something that might help you. On the face of it, it appears only to take a tiny subset of the full API functionality and doesn't offer much else.
From what I can glean from the testmap.js found here, it appears that you can only center the map on an existing marker in your locations array (see line 616 of the github file).
_cfGMapObj.resetCenterToLoc(index);
So, for example, in the source of the example is the line:
_cfGMapObj.resetCenterToLoc($('#locID','#printLink').val());
which grabs the number from the value of the current option in the select at the top of the page.
Now, if you chose instead to knock CFGMap on the head you could employ the Map API fully and set the center of the map using the setCenter method of the API.:
map.setCenter(new google.maps.LatLng(lat, lng));
e.g.
map.setCenter(new google.maps.LatLng(57.4419, -121.1419));
which in my mind is much easier. Plus, if you decided to do this, there are tons more articles on the API on StackOverflow than there are on CFGMap. YMMV.

Bing Maps API -- setBounds

I'm converting a mapping utility from Gmap to Bing. In Google Maps, you can easily add markers to the map, and each time you add a marker, you add the location of the marker to a bounds object, then call setBounds() on the map to recenter the map to fit all markers.
I cannot find a similar method to work with Bing Maps, and the docs are pretty lousy compared to Google's.
So far I've got the map created and markers added-- but the map stays stuck at the initial view, rather than updating to fit the markers.
Is there an equivalent of the setBounds method for the Bing javascript API, or is there another way to handle this?
The way to do this in Bing is:
1) Use the LocationRect.fromLocations function (http://msdn.microsoft.com/en-us/library/gg427621.aspx) to create a bounding box from your pushpins locations
2) pass the LocationRect created in one to the map objects setViewFunction as part of the ViewOptions parameter http://msdn.microsoft.com/en-us/library/gg427628.aspx

Adding markers to Google Maps only when needed

I am able to display a Google Maps map on a web site and load hundreds of markers for the current map area and display them. The problem is how to behave when the map is zoomed in/out or is moved, so the map area changes.
There is a part of the map that is the same than before, and so are the markers in it. But there is a part of the map that is new and new markers should be added.
Right now I am doing the simplest solution, clearing all markers, retrieving markers for the current map area and add them. This produces a flickery effect on the markers (obviously) due to clear and add immediately after.
What is a efficient way to deal with this situation?
Is there a way to check if a marker has already been added to the map?
You can use a marker cluster or a clustering algorithm to group marker that are close together when you zoom out and ungroup them when you zoom in. It can significantly reduce the database query time and the map drawing. A good solution is a kd-tree.
To check if a marker has been added to the map, call the getMap method of the marker and if it returns a map object, it has been added.
To check if a marker is in the visible area of the map, you can call the getBounds method on the map which returns a LatLngBounds. Then call the contains method of the returned LatLngBounds passing the coordinates of the marker.
Hope this helps

Determining if a marker is visible in Google Maps

I am developing a Google Maps application and I've run into this problem. I need to remove all markers which are out of bounds from the map.
Is there any simple way of doing this, besides keeping an array and looking at the latlng of each marker?
I cannot use MarkerManager because I have way too many points. I don't want to use clearOverlays() because it would close any open marker.
Any help would be appreciated.
If you don't want to look at each marker individually then cluster them into some sets initially and calculate the bounds of the set.
You can then show or hide the sets depending on what is currently showing on the map (you can find the boundary of the map using GMap2.getBounds() ).
How many points are we talking about?
Update
A. About 65K.
I can see why you can't create 65K GMarkers when the page loads. That will take over 5 seconds.
I'd cluster them into groups of 200 ish and when the edge of the group gets within a 1/4 map width outside of the displayed edge then find, create markers and display the adjacent group. It it goes outside of 1/2 a map width of the outside edge then hide the group.
Other alternatives are to use a third party library such as
Cluster Marker - http://googlemapsapi.martinpearman.co.uk/articles.php?cat_id=1
Clusterer - http://www.acme.com/javascript/#Clusterer

Categories

Resources