How to know zoom level to display a marker inside google marker cluster -


well, in title :

  • i have thousands of markers google marker cluster (not google marker cluster plus : let me know if help)
  • everything works perfectly

but

  • when fire event relative particular marker, i'd display marker alone (not within cluster anymore).
  • since spacial repartition of markers not homogeneous : @ place zoom level of 9 display alone @ place, i'll have set zoom level 15.

so

  • the ultimate question : giving particular maker, how "query" marker cluster know :
    1. perhaps distance closest maker (giving should able calculate corresponding zoom level)
    2. any other helpful information should still miss @ time ...

some code ;)

/**  * computes scale in meters per pixel given zoom , latitute.  *  * @param {object} opt optional parameters  *      - zoom  *      - lat  *      - precision  *  * @returns {number} scale in meters per pixel  */ google.maps.map.prototype.getmapscale = function (opt){     var circumference = 40075040,         zoom, lat, scale;      if (typeof(opt['zoom']) == 'number' && typeof(opt['lat']) == 'number') {         zoom = opt['zoom'];         lat = opt['lat'];     } else {         zoom = this.getzoom();         lat = this.getcenter().lat();     }      scale = (circumference * math.cos(lat) / math.pow(2, zoom + 8));      if (typeof(opt['precision']) == 'number') {         scale = number(scale.tofixed(opt['precision']));     }      return scale; }   function calculatezoomlevelmarkeraloneincluster(vigneronid){     var distance = 1000000000;     var found_marker_in_cluster = false;     /* clusters gmap marker cluster */     var clustersarray = mc.getclusters();     /* clusters */     $.each(clustersarray, function(key, cluster){         var markersclusterarray = cluster.getmarkers();         /* markers in cluster */         $.each(markersclusterarray, function(key, marker){             /* if find marker */             if(marker.vigneronid === vigneronid){                 /* marker in cluster : no need process markers */                 found_marker_in_cluster = true;                 /* again : markers in cluster */                 var distance_tmp = 0;                 /* markers in cluster */                 $.each(markersclusterarray, function(key, marker2){                     /* markers except mine (otherwise distance 0 :-) ) */                     if(marker.vigneronid !== marker2.vigneronid){                         /* calculate distance between marker : http://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3 */                         distance_tmp = google.maps.geometry.spherical.computedistancebetween(marker.getposition(), marker2.getposition());                         /* keep min distance */                         if(distance_tmp < distance && distance_tmp!==0){                             distance = distance_tmp;                         }                     }                 });                 /* no reason continue process : break */                 return false;             }         });     });      if(found_marker_in_cluster === false){         var markersclusterarray = mc.getmarkers()         $.each(markersclusterarray, function(key, marker){             if(marker.vigneronid === vigneronid){                 var distance_tmp = 0;                 /* markers in cluster */                 $.each(markersclusterarray, function(key, marker2){                     /* markers except mine (otherwise distance 0 :-) ) */                     if(marker.vigneronid !== marker2.vigneronid){                         /* calculate distance between marker : http://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3 */                         distance_tmp = google.maps.geometry.spherical.computedistancebetween(marker.getposition(), marker2.getposition());                         /* keep min distance */                         if(distance_tmp < distance && distance_tmp!==0){                             distance = distance_tmp;                         }                     }                 });             }         });     }      var mapscale = map.getmapscale({}); /* meters / pixels */     var gridsizepixels = mc.getgridsize(); /* in pixels */     var gridsizemeters = gridsizepixels * mapscale;      if(distance!==1000000000 && distance!==0){         if(distance < gridsizemeters){             var factor = get2factor(gridsizemeters, distance);             map.setzoom(map.getzoom() + factor);         } else{             var factor = get2factor(distance, gridsizemeters);             map.setzoom(map.getzoom() - factor + 1);         }      }     /*alert(distance);*/ }  function get2factor(grid, distance){     var count = 0;     while(distance < grid){         count++;         grid = grid / 2;             }     return count; } 

some explanations :

firstly code highly commeted read it

secondly :

  • the first function getmapscale has been found on internet

  • before use functions need set marker identifier (here : marker.vigneronid) before doing push in marker cluster

  • i fisrt iteration in cluster find marker , distance closest marker (in meter)

  • if don't find marker, means it's not in cluster second iteration on markers managed marker cluster , distance closest marker (in meter)

  • then calculate gridsize in meter (thanks function getmapscale transforms pixels in meters)

  • knowing each zoom level factor 2 previous zoom level, compute distance between closest marker , cluster grid size know how many zooms levels can add or substract current zoom level

yeah !


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -