2016-05-19 3 views
0

Я относительно новичок в jQuery и работаю с API в целом. Я пытаюсь сделать oms Перекрытие маркера Spiderifier работает с Google Maps v3. Я использую это, чтобы решить проблему наличия нескольких указателей в одном и том же месте.Google Maps + Overlapping Marker Spiderifier

Существующий код создает экземпляр карты и добавляет к нему маркеры. Кажется, что все работает нормально, без ошибок, но маркеры просто не разглядывают. Понятия не имею почему. Любая помощь, указывающая на то, что я делаю неправильно, будет высоко оценена.

К тому же этот код восходит к 2013 году. Есть ли лучший, более удобный способ подойти к проблеме, упомянутой выше?

Вот мой jsfiddle: https://jsfiddle.net/LaurenceLee/7p3mn92m/

(function($) { 

/* 
* render_map 
* 
* This function will render a Google Map onto the selected jQuery element 
* 
* @type function 
* @date 8/11/2013 
* @since 4.3.0 
* 
* @param $el (jQuery element) 
* @return n/a 
*/ 

function new_map($el) { 

// find markers 
var $markers = $('.marker'); 

// vars 
var args = { 
    zoom  : 4, 
    center  : new google.maps.LatLng(37.096, -113.568), 
    scrollwheel : false, 
    mapTypeId : google.maps.MapTypeId.ROADMAP, 
     mapTypeControl: true, 
      mapTypeControlOptions: { 
       style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
      }, 
      navigationControl: true, 
      scrollwheel: true, 
    streetViewControl: true 
}; 

// create map    
var map = new google.maps.Map($el[0], args); 

// init Overlapping Marker Spiderfier and global listener (oms) 
    var oms = new OverlappingMarkerSpiderfier(map, {markersWontMove: true, markersWontHide: true, keepSpiderfied: true, nearbyDistance: 10, legWeight: 5}); 
var iw = new google.maps.InfoWindow(); 
oms.addListener('click', function(marker, event) { 
    iw.setContent(marker.desc); 
    iw.setLatLng(marker.getLatLng()); 
    iw.open(map, marker); 
}); 
oms.addListener('spiderfy', function(marker, event) { 
    iw.close(map, marker); 
}); 

// add a markers reference 
map.markers = []; 

// add markers 
$markers.each(function(){ 
    add_marker($(this), map); 
    // push to oms 
    oms.addMarker($(this)); 
}); 

// center map 
center_map(map); 
// return 
return map; 

} 

/* 
* add_marker 
* 
* This function will add a marker to the selected Google Map 
* 
* @type  function 
* @date  8/11/2013 
* @since  4.3.0 
* 
* @param  $marker (jQuery element) 
* @param  map (Google Map object) 
* @return  n/a 
*/ 

function add_marker($marker, map) { 

// var 
var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng')); 
var icon = $marker.attr('data-icon'); 

// create marker 
var marker = new google.maps.Marker({ 
    position : latlng, 
    map   : map, 
    icon  : icon 
}); 

// add to array 
map.markers.push(marker); 

// if marker contains HTML, add it to an infoWindow 
if($marker.html()) 
{ 
    // create info window 
    var infowindow = new google.maps.InfoWindow({ 
     content  : $marker.html(), 
    }); 
    // show info window when marker is clicked 
    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.open(map, marker); 
    }); 
} 

} 

/* 
* center_map 
* 
* This function will center the map, showing all markers attached to this 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 all markers and create bounds 
$.each(map.markers, function(i, marker){ 
    var latlng = new google.maps.LatLng(marker.position.lat(), marker.position.lng()); 
    bounds.extend(latlng); 
}); 
// only 1 marker? 
if(map.markers.length == 1) 
{ 
    // set center of map 
    map.setCenter(bounds.getCenter()); 
} 
// no markers? 
else if(map.markers.length == 0) 
{ 
    // set center of map 
    map.setCenter(37.096, -113.568); 
} 
else 
{ 
    // fit to bounds 
    map.fitBounds(bounds); 
} 

} 

/* 
* document ready 
* 
* This function will render each map when the document is ready (page has loaded) 
* 
* @type function 
* @date 8/11/2013 
* @since 5.0.0 
* 
* @param n/a 
* @return n/a 
*/ 

// global var 
var map = null; 
$(document).ready(function(){ 

    $('#map_container').each(function(){ 
     //render_map($(this)); 
     map = new_map($(this)); 
    }); 

}); 

})(jQuery); 
+0

Решено с этим https://jsfiddle.net/LaurenceLee/0yo0h20j/5/ –

ответ

0

перекрывающийся Маркер Spiderfier не знает, что делать с $ (это) (который является JQuery вещи). Вы должны передать ему объект google.maps.Marker.

Один из вариантов - вернуть маркер из функции add_marker.

function add_marker($marker, map) { 

    // var 
    var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng')); 
    var icon = $marker.attr('data-icon'); 

    // create marker 
    var marker = new google.maps.Marker({ 
     position : latlng, 
     map   : map, 
     icon  : icon 
    }); 

    // add to array 
    map.markers.push(marker); 

    // if marker contains HTML, add it to an infoWindow 
    if($marker.html()) 
    { 
     // create info window 
     var infowindow = new google.maps.InfoWindow({ 
      content  : $marker.html(), 
     }); 
     // show info window when marker is clicked 
     google.maps.event.addListener(marker, 'click', function() { 
      infowindow.open(map, marker); 
     }); 
    } 
    return marker; 
} 

Затем передать это значение в spiderfier:

// add markers 
$markers.each(function(){ 

    // push to oms 
    oms.addMarker(add_marker($(this), map)); 
}); 

proof of concept fiddle

фрагмент кода:

(function($) { 
 

 
    /* 
 
    * render_map 
 
    * 
 
    * This function will render a Google Map onto the selected jQuery element 
 
    * 
 
    * @type function 
 
    * @date 8/11/2013 
 
    * @since 4.3.0 
 
    * 
 
    * @param $el (jQuery element) 
 
    * @return n/a 
 
    */ 
 

 
    function new_map($el) { 
 

 
    // find markers 
 
    var $markers = $('.marker'); 
 

 
    // vars 
 
    var args = { 
 
     zoom: 4, 
 
     center: new google.maps.LatLng(37.096, -113.568), 
 
     scrollwheel: false, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
 
     mapTypeControl: true, 
 
     mapTypeControlOptions: { 
 
     style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
 
     }, 
 
     navigationControl: true, 
 
     scrollwheel: true, 
 
     streetViewControl: true 
 
    }; 
 

 
    // create map    
 
    var map = new google.maps.Map($el[0], args); 
 

 
    // init Overlapping Marker Spiderfier and global listener (oms) 
 
    var oms = new OverlappingMarkerSpiderfier(map, { 
 
     markersWontMove: true, 
 
     markersWontHide: true, 
 
     keepSpiderfied: true, 
 
     nearbyDistance: 10, 
 
     legWeight: 5 
 
    }); 
 
    var iw = new google.maps.InfoWindow(); 
 
    oms.addListener('click', function(marker, event) { 
 
     iw.setContent(marker.desc); 
 
     iw.setLatLng(marker.getLatLng()); 
 
     iw.open(map, marker); 
 
    }); 
 
    oms.addListener('spiderfy', function(marker, event) { 
 
     iw.close(map, marker); 
 
    }); 
 

 
    // add a markers reference 
 
    map.markers = []; 
 

 
    // add markers 
 
    $markers.each(function() { 
 

 
     // push to oms 
 
     oms.addMarker(add_marker($(this), map)); 
 
    }); 
 

 
    // center map 
 
    center_map(map); 
 
    // return 
 
    return map; 
 

 
    } 
 

 
    /* 
 
    * add_marker 
 
    * 
 
    * This function will add a marker to the selected Google Map 
 
    * 
 
    * @type  function 
 
    * @date  8/11/2013 
 
    * @since  4.3.0 
 
    * 
 
    * @param  $marker (jQuery element) 
 
    * @param  map (Google Map object) 
 
    * @return  n/a 
 
    */ 
 

 
    function add_marker($marker, map) { 
 

 
    // var 
 
    var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng')); 
 
    var icon = $marker.attr('data-icon'); 
 

 
    // create marker 
 
    var marker = new google.maps.Marker({ 
 
     position: latlng, 
 
     map: map, 
 
     icon: icon 
 
    }); 
 

 
    // add to array 
 
    map.markers.push(marker); 
 

 
    // if marker contains HTML, add it to an infoWindow 
 
    if ($marker.html()) { 
 
     // create info window 
 
     var infowindow = new google.maps.InfoWindow({ 
 
     content: $marker.html(), 
 
     }); 
 
     // show info window when marker is clicked 
 
     google.maps.event.addListener(marker, 'click', function() { 
 
     infowindow.open(map, marker); 
 
     }); 
 
    } 
 
    return marker; 
 
    } 
 

 
    /* 
 
    * center_map 
 
    * 
 
    * This function will center the map, showing all markers attached to this 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 all markers and create bounds 
 
    $.each(map.markers, function(i, marker) { 
 
     var latlng = new google.maps.LatLng(marker.position.lat(), marker.position.lng()); 
 
     bounds.extend(latlng); 
 
    }); 
 
    // only 1 marker? 
 
    if (map.markers.length == 1) { 
 
     // set center of map 
 
     map.setCenter(bounds.getCenter()); 
 
    } 
 
    // no markers? 
 
    else if (map.markers.length == 0) { 
 
     // set center of map 
 
     map.setCenter(37.096, -113.568); 
 
    } else { 
 
     // fit to bounds 
 
     map.fitBounds(bounds); 
 
    } 
 

 
    } 
 

 
    /* 
 
    * document ready 
 
    * 
 
    * This function will render each map when the document is ready (page has loaded) 
 
    * 
 
    * @type function 
 
    * @date 8/11/2013 
 
    * @since 5.0.0 
 
    * 
 
    * @param n/a 
 
    * @return n/a 
 
    */ 
 

 
    // global var 
 
    var map = null; 
 
    $(document).ready(function() { 
 

 
    $('#map_container').each(function() { 
 
     //render_map($(this)); 
 
     map = new_map($(this)); 
 
    }); 
 

 
    }); 
 

 
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<script src="https://eventida.com/assets/plugins/oms.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div id="map_container" class="gmap" style="height:250px; margin-bottom: 30px;"> 
 
</div> 
 

 
<div class="marker" data-lat="37.096" data-lng="-113.568" data-icon="https://eventida.com/images/e2_map_marker.png"> 
 
    Location One 
 
</div> 
 

 
<div class="marker" data-lat="37.096" data-lng="-113.568" data-icon="https://eventida.com/images/e2_map_marker.png"> 
 
    Location Two 
 
</div> 
 

 
<div class="marker" data-lat="37.096" data-lng="-113.568" data-icon="https://eventida.com/images/e2_map_marker.png"> 
 
    Location Three 
 
</div>

+0

Удивительный ответ, спасибо! С некоторой помощью и дальнейшим воплем, окончательное решение в этой скрипке. Он позволяет использовать infowindows и устанавливает более высокий уровень масштабирования, если имеется только один маркер. https://jsfiddle.net/LaurenceLee/779v0459/ –