我在一个leaflet.js站点上运行WordPress地图,该站点正在从自定义邮政类型(Metabox.io)中提取位置。我有所有的工作,除了我只有10个标记在地图上。目前,欧洲防止酷刑委员会有24个地点。我在这里发现了关于一个地图上可以有多少标记的问题,而且我肯定不会接近我在堆栈溢出的答案中看到的限制。
,这是我在函数中使用的代码:
add_action( 'wp_enqueue_scripts', function() {
if ( is_page( 641 ) ) {
wp_enqueue_style( 'leaflet', 'https://unpkg.com/leaflet@1.5.1/dist/leaflet.css', [], '1.5.1' );
wp_enqueue_script( 'leaflet', 'https://unpkg.com/leaflet@1.5.1/dist/leaflet.js', [], '1.5.1', true );
wp_enqueue_script( 'collab_list', get_theme_file_uri( 'collaborative.js' ), ['jquery', 'leaflet'], '1.0.14', true );
}
});
function bww_collaborative_maps() {
$locations = [];
$query = new WP_Query( [
'post_type' => 'collaborative',
] );
foreach ( $query->posts as $post ) {
$location = rwmb_get_value( 'location', '', $post->ID );
$location['title'] = $post->post_title;
$location['address'] = rwmb_get_value( 'collaborative_city_and_state', '', $post->ID );
$location['icon'] = rwmb_get_value( 'collaborative_map_icon', '', $post->ID );
$location['url'] = $post->post_name;
$locations[] = $location;
}
wp_localize_script( 'collab_list', 'Locations', $locations );
}
add_shortcode('collaborative_map','bww_collaborative_maps');,下面是我的.js文件中的代码:
( function( document, Locations, L ) {
// Set map center = first restaurant location.
var center = [39.043135, -98.148637];
// Map options.
var options = {
center: center,
zoom: 4
};
// Initialize the map.
var map = L.map( document.querySelector( '#collab_map' ), options );
// Set tile layer for Open Street Map.
var tileLayer = L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
} );
map.addLayer( tileLayer );
// Show marker for each location.
Locations.forEach( function( location ) {
// Marker options.
var options = {
title: location.title,
icon: L.icon( {
iconUrl: location.icon,
iconSize: [30, 50]
} )
};
var center = L.latLng( location.latitude, location.longitude );
var marker = L.marker( center, options ).addTo( map );
// Show name of the restaurant when click on the icon.
marker.bindPopup( '<span style="font-size:.9rem;"><b>' + location.title + '</b></span><br>' + '<br/><b><a style="font-size:.7rem;" href="' + location.url + '" target="_blank">More Information</a></b>' ).openPopup();
} );
} )( document, Locations, L );以下是活动页面:https://ecf.mywp.dev/collaborative-map-test/
我将非常感谢任何想法,如何使这张地图显示所有的标志,而不是仅仅10个。谢谢!
https://stackoverflow.com/questions/67328353
复制相似问题