Javascript 谷歌地图API中心和缩放多个标记
我知道在这个问题上有很多问题需要回答,但我无法让他们使用我的代码。我使用了来自的代码,但我的javascript知识有限,我无法确定将代码放在哪里才能使其工作。我正在使用从wp查询动态添加的多个位置,我希望地图自动找到所有位置的中心,并最好缩放到所有标记的范围 这是我生成地图的代码:Javascript 谷歌地图API中心和缩放多个标记,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我知道在这个问题上有很多问题需要回答,但我无法让他们使用我的代码。我使用了来自的代码,但我的javascript知识有限,我无法确定将代码放在哪里才能使其工作。我正在使用从wp查询动态添加的多个位置,我希望地图自动找到所有位置的中心,并最好缩放到所有标记的范围 这是我生成地图的代码: var map = new google.maps.Map(document.getElementById('map-result'), { zoom: 6,
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));
}
var-map=new google.maps.map(document.getElementById('map-result'){
缩放:6,
中心:新google.maps.LatLng(52.999085,-2.833751),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
变量位置=[
['', , ''],
];
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
这是地图居中的代码:
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()
);
var-bound=new google.maps.LatLngBounds();
对于(i=0;i
有人能告诉我代码应该放在哪里吗?使用函数:
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
bounds.extend(new google.maps.LatLng(locations[i][1], locations[i][2]));
//the remaining code for initializing markers goes here...
}
map.fitBounds(bounds); //Sets the viewport to contain the given bounds
var-bounds=new google.maps.LatLngBounds();
对于(i=0;i
示例
函数初始化(){
变量位置=[
[Bondi Beach',-33.890542151.274856,4],
[Coogee Beach',-33.923036151.259052,5],
[Cronulla Beach',-34.028249151.157507,3],
[‘曼利海滩’,-33.80010128657071151.28747820854187,2],
[‘马鲁布拉海滩’,-33.950198151.259302,1]
];
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新谷歌地图LatLng(-33.92151.25),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
var bounds=new google.maps.LatLngBounds();
对于(i=0;i
使用功能:
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
bounds.extend(new google.maps.LatLng(locations[i][1], locations[i][2]));
//the remaining code for initializing markers goes here...
}
map.fitBounds(bounds); //Sets the viewport to contain the given bounds
var-bounds=new google.maps.LatLngBounds();
对于(i=0;i
示例
函数初始化(){
变量位置=[
[Bondi Beach',-33.890542151.274856,4],
[Coogee Beach',-33.923036151.259052,5],
[Cronulla Beach',-34.028249151.157507,3],
[‘曼利海滩’,-33.80010128657071151.28747820854187,2],
[‘马鲁布拉海滩’,-33.950198151.259302,1]
];
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新谷歌地图LatLng(-33.92151.25),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
var bounds=new google.maps.LatLngBounds();
对于(i=0;i
您需要严格执行以下建议:
首先,可以通过包含所有动态生成的位置来创建LatLngBounds对象。使用“延伸”方法包括点
然后使用map.fitBounds
方法在标记上居中并缩放地图
您需要严格执行以下建议:
首先,可以通过包含所有动态生成的位置来创建LatLngBounds对象。使用“延伸”方法包括点
然后使用map.fitBounds
方法在标记上居中并缩放地图
谢谢大家!经过一点努力,我几乎得出了相同的解决方案。谢谢大家!经过一番努力,我几乎达到了同样的目的