javascript - Implementing Google Maps Spiderfier -
javascript - Implementing Google Maps Spiderfier -
i'm trying implement https://github.com/jawj/overlappingmarkerspiderfier handle multiple map markers can occur in exact same location. i'm using below javascript grab map info divs of class .marker , create map. it's functioning , works great.
function initialize() { function render_map( $el ) { // var var $markers = $el.find('.marker'); // vars var args = { zoom : 8, center : new google.maps.latlng(42.7086815, -84.559032), scrollwheel : false, maptypeid : google.maps.maptypeid.roadmap }; // create map var map = new google.maps.map( $el[0], args); // add together markers reference map.markers = []; // add together markers $markers.each(function(){ add_marker( $(this), map ); }); var padder = document.createelement('div'); padder.style.height = '100px'; padder.style.width = '100%'; map.controls[google.maps.controlposition.top_center].push(padder); // center map center_map( map ); } function add_marker( $marker, map ) { // var var latlng = new google.maps.latlng( $marker.attr('data-lat'), $marker.attr('data-lng') ); // create marker var marker = new google.maps.marker({ position : latlng, map : map }); // add together array map.markers.push( marker ); // if marker contains html, add together infowindow if( $marker.html() ) { // create info window var infowindow = new google.maps.infowindow({ content : $marker.html() }); // show info window when marker clicked google.maps.event.addlistener(marker, 'click', function() { infowindow.open( map, marker ); map.setcenter(marker.getposition()); }); } } function center_map( map ) { // vars var bounds = new google.maps.latlngbounds(); // loop through markers , create bounds $.each( map.markers, function( i, marker ){ var latlng = new google.maps.latlng( marker.position.lat(), marker.position.lng() ); bounds.extend( latlng ); }); // 1 marker? if( map.markers.length == 1 ) { // set center of map map.setcenter( bounds.getcenter() ); } else if( map.markers.length == 0 ) { // set center of map map.setcenter( 42.7086815, -84.559032 ); } else { // fit bounds map.fitbounds( bounds ); } google.maps.event.addlisteneronce(map, 'bounds_changed', function(event) { if (this.getzoom() > 8) { this.setzoom(8); } }); } $(document).ready(function(){ $('#acf-map').each(function(){ render_map( $(this) ); }); }); }
however, when seek integrate have spiderfier js, map markers not showing up. sorry effort @ merging js. thought of how can utilize spiderfier still grab content .marker divs?
function initialize() { function render_map( $el ) { // var var $markers = $el.find('.marker'); // vars var args = { zoom : 8, center : new google.maps.latlng(42.7086815, -84.559032), scrollwheel : false, maptypeid : google.maps.maptypeid.roadmap }; // create map var map = new google.maps.map( $el[0], args); var oms = new overlappingmarkerspiderfier(map); var iw = new google.maps.infowindow(); oms.addlistener('click', function(marker, event) { iw.setcontent(marker.desc); iw.open(map, marker); }); oms.addlistener('spiderfy', function(markers) { iw.close(); }); // var var latlng = new google.maps.latlng( $marker.attr('data-lat'), $marker.attr('data-lng') ); // create marker var marker = new google.maps.marker({ position : latlng, map : map }); marker.desc = $marker.html(); oms.addmarker(marker); var padder = document.createelement('div'); padder.style.height = '100px'; padder.style.width = '100%'; map.controls[google.maps.controlposition.top_center].push(padder); // center map center_map( map ); } function center_map( map ) { // vars var bounds = new google.maps.latlngbounds(); // loop through markers , create bounds $.each( map.markers, function( i, marker ){ var latlng = new google.maps.latlng( marker.position.lat(), marker.position.lng() ); bounds.extend( latlng ); }); // 1 marker? if( map.markers.length == 1 ) { // set center of map map.setcenter( bounds.getcenter() ); } else if( map.markers.length == 0 ) { // set center of map map.setcenter( 42.7086815, -84.559032 ); } else { // fit bounds map.fitbounds( bounds ); } google.maps.event.addlisteneronce(map, 'bounds_changed', function(event) { if (this.getzoom() > 8) { this.setzoom(8); } }); } $(document).ready(function(){ $('#acf-map').each(function(){ render_map( $(this) ); }); }); }
below correctly integrated javascript in case it's helpful else.
function initialize() { /* * render_map * * function render google map onto selected jquery element * * @type function * @date 8/11/2013 * @since 4.3.0 * * @param $el (jquery element) * @return n/a */ function render_map( $el ) { // var var $markers = $el.find('.marker'); // vars var args = { zoom : 8, center : new google.maps.latlng(42.7086815, -84.559032), scrollwheel : false, maptypeid : google.maps.maptypeid.roadmap }; // create map var map = new google.maps.map( $el[0], args); var oms = new overlappingmarkerspiderfier(map); var iw = new google.maps.infowindow(); oms.addlistener('click', function(marker, event) { iw.setcontent(marker.desc); iw.open(map, marker); }); oms.addlistener('spiderfy', function(markers) { iw.close(); }); $markers.each(function(index, item) { // create latlng var latlng = new google.maps.latlng( $(item).attr('data-lat'), $(item).attr('data-lng') ); // create marker var iconbase = '/images/'; var marker = new google.maps.marker({ position : latlng, map : map, icon: iconbase + 'marker.png' }); marker.desc = $(item).html(); oms.addmarker(marker); }); var padder = document.createelement('div'); padder.style.height = '100px'; padder.style.width = '100%'; map.controls[google.maps.controlposition.top_center].push(padder); // center map center_map(map); } /* * center_map * * function center map, showing markers attached map * * @type function * @date 8/11/2013 * @since 4.3.0 * * @param map (google map object) * @return n/a */ function center_map(map) { // vars var bounds = new google.maps.latlngbounds(); // loop through markers , create bounds $.each( map.markers, function( i, marker ){ var latlng = new google.maps.latlng( marker.position.lat(), marker.position.lng() ); bounds.extend( latlng ); }); // 1 marker? if( map.markers.length == 1 ) { // set center of map map.setcenter( bounds.getcenter() ); } else if( map.markers.length == 0 ) { // set center of map map.setcenter( 42.7086815, -84.559032 ); } else { // fit bounds map.fitbounds( bounds ); } google.maps.event.addlisteneronce(map, 'bounds_changed', function(event) { if (this.getzoom() > 8) { this.setzoom(8); } }); } /* * document ready * * function render each map when document ready (page has loaded) * * @type function * @date 8/11/2013 * @since 5.0.0 * * @param n/a * @return n/a */ $(document).ready(function(){ $('#acf-map').each(function(){ render_map( $(this) ); }); }); }
javascript google-maps google-maps-api-3 markerspiderfier
Comments
Post a Comment