【发布时间】:2015-10-07 08:20:52
【问题描述】:
我知道有很多关于这个主题的问题得到了解答,但我无法让它们与我的代码一起工作。我使用了来自 this question here 的代码,但我的 javascript 知识有限,我无法弄清楚将它放在哪里才能让它工作。我正在使用从 wp 查询动态添加的多个位置,并且我希望地图能够自动找到所有位置的中心,并最好缩放到所有标记的范围。
这是我生成地图的代码:
var map = new google.maps.Map(document.getElementById('map-result'), {
zoom: 6,
center: new google.maps.LatLng(52.999085, -2.833751),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var locations = [
<?php
$post_query = new WP_Query(array('posts_per_page' => 999, 'post_type' => 'surf-school', 'category_name' => $cat_name));
if(!empty($post_query->posts)){
foreach($post_query->posts as $post) {
$location_lat = get_post_meta($post->ID,'wp_gp_latitude',true);
$location_long = get_post_meta($post->ID,'wp_gp_longitude',true);
$the_post_type = get_post_type( get_the_ID() );
?>
['<a href="<?php the_permalink();?>"><?php the_title(); ?></a>', <?php echo $location_lat; ?>, <?php echo $location_long; ?>, '<?php echo home_url().'/wp-content/themes/blutek/images/beach-marker.png'; ?>'],
<?php } }?>
];
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
icon: locations[i][3],
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
这是来自the other answer 的代码,它应该使地图居中:
var bound = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
bound.extend( new google.maps.LatLng(locations[i][2], locations[i][3]) );
// OTHER CODE
}
console.log( bound.getCenter()
);
谁能告诉我该代码应该放在我的代码中的什么位置?
【问题讨论】:
标签: javascript google-maps google-maps-api-3