-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
I've added a gmaps component to my zul page and in java code I adds some markers based on latitude and longitude I get from database.
I would change map zoom so that all markers are visible.
I've found some javascript codes to do that but I don't know how to implement it using zk gmaps component.
This is gmaps component in zul page
<gmaps id="mymap" width="706px" height="300px" showSmallCtrl="true">
Here is the JS code:
function extendBounds(lat, lng) { alert("I'm here:" + lat + "-" + lng); var point = new GLatLng(lat,lng); map = new GMap2(mymap); bounds = map.getBounds(); bounds.extend(point); map.setZoom(map.getBoundsZoomLevel(bounds)); map.setCenter(bounds.getCenter()); }
and here is the java code I use to add the marker and to call the js function:
new Gmarker(content,Double.parseDouble(latitude), Double.parseDouble(longitude)).setParent(mymap); Clients.evalJavaScript("extendBounds(" + Double.parseDouble(latitude) + "," + Double.parseDouble(longitude) + ");");
Using Clients.evalJavascript, the javascript function is executed but the problem is that I'm not able to access the maps object.
Any suggestion?
Many thanks,
Giovanni
From 2010 Sep, to now has already pass half year. Is this still no solution?
How to get the Javascript Object?
Is there new function for setCenter(lat,lng,RoomLelvel) + getBoundsZoomLevel(bounds) in ZK gmap library?
I just saw in setCenter(lat,lng) setZoom() in org.zkoss.gmaps.Gmaps of javadoc.
I don't see any function(like map.getBoundsZoomLevel(bounds)) for get ZoomLevel From a specific Bound.
In Google Map there is a function which can get the suitable zoom level for Gmarkers located in the specific bound region. Therefore, we can see all Gmarkers in the same map.
I have read several times of javadoc and still cannot find a function like the "map.getBoundsZoomLevel(bounds)". Do you have any suggestion function?
is Gmaps.getZoom() what you want ?
after search online, "getBoundsZoomLevel" is API 3, I think current zk gmap is API 2 set, you can post feature request here
or, someone mentioned online, we can implement by our self
I had the same problem, so had a hunt around google to find out how I could implement it myself and came up with this.
1. Create the map
2. Add all the Gmarkers
3. Call the static method scaleMap passing in the map object
private static void scaleMap(Gmaps map) { Double minLat = null; Double maxLat = null; Double minLng = null; Double maxLng = null; // Work out the minimum and maximmum latitude and longitude // for (Object o : map.getChildren()) { if (o instanceof Gmarker) { Gmarker g = (Gmarker) o; if ((minLat == null) || (g.getLat() < minLat)) { minLat = g.getLat(); } if ((maxLat == null) || (g.getLat() > maxLat)) { maxLat = g.getLat(); } if ((minLng == null) || (g.getLng() < minLng)) { minLng = g.getLng(); } if ((maxLng == null) || (g.getLng() > maxLng)) { maxLng = g.getLng(); } } } // No markers found, so don't bother scaling if (minLat == null) { return; } // Calculate the centre Lat and Long // Double ctrLng = (maxLng + minLng) / 2; Double ctrLat = (maxLat + minLat) / 2; // The next calculation is sourced here http://aiskahendra.blogspot.com/2009/01/set-zoom-level-of-google-map-base-on.html // I have no idea what it's actually doing !!! // Double interval = 0.0; int mapDisplay = 600; // Minimum of height or width of map in pixels // Some sort of tweak ! if ((maxLat - minLat) > (maxLng - minLng)) { interval = (maxLat - minLat) / 2; minLng = ctrLng - interval; maxLng = ctrLng + interval; } else { interval = (maxLng - minLng) / 2; minLat = ctrLat - interval; maxLat = ctrLat + interval; } Double dist = (6371 * Math.acos(Math.sin(minLat / 57.2958) * Math.sin(maxLat / 57.2958) + (Math.cos(minLat / 57.2958) * Math.cos(maxLat / 57.2958) * Math.cos((maxLng / 57.2958) - (minLng / 57.2958))))); // Note ... original calc used 8, but I found it worked better with 7 Double zoom = Math.floor(7 - Math.log(1.6446 * dist / Math.sqrt(2 * (mapDisplay * mapDisplay))) / Math.log(2)); // Centre the map map.setCenter(ctrLat, ctrLng); // Set appropriate zoom map.setZoom(zoom.intValue()); }
To 'scale' the map is easy: no need for such elaborate calculations. Just find simple arithmetic average of your gmarkers and set center to that value. I usually create a 'center' marker that is placed on map too.
Asked: 2010-09-20 08:24:45 +0800
Seen: 1,994 times
Last updated: Jan 29 '16