From 030f80018bb8d904cbe871209bc26e6f341a1c70 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 8 Apr 2013 19:45:28 -0300 Subject: Minor fixes and getZoomByBounds method --- gmap_arcgis.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/gmap_arcgis.js b/gmap_arcgis.js index 9a9b4db..a2c8b53 100644 --- a/gmap_arcgis.js +++ b/gmap_arcgis.js @@ -4,7 +4,7 @@ function gmapArcgis(config) { return { // Storable properties config: config, - map: config.map, + map: (config.map != undefined) ? config.map : null, // UI unblocker wrapper unblockUI: function() { @@ -219,21 +219,21 @@ function gmapArcgis(config) { var fs = fset.features; var title; for (var i = 0, c = fs.length; i < c; i++) { - fset = fs[i]; + var feature = fs[i]; if (self.config['markers'][element].title != undefined) { - title = fset.attributes[self.config['markers'][element].title]; + title = feature.attributes[self.config['markers'][element].title]; } else { - title = fset.attributes.nome; + title = feature.attributes.nome; } self.config['markers'][element].overlay[i] = {}; self.config['markers'][element].overlay[i].marker = new google.maps.Marker({ title: title, icon: self.config['markers'][element].icon, - position: fset.geometry[0].getPosition(), - map: map, + position: feature.geometry[0].getPosition(), + map: self.map, }); // Use a closure so marker data remains available to the listeners @@ -241,7 +241,7 @@ function gmapArcgis(config) { var marker = self.config['markers'][element].overlay[i].marker; var infowindow = self.config['markers'][element].infowindow; var content = self.config['markers'][element].content; - var attributes = fset.attributes; + var attributes = feature.attributes; google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(content(attributes)); @@ -375,5 +375,35 @@ function gmapArcgis(config) { }); }, + /** + * Returns the zoom level at which the given rectangular region fits in the map view. + * The zoom level is computed for the currently selected map type. + * + * @param {google.maps.Map} map + * @param {google.maps.LatLngBounds} bounds + * @return {Number} zoom level + * @see http://stackoverflow.com/questions/9837017/equivalent-of-getboundszoomlevel-in-gmaps-api-3 + */ + getZoomByBounds: function(bounds) { + var map = this.map(); + var MAX_ZOOM = map.mapTypes.get(map.getMapTypeId()).maxZoom || 21; + var MIN_ZOOM = map.mapTypes.get(map.getMapTypeId()).minZoom || 0; + var ne = map.getProjection().fromLatLngToPoint(bounds.getNorthEast()); + var sw = map.getProjection().fromLatLngToPoint(bounds.getSouthWest()); + var worldCoordWidth = Math.abs(ne.x-sw.x); + var worldCoordHeight = Math.abs(ne.y-sw.y); + + // Fit padding in pixels + var FIT_PAD = 40; + + for (var zoom = MAX_ZOOM; zoom >= MIN_ZOOM; --zoom) { + if (worldCoordWidth*(1<