From 26fb3758e7a55c68566f7cc395804ee9f4a81455 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Tue, 27 Aug 2013 12:23:53 +0200 Subject: [PATCH] [IMP] website_crm_partner_assign: new module for display worldmap for list of partner. Call the template in an iframe (with get options: partner_id, width, height, partner_url). bzr revid: chm@openerp.com-20130827102353-htupokzt9nmaiyeu --- addons/website/website.py | 2 +- addons/website_contract/__openerp__.py | 2 +- addons/website_contract/controllers/main.py | 5 + .../views/website_contract.xml | 2 + addons/website_crm/__init__.py | 1 + addons/website_crm/__openerp__.py | 4 +- addons/website_crm/controllers/main.py | 36 + addons/website_crm/website_crm.py | 9 + addons/website_crm_partner_assign/__init__.py | 2 + .../website_crm_partner_assign/__openerp__.py | 17 + .../controllers/__init__.py | 3 + .../controllers/main.py | 83 ++ .../static/src/img/partners.png | Bin 0 -> 2013 bytes .../static/src/js/markerclusterer.js | 1294 +++++++++++++++++ .../static/src/js/markerclusterer_compiled.js | 21 + .../static/src/js/worldmap.js | 84 ++ .../views/worldmap.xml | 42 + .../website_crm_partner_assign.py | 9 + 18 files changed, 1611 insertions(+), 5 deletions(-) create mode 100644 addons/website_crm/website_crm.py create mode 100644 addons/website_crm_partner_assign/__init__.py create mode 100644 addons/website_crm_partner_assign/__openerp__.py create mode 100644 addons/website_crm_partner_assign/controllers/__init__.py create mode 100644 addons/website_crm_partner_assign/controllers/main.py create mode 100644 addons/website_crm_partner_assign/static/src/img/partners.png create mode 100644 addons/website_crm_partner_assign/static/src/js/markerclusterer.js create mode 100644 addons/website_crm_partner_assign/static/src/js/markerclusterer_compiled.js create mode 100644 addons/website_crm_partner_assign/static/src/js/worldmap.js create mode 100644 addons/website_crm_partner_assign/views/worldmap.xml create mode 100644 addons/website_crm_partner_assign/website_crm_partner_assign.py diff --git a/addons/website/website.py b/addons/website/website.py index 352cf921d72..48276bfa350 100644 --- a/addons/website/website.py +++ b/addons/website/website.py @@ -90,7 +90,7 @@ class website(osv.osv): except ValueError: logger.error("Website Rendering Error.\n\n%s" % traceback.format_exc()) return self.render('website.404', values) - + # render template and catch error try: return view.render(request.cr, request.uid, template, values, context=context) diff --git a/addons/website_contract/__openerp__.py b/addons/website_contract/__openerp__.py index 3be38778530..978552d23f5 100644 --- a/addons/website_contract/__openerp__.py +++ b/addons/website_contract/__openerp__.py @@ -9,7 +9,7 @@ OpenERP Blog """, 'author': 'OpenERP SA', - 'depends': ['website', 'sale'], + 'depends': ['website', 'sale', 'website_crm_partner_assign'], 'data': [ 'views/website_contract.xml', ], diff --git a/addons/website_contract/controllers/main.py b/addons/website_contract/controllers/main.py index f1349b2c142..19da7c30677 100644 --- a/addons/website_contract/controllers/main.py +++ b/addons/website_contract/controllers/main.py @@ -20,6 +20,7 @@ class website_contract(http.Controller): # public partner profile partner_ids = partner_obj.search(request.cr, openerp.SUPERUSER_ID, domain + [('website_published', '=', True)]) + worldmap_partner_ids = ",".join([str(p) for p in partner_ids]) if request.uid != website.get_public_user().id: contract_ids = account_obj.search(request.cr, openerp.SUPERUSER_ID, [(1, "=", 1)]) @@ -36,6 +37,7 @@ class website_contract(http.Controller): values = website.get_rendering_context({ 'partner_ids': partner_obj.browse(request.cr, openerp.SUPERUSER_ID, partner_ids), + 'worldmap_partner_ids': worldmap_partner_ids, 'pager': pager, 'search': post.get("search"), }) @@ -46,6 +48,9 @@ class website_contract(http.Controller): website = request.registry['website'] partner_obj = request.registry['res.partner'] partner_ids = partner_obj.search(request.cr, openerp.SUPERUSER_ID, [('website_published', '=', True), ('id', '=', ref_id)]) + if request.uid != website.get_public_user().id: + partner_ids += partner_obj.search(request.cr, request.uid, [('id', '=', ref_id)]) + values = website.get_rendering_context({ 'partner_id': partner_obj.browse(request.cr, openerp.SUPERUSER_ID, partner_ids[0], context={'show_address': True}), }) diff --git a/addons/website_contract/views/website_contract.xml b/addons/website_contract/views/website_contract.xml index 2c036ef870e..a822a8cc246 100644 --- a/addons/website_contract/views/website_contract.xml +++ b/addons/website_contract/views/website_contract.xml @@ -67,6 +67,8 @@ () + diff --git a/addons/website_crm/__init__.py b/addons/website_crm/__init__.py index ee5959455ad..9b7fc18e45e 100644 --- a/addons/website_crm/__init__.py +++ b/addons/website_crm/__init__.py @@ -1 +1,2 @@ import controllers +import website_crm \ No newline at end of file diff --git a/addons/website_crm/__openerp__.py b/addons/website_crm/__openerp__.py index 223d014cd09..fe4311def44 100644 --- a/addons/website_crm/__openerp__.py +++ b/addons/website_crm/__openerp__.py @@ -11,9 +11,7 @@ OpenERP Contact Form 'author': 'OpenERP SA', 'depends': ['website', 'crm'], 'data': [ - 'views/website_crm.xml' + 'views/website_crm.xml', ], - 'js': ['static/src/js/ecommerce.js'], - 'css': ['static/src/css/ecommerce.css'], 'installable': True, } diff --git a/addons/website_crm/controllers/main.py b/addons/website_crm/controllers/main.py index 83613e108d4..a9b90f64c5a 100644 --- a/addons/website_crm/controllers/main.py +++ b/addons/website_crm/controllers/main.py @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- +import openerp from openerp.addons.web import http from openerp.addons.web.http import request +import simplejson +import werkzeug.wrappers from urllib import quote_plus @@ -19,4 +22,37 @@ class contactus(http.Controller): }) return website.render("website_crm.thanks", values) + @http.route(['/crm/worldmap/'], type='http', auth="admin") + def worldmap(self, *arg, **post): + website = request.registry['website'] + values = website.get_rendering_context() + values['partner_ids'] = post.get('partner_ids', "") + return website.render("website_crm.worldmap", values) + + @http.route(['/crm/worldmap/partners.json'], type='http', auth="admin") + def worldmap_data(self, *arg, **post): + website = request.registry['website'] + partner_obj = request.registry['res.partner'] + + domain = [("id", "in", [int(p) for p in post.get('partner_ids', "").split(",") if p])] + partner_ids = partner_obj.search(request.cr, openerp.SUPERUSER_ID, domain + [('website_published', '=', True)]) + if request.uid != website.get_public_user().id: + partner_ids += partner_obj.search(request.cr, request.uid, domain) + + data = { + "counter": len(partner_ids), + "partners": [] + } + for partner in partner_obj.browse(request.cr, openerp.SUPERUSER_ID, partner_ids, context={'show_address': True}): + data["partners"].append({ + 'id': partner.id, + 'name': partner.name, + 'address': '\n'.join(partner.name_get()[0][1].split('\n')[1:]), + 'type': "Silver Partners" + }) + + mime = 'application/json' + body = "var data = " + "}, \n{".join(simplejson.dumps(data).split("}, {")) + return werkzeug.wrappers.Response(body, headers=[('Content-Type', mime), ('Content-Length', len(body))]) + # vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/website_crm/website_crm.py b/addons/website_crm/website_crm.py new file mode 100644 index 00000000000..82d5390ec71 --- /dev/null +++ b/addons/website_crm/website_crm.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +from openerp.osv import osv, fields + +class res_partner(osv.osv): + _inherit = 'res.partner' + _columns = { + 'website_published': fields.boolean('Available in the website'), + } diff --git a/addons/website_crm_partner_assign/__init__.py b/addons/website_crm_partner_assign/__init__.py new file mode 100644 index 00000000000..8fddc880d43 --- /dev/null +++ b/addons/website_crm_partner_assign/__init__.py @@ -0,0 +1,2 @@ +import controllers +import website_crm_partner_assign \ No newline at end of file diff --git a/addons/website_crm_partner_assign/__openerp__.py b/addons/website_crm_partner_assign/__openerp__.py new file mode 100644 index 00000000000..7ce09ad4e27 --- /dev/null +++ b/addons/website_crm_partner_assign/__openerp__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Website Worldmap', + 'category': 'Website', + 'summary': '', + 'version': '1.0', + 'description': """ +OpenERP Website Worldmap +======================== + + """, + 'author': 'OpenERP SA', + 'depends': ['website', 'website_crm', 'crm_partner_assign'], + 'data': [ + 'views/worldmap.xml', + ], + 'installable': True, +} diff --git a/addons/website_crm_partner_assign/controllers/__init__.py b/addons/website_crm_partner_assign/controllers/__init__.py new file mode 100644 index 00000000000..e11f9ba81bb --- /dev/null +++ b/addons/website_crm_partner_assign/controllers/__init__.py @@ -0,0 +1,3 @@ +import main + +# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/website_crm_partner_assign/controllers/main.py b/addons/website_crm_partner_assign/controllers/main.py new file mode 100644 index 00000000000..299be4fddca --- /dev/null +++ b/addons/website_crm_partner_assign/controllers/main.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- + +import openerp +from openerp.addons.web import http +from openerp.addons.web.http import request +import simplejson +import werkzeug.wrappers +from datetime import datetime + +from urllib import quote_plus + +class contactus(http.Controller): + + @http.route(['/crm/contactus'], type='http', auth="admin") + def contactus(self, *arg, **post): + website = request.registry['website'] + post['user_id'] = False + request.registry['crm.lead'].create(request.cr, request.uid, post) + values = website.get_rendering_context() + company = values['res_company'] + values.update({ + 'google_map_url': "http://maps.googleapis.com/maps/api/staticmap?center=%s&sensor=false&zoom=8&size=298x298" % quote_plus('%s, %s %s, %s' % (company.street, company.city, company.zip, company.country_id and company.country_id.name_get()[0][1] or '')) + }) + return website.render("website_crm.thanks", values) + + @http.route(['/worldmap/'], type='http', auth="admin") + def worldmap(self, *arg, **post): + website = request.registry['website'] + values = website.get_rendering_context() + values['partner_ids'] = post.get('partner_ids', "") + values['width'] = post.get('width', 900) + values['height'] = post.get('height', 460) + values['partner_url'] = post.get('partner_url') + return website.render("website_crm_partner_assign.worldmap", values) + + @http.route(['/worldmap/partners.json'], type='http', auth="admin") + def worldmap_data(self, *arg, **post): + website = request.registry['website'] + partner_obj = request.registry['res.partner'] + + domain = [("id", "in", [int(p) for p in post.get('partner_ids', "").split(",") if p])] + partner_ids = partner_obj.search(request.cr, openerp.SUPERUSER_ID, domain + [('website_published', '=', True)]) + if request.uid != website.get_public_user().id: + partner_ids += partner_obj.search(request.cr, request.uid, domain) + partner_ids = list(set(partner_ids)) + + data = { + "counter": len(partner_ids), + "partners": [] + } + for partner in partner_obj.browse(request.cr, openerp.SUPERUSER_ID, partner_ids, context={'show_address': True}): + data["partners"].append({ + 'id': partner.id, + 'name': partner.name, + 'address': '\n'.join(partner.name_get()[0][1].split('\n')[1:]), + 'type': partner.grade_id.name, + 'latitude': partner.partner_latitude, + 'longitude': partner.partner_longitude, + }) + + mime = 'application/json' + body = "var data = " + "}, \n{".join(simplejson.dumps(data).split("}, {")) + return werkzeug.wrappers.Response(body, headers=[('Content-Type', mime), ('Content-Length', len(body))]) + + @http.route(['/worldmap/set_partner_position/'], type='http', auth="admin") + def worldmap_set_partner_position(self, *arg, **post): + website = request.registry['website'] + partner_obj = request.registry['res.partner'] + + partner_id = post.get('partner_id') and int(post['partner_id']) + latitude = post.get('latitude') and float(post['latitude']) + longitude = post.get('longitude') and float(post['longitude']) + + if request.uid != website.get_public_user().id and partner_id and (latitude or longitude): + values = { + 'partner_latitude': latitude, + 'partner_longitude': longitude, + 'date_localization': datetime.now().strftime('%Y-%m-%d'), + } + partner_obj.write(request.cr, request.uid, [partner_id], values) + + +# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/website_crm_partner_assign/static/src/img/partners.png b/addons/website_crm_partner_assign/static/src/img/partners.png new file mode 100644 index 0000000000000000000000000000000000000000..9193bfe12423c950ebb245294414419cd67ffd4e GIT binary patch literal 2013 zcmV<32O{{1P)P000>X1^@s6#OZ}&00001b5ch_0Itp) z=>Px#24YJ`L;(K){{a7>y{D4^000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2ipZ7 z3NJN1kN0f=00&)3L_t(Y$Ay(^a8%_P$N$ecXS2J>Ztht&0X88fkRU-W0TNIw5^B{} z5QK)?bfC1AigpSgz)~GxG{WGN8QW=Xb#N?>qGUj9aex+)jz9-&Ya>;1zie)M$?n-b zyL-;=xjg+4$^qsd+uwm2q`snM<zpnXH{4Yh{cpU&R z^whRzIx2leM{Tur(^tyD1N-(dGX^$qK;nm6!P}p}>{-M4>o+bb+5NMApF0l&N(@VvuV|bywRBgG z)7hZJmBG)W%Ko#ax+c@#_kAW}=sGzUHth4gbA11{+acj2uRtpCP71Xg_?RtgCJ|Yd zWeR17j`-ZGR{9S$ugC7=?_%Df#g8w&e_?BGuFJ-3As7e&%$YzU({lLwm5nS=6_~-y z6?b~^(gWZACfKEAq1P}|x70KH2WznSUl9R~m; zPV%F2>#vmjq{(d z(TT#M0uX?)NEDS7P-IoSN-(LrQD1q4+oez1;7brAj=Lb z9&3~aH?8-IE_b69j{yLfEgH>sXXzx#PBDC6j;Q2J!_-$^!jIXNZz9GO~nq9b~fs z%w$P6EZ)+1U`cP+xww@NMKz$QDa2wjx`va`(sm>@6MdEsnkGT}f3Q7!WAV~rdftexZjMvYi;-7c0GrbV=qW%OgBlya{#G4h zW)2*R51x-cLTxY(O%RAR4K?xj>^8WqW!ZKZ0ALt^&Gx0ls#-Z=j2V!`(f&wyEMt>Y zRTi9D$_1D@^t1w}q2k;10!9v9#@v%9VFx&=X_T}qr~oU81es0;5!nF-m;jrnWivShJ?2LyF$zkD0MUVyZF%VNE#Y7)g}zjZMl6ei03r?t z-Eg@qk0g-A05d}>EK0L!wo4EM^Hy&NV0#{8UPcfra=ydsv4errVfD7v25vMW+vkWGiZFSXp`Uk{jUH(5cBgduwej1 zD31-o0gJV$A9^B;@aQnU^vf6wB@h`4K-XksQZh2hD2%j%EKv{x12(~r$2yu<*K_wZr9Kcwiadv`m4pfy!pD{KZA%|mZ}oD++#Mc_a*~3 v$(Ge$ymQxZcKrei073^}Ldl-JcXaY^xvJNqRiRo{00000NkvXXu0mjf8L+CM literal 0 HcmV?d00001 diff --git a/addons/website_crm_partner_assign/static/src/js/markerclusterer.js b/addons/website_crm_partner_assign/static/src/js/markerclusterer.js new file mode 100644 index 00000000000..1afbe5aad94 --- /dev/null +++ b/addons/website_crm_partner_assign/static/src/js/markerclusterer.js @@ -0,0 +1,1294 @@ +// ==ClosureCompiler== +// @compilation_level ADVANCED_OPTIMIZATIONS +// @externs_url http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/maps/google_maps_api_v3_3.js +// ==/ClosureCompiler== + +/** + * @name MarkerClusterer for Google Maps v3 + * @version version 1.0 + * @author Luke Mahe + * @fileoverview + * The library creates and manages per-zoom-level clusters for large amounts of + * markers. + *
+ * This is a v3 implementation of the + * v2 MarkerClusterer. + */ + +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * A Marker Clusterer that clusters markers. + * + * @param {google.maps.Map} map The Google map to attach to. + * @param {Array.=} opt_markers Optional markers to add to + * the cluster. + * @param {Object=} opt_options support the following options: + * 'gridSize': (number) The grid size of a cluster in pixels. + * 'maxZoom': (number) The maximum zoom level that a marker can be part of a + * cluster. + * 'zoomOnClick': (boolean) Whether the default behaviour of clicking on a + * cluster is to zoom into it. + * 'averageCenter': (boolean) Wether the center of each cluster should be + * the average of all markers in the cluster. + * 'minimumClusterSize': (number) The minimum number of markers to be in a + * cluster before the markers are hidden and a count + * is shown. + * 'styles': (object) An object that has style properties: + * 'url': (string) The image url. + * 'height': (number) The image height. + * 'width': (number) The image width. + * 'anchor': (Array) The anchor position of the label text. + * 'textColor': (string) The text color. + * 'textSize': (number) The text size. + * 'backgroundPosition': (string) The position of the backgound x, y. + * @constructor + * @extends google.maps.OverlayView + */ +function MarkerClusterer(map, opt_markers, opt_options) { + // MarkerClusterer implements google.maps.OverlayView interface. We use the + // extend function to extend MarkerClusterer with google.maps.OverlayView + // because it might not always be available when the code is defined so we + // look for it at the last possible moment. If it doesn't exist now then + // there is no point going ahead :) + this.extend(MarkerClusterer, google.maps.OverlayView); + this.map_ = map; + + /** + * @type {Array.} + * @private + */ + this.markers_ = []; + + /** + * @type {Array.} + */ + this.clusters_ = []; + + this.sizes = [53, 56, 66, 78, 90]; + + /** + * @private + */ + this.styles_ = []; + + /** + * @type {boolean} + * @private + */ + this.ready_ = false; + + var options = opt_options || {}; + + /** + * @type {number} + * @private + */ + this.gridSize_ = options['gridSize'] || 60; + + /** + * @private + */ + this.minClusterSize_ = options['minimumClusterSize'] || 2; + + + /** + * @type {?number} + * @private + */ + this.maxZoom_ = options['maxZoom'] || null; + + this.styles_ = options['styles'] || []; + + /** + * @type {string} + * @private + */ + this.imagePath_ = options['imagePath'] || + this.MARKER_CLUSTER_IMAGE_PATH_; + + /** + * @type {string} + * @private + */ + this.imageExtension_ = options['imageExtension'] || + this.MARKER_CLUSTER_IMAGE_EXTENSION_; + + /** + * @type {boolean} + * @private + */ + this.zoomOnClick_ = true; + + if (options['zoomOnClick'] != undefined) { + this.zoomOnClick_ = options['zoomOnClick']; + } + + /** + * @type {boolean} + * @private + */ + this.averageCenter_ = false; + + if (options['averageCenter'] != undefined) { + this.averageCenter_ = options['averageCenter']; + } + + this.setupStyles_(); + + this.setMap(map); + + /** + * @type {number} + * @private + */ + this.prevZoom_ = this.map_.getZoom(); + + // Add the map event listeners + var that = this; + google.maps.event.addListener(this.map_, 'zoom_changed', function() { + var maxZoom = that.map_.mapTypes[that.map_.getMapTypeId()].maxZoom; + var zoom = that.map_.getZoom(); + if (zoom < 0 || zoom > maxZoom) { + return; + } + + if (that.prevZoom_ != zoom) { + that.prevZoom_ = that.map_.getZoom(); + that.resetViewport(); + } + }); + + google.maps.event.addListener(this.map_, 'idle', function() { + that.redraw(); + }); + + // Finally, add the markers + if (opt_markers && opt_markers.length) { + this.addMarkers(opt_markers, false); + } +} + + +/** + * The marker cluster image path. + * + * @type {string} + * @private + */ +MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ = + 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/' + + 'images/m'; + + +/** + * The marker cluster image path. + * + * @type {string} + * @private + */ +MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_EXTENSION_ = 'png'; + + +/** + * Extends a objects prototype by anothers. + * + * @param {Object} obj1 The object to be extended. + * @param {Object} obj2 The object to extend with. + * @return {Object} The new extended object. + * @ignore + */ +MarkerClusterer.prototype.extend = function(obj1, obj2) { + return (function(object) { + for (var property in object.prototype) { + this.prototype[property] = object.prototype[property]; + } + return this; + }).apply(obj1, [obj2]); +}; + + +/** + * Implementaion of the interface method. + * @ignore + */ +MarkerClusterer.prototype.onAdd = function() { + this.setReady_(true); +}; + +/** + * Implementaion of the interface method. + * @ignore + */ +MarkerClusterer.prototype.draw = function() {}; + +/** + * Sets up the styles object. + * + * @private + */ +MarkerClusterer.prototype.setupStyles_ = function() { + if (this.styles_.length) { + return; + } + + for (var i = 0, size; size = this.sizes[i]; i++) { + this.styles_.push({ + url: this.imagePath_ + (i + 1) + '.' + this.imageExtension_, + height: size, + width: size + }); + } +}; + +/** + * Fit the map to the bounds of the markers in the clusterer. + */ +MarkerClusterer.prototype.fitMapToMarkers = function() { + var markers = this.getMarkers(); + var bounds = new google.maps.LatLngBounds(); + for (var i = 0, marker; marker = markers[i]; i++) { + bounds.extend(marker.getPosition()); + } + + this.map_.fitBounds(bounds); +}; + + +/** + * Sets the styles. + * + * @param {Object} styles The style to set. + */ +MarkerClusterer.prototype.setStyles = function(styles) { + this.styles_ = styles; +}; + + +/** + * Gets the styles. + * + * @return {Object} The styles object. + */ +MarkerClusterer.prototype.getStyles = function() { + return this.styles_; +}; + + +/** + * Whether zoom on click is set. + * + * @return {boolean} True if zoomOnClick_ is set. + */ +MarkerClusterer.prototype.isZoomOnClick = function() { + return this.zoomOnClick_; +}; + +/** + * Whether average center is set. + * + * @return {boolean} True if averageCenter_ is set. + */ +MarkerClusterer.prototype.isAverageCenter = function() { + return this.averageCenter_; +}; + + +/** + * Returns the array of markers in the clusterer. + * + * @return {Array.} The markers. + */ +MarkerClusterer.prototype.getMarkers = function() { + return this.markers_; +}; + + +/** + * Returns the number of markers in the clusterer + * + * @return {Number} The number of markers. + */ +MarkerClusterer.prototype.getTotalMarkers = function() { + return this.markers_.length; +}; + + +/** + * Sets the max zoom for the clusterer. + * + * @param {number} maxZoom The max zoom level. + */ +MarkerClusterer.prototype.setMaxZoom = function(maxZoom) { + this.maxZoom_ = maxZoom; +}; + + +/** + * Gets the max zoom for the clusterer. + * + * @return {number} The max zoom level. + */ +MarkerClusterer.prototype.getMaxZoom = function() { + return this.maxZoom_ || this.map_.mapTypes[this.map_.getMapTypeId()].maxZoom; +}; + + +/** + * The function for calculating the cluster icon image. + * + * @param {Array.} markers The markers in the clusterer. + * @param {number} numStyles The number of styles available. + * @return {Object} A object properties: 'text' (string) and 'index' (number). + * @private + */ +MarkerClusterer.prototype.calculator_ = function(markers, numStyles) { + var index = 0; + var count = markers.length; + var dv = count; + while (dv !== 0) { + dv = parseInt(dv / 10, 10); + index++; + } + + index = Math.min(index, numStyles); + return { + text: count, + index: index + }; +}; + + +/** + * Set the calculator function. + * + * @param {function(Array, number)} calculator The function to set as the + * calculator. The function should return a object properties: + * 'text' (string) and 'index' (number). + * + */ +MarkerClusterer.prototype.setCalculator = function(calculator) { + this.calculator_ = calculator; +}; + + +/** + * Get the calculator function. + * + * @return {function(Array, number)} the calculator function. + */ +MarkerClusterer.prototype.getCalculator = function() { + return this.calculator_; +}; + + +/** + * Add an array of markers to the clusterer. + * + * @param {Array.} markers The markers to add. + * @param {boolean=} opt_nodraw Whether to redraw the clusters. + */ +MarkerClusterer.prototype.addMarkers = function(markers, opt_nodraw) { + for (var i = 0, marker; marker = markers[i]; i++) { + this.pushMarkerTo_(marker); + } + if (!opt_nodraw) { + this.redraw(); + } +}; + + +/** + * Pushes a marker to the clusterer. + * + * @param {google.maps.Marker} marker The marker to add. + * @private + */ +MarkerClusterer.prototype.pushMarkerTo_ = function(marker) { + marker.isAdded = false; + if (marker['draggable']) { + // If the marker is draggable add a listener so we update the clusters on + // the drag end. + var that = this; + google.maps.event.addListener(marker, 'dragend', function() { + marker.isAdded = false; + that.repaint(); + }); + } + this.markers_.push(marker); +}; + + +/** + * Adds a marker to the clusterer and redraws if needed. + * + * @param {google.maps.Marker} marker The marker to add. + * @param {boolean=} opt_nodraw Whether to redraw the clusters. + */ +MarkerClusterer.prototype.addMarker = function(marker, opt_nodraw) { + this.pushMarkerTo_(marker); + if (!opt_nodraw) { + this.redraw(); + } +}; + + +/** + * Removes a marker and returns true if removed, false if not + * + * @param {google.maps.Marker} marker The marker to remove + * @return {boolean} Whether the marker was removed or not + * @private + */ +MarkerClusterer.prototype.removeMarker_ = function(marker) { + var index = -1; + if (this.markers_.indexOf) { + index = this.markers_.indexOf(marker); + } else { + for (var i = 0, m; m = this.markers_[i]; i++) { + if (m == marker) { + index = i; + break; + } + } + } + + if (index == -1) { + // Marker is not in our list of markers. + return false; + } + + marker.setMap(null); + + this.markers_.splice(index, 1); + + return true; +}; + + +/** + * Remove a marker from the cluster. + * + * @param {google.maps.Marker} marker The marker to remove. + * @param {boolean=} opt_nodraw Optional boolean to force no redraw. + * @return {boolean} True if the marker was removed. + */ +MarkerClusterer.prototype.removeMarker = function(marker, opt_nodraw) { + var removed = this.removeMarker_(marker); + + if (!opt_nodraw && removed) { + this.resetViewport(); + this.redraw(); + return true; + } else { + return false; + } +}; + + +/** + * Removes an array of markers from the cluster. + * + * @param {Array.} markers The markers to remove. + * @param {boolean=} opt_nodraw Optional boolean to force no redraw. + */ +MarkerClusterer.prototype.removeMarkers = function(markers, opt_nodraw) { + var removed = false; + + for (var i = 0, marker; marker = markers[i]; i++) { + var r = this.removeMarker_(marker); + removed = removed || r; + } + + if (!opt_nodraw && removed) { + this.resetViewport(); + this.redraw(); + return true; + } +}; + + +/** + * Sets the clusterer's ready state. + * + * @param {boolean} ready The state. + * @private + */ +MarkerClusterer.prototype.setReady_ = function(ready) { + if (!this.ready_) { + this.ready_ = ready; + this.createClusters_(); + } +}; + + +/** + * Returns the number of clusters in the clusterer. + * + * @return {number} The number of clusters. + */ +MarkerClusterer.prototype.getTotalClusters = function() { + return this.clusters_.length; +}; + + +/** + * Returns the google map that the clusterer is associated with. + * + * @return {google.maps.Map} The map. + */ +MarkerClusterer.prototype.getMap = function() { + return this.map_; +}; + + +/** + * Sets the google map that the clusterer is associated with. + * + * @param {google.maps.Map} map The map. + */ +MarkerClusterer.prototype.setMap = function(map) { + this.map_ = map; +}; + + +/** + * Returns the size of the grid. + * + * @return {number} The grid size. + */ +MarkerClusterer.prototype.getGridSize = function() { + return this.gridSize_; +}; + + +/** + * Sets the size of the grid. + * + * @param {number} size The grid size. + */ +MarkerClusterer.prototype.setGridSize = function(size) { + this.gridSize_ = size; +}; + + +/** + * Returns the min cluster size. + * + * @return {number} The grid size. + */ +MarkerClusterer.prototype.getMinClusterSize = function() { + return this.minClusterSize_; +}; + +/** + * Sets the min cluster size. + * + * @param {number} size The grid size. + */ +MarkerClusterer.prototype.setMinClusterSize = function(size) { + this.minClusterSize_ = size; +}; + + +/** + * Extends a bounds object by the grid size. + * + * @param {google.maps.LatLngBounds} bounds The bounds to extend. + * @return {google.maps.LatLngBounds} The extended bounds. + */ +MarkerClusterer.prototype.getExtendedBounds = function(bounds) { + var projection = this.getProjection(); + + // Turn the bounds into latlng. + var tr = new google.maps.LatLng(bounds.getNorthEast().lat(), + bounds.getNorthEast().lng()); + var bl = new google.maps.LatLng(bounds.getSouthWest().lat(), + bounds.getSouthWest().lng()); + + // Convert the points to pixels and the extend out by the grid size. + var trPix = projection.fromLatLngToDivPixel(tr); + trPix.x += this.gridSize_; + trPix.y -= this.gridSize_; + + var blPix = projection.fromLatLngToDivPixel(bl); + blPix.x -= this.gridSize_; + blPix.y += this.gridSize_; + + // Convert the pixel points back to LatLng + var ne = projection.fromDivPixelToLatLng(trPix); + var sw = projection.fromDivPixelToLatLng(blPix); + + // Extend the bounds to contain the new bounds. + bounds.extend(ne); + bounds.extend(sw); + + return bounds; +}; + + +/** + * Determins if a marker is contained in a bounds. + * + * @param {google.maps.Marker} marker The marker to check. + * @param {google.maps.LatLngBounds} bounds The bounds to check against. + * @return {boolean} True if the marker is in the bounds. + * @private + */ +MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) { + return bounds.contains(marker.getPosition()); +}; + + +/** + * Clears all clusters and markers from the clusterer. + */ +MarkerClusterer.prototype.clearMarkers = function() { + this.resetViewport(true); + + // Set the markers a empty array. + this.markers_ = []; +}; + + +/** + * Clears all existing clusters and recreates them. + * @param {boolean} opt_hide To also hide the marker. + */ +MarkerClusterer.prototype.resetViewport = function(opt_hide) { + // Remove all the clusters + for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { + cluster.remove(); + } + + // Reset the markers to not be added and to be invisible. + for (var i = 0, marker; marker = this.markers_[i]; i++) { + marker.isAdded = false; + if (opt_hide) { + marker.setMap(null); + } + } + + this.clusters_ = []; +}; + +/** + * + */ +MarkerClusterer.prototype.repaint = function() { + var oldClusters = this.clusters_.slice(); + this.clusters_.length = 0; + this.resetViewport(); + this.redraw(); + + // Remove the old clusters. + // Do it in a timeout so the other clusters have been drawn first. + window.setTimeout(function() { + for (var i = 0, cluster; cluster = oldClusters[i]; i++) { + cluster.remove(); + } + }, 0); +}; + + +/** + * Redraws the clusters. + */ +MarkerClusterer.prototype.redraw = function() { + this.createClusters_(); +}; + + +/** + * Calculates the distance between two latlng locations in km. + * @see http://www.movable-type.co.uk/scripts/latlong.html + * + * @param {google.maps.LatLng} p1 The first lat lng point. + * @param {google.maps.LatLng} p2 The second lat lng point. + * @return {number} The distance between the two points in km. + * @private +*/ +MarkerClusterer.prototype.distanceBetweenPoints_ = function(p1, p2) { + if (!p1 || !p2) { + return 0; + } + + var R = 6371; // Radius of the Earth in km + var dLat = (p2.lat() - p1.lat()) * Math.PI / 180; + var dLon = (p2.lng() - p1.lng()) * Math.PI / 180; + var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + + Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) * + Math.sin(dLon / 2) * Math.sin(dLon / 2); + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + var d = R * c; + return d; +}; + + +/** + * Add a marker to a cluster, or creates a new cluster. + * + * @param {google.maps.Marker} marker The marker to add. + * @private + */ +MarkerClusterer.prototype.addToClosestCluster_ = function(marker) { + var distance = 40000; // Some large number + var clusterToAddTo = null; + var pos = marker.getPosition(); + for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { + var center = cluster.getCenter(); + if (center) { + var d = this.distanceBetweenPoints_(center, marker.getPosition()); + if (d < distance) { + distance = d; + clusterToAddTo = cluster; + } + } + } + + if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) { + clusterToAddTo.addMarker(marker); + } else { + var cluster = new Cluster(this); + cluster.addMarker(marker); + this.clusters_.push(cluster); + } +}; + + +/** + * Creates the clusters. + * + * @private + */ +MarkerClusterer.prototype.createClusters_ = function() { + if (!this.ready_) { + return; + } + + // Get our current map view bounds. + // Create a new bounds object so we don't affect the map. + var mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(), + this.map_.getBounds().getNorthEast()); + var bounds = this.getExtendedBounds(mapBounds); + + for (var i = 0, marker; marker = this.markers_[i]; i++) { + if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) { + this.addToClosestCluster_(marker); + } + } +}; + + +/** + * A cluster that contains markers. + * + * @param {MarkerClusterer} markerClusterer The markerclusterer that this + * cluster is associated with. + * @constructor + * @ignore + */ +function Cluster(markerClusterer) { + this.markerClusterer_ = markerClusterer; + this.map_ = markerClusterer.getMap(); + this.gridSize_ = markerClusterer.getGridSize(); + this.minClusterSize_ = markerClusterer.getMinClusterSize(); + this.averageCenter_ = markerClusterer.isAverageCenter(); + this.center_ = null; + this.markers_ = []; + this.bounds_ = null; + this.clusterIcon_ = new ClusterIcon(this, markerClusterer.getStyles(), + markerClusterer.getGridSize()); +} + +/** + * Determins if a marker is already added to the cluster. + * + * @param {google.maps.Marker} marker The marker to check. + * @return {boolean} True if the marker is already added. + */ +Cluster.prototype.isMarkerAlreadyAdded = function(marker) { + if (this.markers_.indexOf) { + return this.markers_.indexOf(marker) != -1; + } else { + for (var i = 0, m; m = this.markers_[i]; i++) { + if (m == marker) { + return true; + } + } + } + return false; +}; + + +/** + * Add a marker the cluster. + * + * @param {google.maps.Marker} marker The marker to add. + * @return {boolean} True if the marker was added. + */ +Cluster.prototype.addMarker = function(marker) { + if (this.isMarkerAlreadyAdded(marker)) { + return false; + } + + if (!this.center_) { + this.center_ = marker.getPosition(); + this.calculateBounds_(); + } else { + if (this.averageCenter_) { + var l = this.markers_.length + 1; + var lat = (this.center_.lat() * (l-1) + marker.getPosition().lat()) / l; + var lng = (this.center_.lng() * (l-1) + marker.getPosition().lng()) / l; + this.center_ = new google.maps.LatLng(lat, lng); + this.calculateBounds_(); + } + } + + marker.isAdded = true; + this.markers_.push(marker); + + var len = this.markers_.length; + if (len < this.minClusterSize_ && marker.getMap() != this.map_) { + // Min cluster size not reached so show the marker. + marker.setMap(this.map_); + } + + if (len == this.minClusterSize_) { + // Hide the markers that were showing. + for (var i = 0; i < len; i++) { + this.markers_[i].setMap(null); + } + } + + if (len >= this.minClusterSize_) { + marker.setMap(null); + } + + this.updateIcon(); + return true; +}; + + +/** + * Returns the marker clusterer that the cluster is associated with. + * + * @return {MarkerClusterer} The associated marker clusterer. + */ +Cluster.prototype.getMarkerClusterer = function() { + return this.markerClusterer_; +}; + + +/** + * Returns the bounds of the cluster. + * + * @return {google.maps.LatLngBounds} the cluster bounds. + */ +Cluster.prototype.getBounds = function() { + var bounds = new google.maps.LatLngBounds(this.center_, this.center_); + var markers = this.getMarkers(); + for (var i = 0, marker; marker = markers[i]; i++) { + bounds.extend(marker.getPosition()); + } + return bounds; +}; + + +/** + * Removes the cluster + */ +Cluster.prototype.remove = function() { + this.clusterIcon_.remove(); + this.markers_.length = 0; + delete this.markers_; +}; + + +/** + * Returns the center of the cluster. + * + * @return {number} The cluster center. + */ +Cluster.prototype.getSize = function() { + return this.markers_.length; +}; + + +/** + * Returns the center of the cluster. + * + * @return {Array.} The cluster center. + */ +Cluster.prototype.getMarkers = function() { + return this.markers_; +}; + + +/** + * Returns the center of the cluster. + * + * @return {google.maps.LatLng} The cluster center. + */ +Cluster.prototype.getCenter = function() { + return this.center_; +}; + + +/** + * Calculated the extended bounds of the cluster with the grid. + * + * @private + */ +Cluster.prototype.calculateBounds_ = function() { + var bounds = new google.maps.LatLngBounds(this.center_, this.center_); + this.bounds_ = this.markerClusterer_.getExtendedBounds(bounds); +}; + + +/** + * Determines if a marker lies in the clusters bounds. + * + * @param {google.maps.Marker} marker The marker to check. + * @return {boolean} True if the marker lies in the bounds. + */ +Cluster.prototype.isMarkerInClusterBounds = function(marker) { + return this.bounds_.contains(marker.getPosition()); +}; + + +/** + * Returns the map that the cluster is associated with. + * + * @return {google.maps.Map} The map. + */ +Cluster.prototype.getMap = function() { + return this.map_; +}; + + +/** + * Updates the cluster icon + */ +Cluster.prototype.updateIcon = function() { + var zoom = this.map_.getZoom(); + var mz = this.markerClusterer_.getMaxZoom(); + + if (zoom > mz) { + // The zoom is greater than our max zoom so show all the markers in cluster. + for (var i = 0, marker; marker = this.markers_[i]; i++) { + marker.setMap(this.map_); + } + return; + } + + if (this.markers_.length < this.minClusterSize_) { + // Min cluster size not yet reached. + this.clusterIcon_.hide(); + return; + } + + var numStyles = this.markerClusterer_.getStyles().length; + var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles); + this.clusterIcon_.setCenter(this.center_); + this.clusterIcon_.setSums(sums); + this.clusterIcon_.show(); +}; + + +/** + * A cluster icon + * + * @param {Cluster} cluster The cluster to be associated with. + * @param {Object} styles An object that has style properties: + * 'url': (string) The image url. + * 'height': (number) The image height. + * 'width': (number) The image width. + * 'anchor': (Array) The anchor position of the label text. + * 'textColor': (string) The text color. + * 'textSize': (number) The text size. + * 'backgroundPosition: (string) The background postition x, y. + * @param {number=} opt_padding Optional padding to apply to the cluster icon. + * @constructor + * @extends google.maps.OverlayView + * @ignore + */ +function ClusterIcon(cluster, styles, opt_padding) { + cluster.getMarkerClusterer().extend(ClusterIcon, google.maps.OverlayView); + + this.styles_ = styles; + this.padding_ = opt_padding || 0; + this.cluster_ = cluster; + this.center_ = null; + this.map_ = cluster.getMap(); + this.div_ = null; + this.sums_ = null; + this.visible_ = false; + + this.setMap(this.map_); +} + + +/** + * Triggers the clusterclick event and zoom's if the option is set. + */ +ClusterIcon.prototype.triggerClusterClick = function() { + var markerClusterer = this.cluster_.getMarkerClusterer(); + + // Trigger the clusterclick event. + google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_); + + if (markerClusterer.isZoomOnClick()) { + // Zoom into the cluster. + this.map_.fitBounds(this.cluster_.getBounds()); + } +}; + + +/** + * Adding the cluster icon to the dom. + * @ignore + */ +ClusterIcon.prototype.onAdd = function() { + this.div_ = document.createElement('DIV'); + if (this.visible_) { + var pos = this.getPosFromLatLng_(this.center_); + this.div_.style.cssText = this.createCss(pos); + this.div_.innerHTML = this.sums_.text; + } + + var panes = this.getPanes(); + panes.overlayMouseTarget.appendChild(this.div_); + + var that = this; + google.maps.event.addDomListener(this.div_, 'click', function() { + that.triggerClusterClick(); + }); +}; + + +/** + * Returns the position to place the div dending on the latlng. + * + * @param {google.maps.LatLng} latlng The position in latlng. + * @return {google.maps.Point} The position in pixels. + * @private + */ +ClusterIcon.prototype.getPosFromLatLng_ = function(latlng) { + var pos = this.getProjection().fromLatLngToDivPixel(latlng); + pos.x -= parseInt(this.width_ / 2, 10); + pos.y -= parseInt(this.height_ / 2, 10); + return pos; +}; + + +/** + * Draw the icon. + * @ignore + */ +ClusterIcon.prototype.draw = function() { + if (this.visible_) { + var pos = this.getPosFromLatLng_(this.center_); + this.div_.style.top = pos.y + 'px'; + this.div_.style.left = pos.x + 'px'; + } +}; + + +/** + * Hide the icon. + */ +ClusterIcon.prototype.hide = function() { + if (this.div_) { + this.div_.style.display = 'none'; + } + this.visible_ = false; +}; + + +/** + * Position and show the icon. + */ +ClusterIcon.prototype.show = function() { + if (this.div_) { + var pos = this.getPosFromLatLng_(this.center_); + this.div_.style.cssText = this.createCss(pos); + this.div_.style.display = ''; + } + this.visible_ = true; +}; + + +/** + * Remove the icon from the map + */ +ClusterIcon.prototype.remove = function() { + this.setMap(null); +}; + + +/** + * Implementation of the onRemove interface. + * @ignore + */ +ClusterIcon.prototype.onRemove = function() { + if (this.div_ && this.div_.parentNode) { + this.hide(); + this.div_.parentNode.removeChild(this.div_); + this.div_ = null; + } +}; + + +/** + * Set the sums of the icon. + * + * @param {Object} sums The sums containing: + * 'text': (string) The text to display in the icon. + * 'index': (number) The style index of the icon. + */ +ClusterIcon.prototype.setSums = function(sums) { + this.sums_ = sums; + this.text_ = sums.text; + this.index_ = sums.index; + if (this.div_) { + this.div_.innerHTML = sums.text; + } + + this.useStyle(); +}; + + +/** + * Sets the icon to the the styles. + */ +ClusterIcon.prototype.useStyle = function() { + var index = Math.max(0, this.sums_.index - 1); + index = Math.min(this.styles_.length - 1, index); + var style = this.styles_[index]; + this.url_ = style['url']; + this.height_ = style['height']; + this.width_ = style['width']; + this.textColor_ = style['textColor']; + this.anchor_ = style['anchor']; + this.textSize_ = style['textSize']; + this.backgroundPosition_ = style['backgroundPosition']; +}; + + +/** + * Sets the center of the icon. + * + * @param {google.maps.LatLng} center The latlng to set as the center. + */ +ClusterIcon.prototype.setCenter = function(center) { + this.center_ = center; +}; + + +/** + * Create the css text based on the position of the icon. + * + * @param {google.maps.Point} pos The position. + * @return {string} The css style text. + */ +ClusterIcon.prototype.createCss = function(pos) { + var style = []; + style.push('background-image:url(' + this.url_ + ');'); + var backgroundPosition = this.backgroundPosition_ ? this.backgroundPosition_ : '0 0'; + style.push('background-position:' + backgroundPosition + ';'); + + if (typeof this.anchor_ === 'object') { + if (typeof this.anchor_[0] === 'number' && this.anchor_[0] > 0 && + this.anchor_[0] < this.height_) { + style.push('height:' + (this.height_ - this.anchor_[0]) + + 'px; padding-top:' + this.anchor_[0] + 'px;'); + } else { + style.push('height:' + this.height_ + 'px; line-height:' + this.height_ + + 'px;'); + } + if (typeof this.anchor_[1] === 'number' && this.anchor_[1] > 0 && + this.anchor_[1] < this.width_) { + style.push('width:' + (this.width_ - this.anchor_[1]) + + 'px; padding-left:' + this.anchor_[1] + 'px;'); + } else { + style.push('width:' + this.width_ + 'px; text-align:center;'); + } + } else { + style.push('height:' + this.height_ + 'px; line-height:' + + this.height_ + 'px; width:' + this.width_ + 'px; text-align:center;'); + } + + var txtColor = this.textColor_ ? this.textColor_ : 'black'; + var txtSize = this.textSize_ ? this.textSize_ : 11; + + style.push('cursor:pointer; top:' + pos.y + 'px; left:' + + pos.x + 'px; color:' + txtColor + '; position:absolute; font-size:' + + txtSize + 'px; font-family:Arial,sans-serif; font-weight:bold'); + return style.join(''); +}; + + +// Export Symbols for Closure +// If you are not going to compile with closure then you can remove the +// code below. +window['MarkerClusterer'] = MarkerClusterer; +MarkerClusterer.prototype['addMarker'] = MarkerClusterer.prototype.addMarker; +MarkerClusterer.prototype['addMarkers'] = MarkerClusterer.prototype.addMarkers; +MarkerClusterer.prototype['clearMarkers'] = + MarkerClusterer.prototype.clearMarkers; +MarkerClusterer.prototype['fitMapToMarkers'] = + MarkerClusterer.prototype.fitMapToMarkers; +MarkerClusterer.prototype['getCalculator'] = + MarkerClusterer.prototype.getCalculator; +MarkerClusterer.prototype['getGridSize'] = + MarkerClusterer.prototype.getGridSize; +MarkerClusterer.prototype['getExtendedBounds'] = + MarkerClusterer.prototype.getExtendedBounds; +MarkerClusterer.prototype['getMap'] = MarkerClusterer.prototype.getMap; +MarkerClusterer.prototype['getMarkers'] = MarkerClusterer.prototype.getMarkers; +MarkerClusterer.prototype['getMaxZoom'] = MarkerClusterer.prototype.getMaxZoom; +MarkerClusterer.prototype['getStyles'] = MarkerClusterer.prototype.getStyles; +MarkerClusterer.prototype['getTotalClusters'] = + MarkerClusterer.prototype.getTotalClusters; +MarkerClusterer.prototype['getTotalMarkers'] = + MarkerClusterer.prototype.getTotalMarkers; +MarkerClusterer.prototype['redraw'] = MarkerClusterer.prototype.redraw; +MarkerClusterer.prototype['removeMarker'] = + MarkerClusterer.prototype.removeMarker; +MarkerClusterer.prototype['removeMarkers'] = + MarkerClusterer.prototype.removeMarkers; +MarkerClusterer.prototype['resetViewport'] = + MarkerClusterer.prototype.resetViewport; +MarkerClusterer.prototype['repaint'] = + MarkerClusterer.prototype.repaint; +MarkerClusterer.prototype['setCalculator'] = + MarkerClusterer.prototype.setCalculator; +MarkerClusterer.prototype['setGridSize'] = + MarkerClusterer.prototype.setGridSize; +MarkerClusterer.prototype['setMaxZoom'] = + MarkerClusterer.prototype.setMaxZoom; +MarkerClusterer.prototype['onAdd'] = MarkerClusterer.prototype.onAdd; +MarkerClusterer.prototype['draw'] = MarkerClusterer.prototype.draw; + +Cluster.prototype['getCenter'] = Cluster.prototype.getCenter; +Cluster.prototype['getSize'] = Cluster.prototype.getSize; +Cluster.prototype['getMarkers'] = Cluster.prototype.getMarkers; + +ClusterIcon.prototype['onAdd'] = ClusterIcon.prototype.onAdd; +ClusterIcon.prototype['draw'] = ClusterIcon.prototype.draw; +ClusterIcon.prototype['onRemove'] = ClusterIcon.prototype.onRemove; diff --git a/addons/website_crm_partner_assign/static/src/js/markerclusterer_compiled.js b/addons/website_crm_partner_assign/static/src/js/markerclusterer_compiled.js new file mode 100644 index 00000000000..1e1241fa627 --- /dev/null +++ b/addons/website_crm_partner_assign/static/src/js/markerclusterer_compiled.js @@ -0,0 +1,21 @@ +(function(){var d=null;function e(a){return function(b){this[a]=b}}function i(a){return function(){return this[a]}}var j; +function k(a,b,c){this.extend(k,google.maps.OverlayView);this.b=a;this.a=[];this.f=[];this.ca=[53,56,66,78,90];this.j=[];this.A=!1;c=c||{};this.g=c.gridSize||60;this.l=c.minimumClusterSize||2;this.J=c.maxZoom||d;this.j=c.styles||[];this.X=c.imagePath||this.Q;this.W=c.imageExtension||this.P;this.O=!0;if(c.zoomOnClick!=void 0)this.O=c.zoomOnClick;this.r=!1;if(c.averageCenter!=void 0)this.r=c.averageCenter;l(this);this.setMap(a);this.K=this.b.getZoom();var f=this;google.maps.event.addListener(this.b, +"zoom_changed",function(){var a=f.b.mapTypes[f.b.getMapTypeId()].maxZoom,b=f.b.getZoom();if(!(b<0||b>a)&&f.K!=b)f.K=f.b.getZoom(),f.m()});google.maps.event.addListener(this.b,"idle",function(){f.i()});b&&b.length&&this.C(b,!1)}j=k.prototype;j.Q="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m";j.P="png";j.extend=function(a,b){return function(a){for(var b in a.prototype)this.prototype[b]=a.prototype[b];return this}.apply(a,[b])}; +j.onAdd=function(){if(!this.A)this.A=!0,n(this)};j.draw=function(){};function l(a){if(!a.j.length)for(var b=0,c;c=a.ca[b];b++)a.j.push({url:a.X+(b+1)+"."+a.W,height:c,width:c})}j.S=function(){for(var a=this.o(),b=new google.maps.LatLngBounds,c=0,f;f=a[c];c++)b.extend(f.getPosition());this.b.fitBounds(b)};j.z=i("j");j.o=i("a");j.V=function(){return this.a.length};j.ba=e("J");j.I=function(){return this.J||this.b.mapTypes[this.b.getMapTypeId()].maxZoom}; +j.G=function(a,b){for(var c=0,f=a.length,g=f;g!==0;)g=parseInt(g/10,10),c++;c=Math.min(c,b);return{text:f,index:c}};j.$=e("G");j.H=i("G");j.C=function(a,b){for(var c=0,f;f=a[c];c++)q(this,f);b||this.i()};function q(a,b){b.s=!1;b.draggable&&google.maps.event.addListener(b,"dragend",function(){b.s=!1;a.L()});a.a.push(b)}j.q=function(a,b){q(this,a);b||this.i()}; +function s(a,b){var c=-1;if(a.a.indexOf)c=a.a.indexOf(b);else for(var f=0,g;g=a.a[f];f++)if(g==b){c=f;break}if(c==-1)return!1;a.a.splice(c,1);return!0}j.Y=function(a,b){var c=s(this,a);return!b&&c?(this.m(),this.i(),!0):!1};j.Z=function(a,b){for(var c=!1,f=0,g;g=a[f];f++)g=s(this,g),c=c||g;if(!b&&c)return this.m(),this.i(),!0};j.U=function(){return this.f.length};j.getMap=i("b");j.setMap=e("b");j.w=i("g");j.aa=e("g"); +j.v=function(a){var b=this.getProjection(),c=new google.maps.LatLng(a.getNorthEast().lat(),a.getNorthEast().lng()),f=new google.maps.LatLng(a.getSouthWest().lat(),a.getSouthWest().lng());c=b.fromLatLngToDivPixel(c);c.x+=this.g;c.y-=this.g;f=b.fromLatLngToDivPixel(f);f.x-=this.g;f.y+=this.g;c=b.fromDivPixelToLatLng(c);b=b.fromDivPixelToLatLng(f);a.extend(c);a.extend(b);return a};j.R=function(){this.m(!0);this.a=[]}; +j.m=function(a){for(var b=0,c;c=this.f[b];b++)c.remove();for(b=0;c=this.a[b];b++)c.s=!1,a&&c.setMap(d);this.f=[]};j.L=function(){var a=this.f.slice();this.f.length=0;this.m();this.i();window.setTimeout(function(){for(var b=0,c;c=a[b];b++)c.remove()},0)};j.i=function(){n(this)}; +function n(a){if(a.A)for(var b=a.v(new google.maps.LatLngBounds(a.b.getBounds().getSouthWest(),a.b.getBounds().getNorthEast())),c=0,f;f=a.a[c];c++)if(!f.s&&b.contains(f.getPosition())){for(var g=a,r=4E4,o=d,v=0,m=void 0;m=g.f[v];v++){var h=m.getCenter();if(h){var p=f.getPosition();if(!h||!p)h=0;else{var w=(p.lat()-h.lat())*Math.PI/180,x=(p.lng()-h.lng())*Math.PI/180;h=Math.sin(w/2)*Math.sin(w/2)+Math.cos(h.lat()*Math.PI/180)*Math.cos(p.lat()*Math.PI/180)*Math.sin(x/2)*Math.sin(x/2);h=6371*2*Math.atan2(Math.sqrt(h), +Math.sqrt(1-h))}h=this.l&&a.setMap(d); +if(this.b.getZoom()>this.k.I())for(a=0;b=this.a[a];a++)b.setMap(this.b);else if(this.a.length0&&a.e[0]0&&a.e[1]'+ + (partner_url.length ? ''+p.name +'' : ''+p.name+'' )+ '
'+ + (p.type ? ' ' + p.type + '' : '')+ + '
' + p.address + '
'+ + '' + ); + infoWindow.open(map, marker); + }; + + var set_marker = function(partner) { + if (!partner.latitude && !partner.longitude) { + + Geocoder.geocode( { 'address': partner.address}, function(results, status) { + if (status == google.maps.GeocoderStatus.OK) { + var location = results[0].geometry.location; + + $.post("/worldmap/set_partner_position/", { + 'partner_id': partner.id, + 'latitude': location.ob, + 'longitude': location.pb + }); + partner.latitude = location.ob; + partner.longitude = location.pb; + + map.setCenter(results[0].geometry.location); + var marker = new google.maps.Marker({ + partner: partner, + map: map, + icon: partners, + position: location + }); + google.maps.event.addListener(marker, 'click', onMarkerClick); + markers.push(marker); + } else { + console.debug('Geocode was not successful for the following reason: ' + status); + } + }); + + } else { + + var latLng = new google.maps.LatLng(partner.latitude, partner.longitude); + var marker = new google.maps.Marker({ + partner: partner, + icon: partners, + position: latLng + }); + google.maps.event.addListener(marker, 'click', onMarkerClick); + markers.push(marker); + } + + }; + + if (data) + for (var i = 0; i < data.counter; i++) { + set_marker(data.partners[i]); + } + var markerCluster = new MarkerClusterer(map, markers); +} +google.maps.event.addDomListener(window, 'load', initialize); + diff --git a/addons/website_crm_partner_assign/views/worldmap.xml b/addons/website_crm_partner_assign/views/worldmap.xml new file mode 100644 index 00000000000..9e024982780 --- /dev/null +++ b/addons/website_crm_partner_assign/views/worldmap.xml @@ -0,0 +1,42 @@ + + + + + + \ No newline at end of file diff --git a/addons/website_crm_partner_assign/website_crm_partner_assign.py b/addons/website_crm_partner_assign/website_crm_partner_assign.py new file mode 100644 index 00000000000..82d5390ec71 --- /dev/null +++ b/addons/website_crm_partner_assign/website_crm_partner_assign.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +from openerp.osv import osv, fields + +class res_partner(osv.osv): + _inherit = 'res.partner' + _columns = { + 'website_published': fields.boolean('Available in the website'), + }