如何使用多个地图指针制作谷歌地图,以在打开页面时缩放特定区域/位置
How to make Google Map with Multiple Map Pointers to zoom a particular area/location on opening a page
在有多个地图指针的谷歌地图中,我无法缩放到特定区域/位置。目前,它会自动将地图居中,覆盖所有位置。但我需要的是在打开网页时放大到一个特定的位置。这是我的代码,
<script>
jQuery(function ($) {
// Asynchronously Load the map API
var script = document.createElement('script');
script.src = "//maps.googleapis.com/maps/api/js?key=AIzaSyA157quXbUlHHgm4wJJxzD49MivKgPSil8&sensor=false&callback=initialize";
document.body.appendChild(script);
});
function initialize() {
locations = <?php echo json_encode($sl_select_obj) ?>;
markerdata = JSON.parse('<?php echo json_encode($locations_data) ?>');
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap',
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
map.setTilt(45);
// Multiple Markers''
// $info='';
var markers = locations;
<?php
$total = count($locations_data);
$markerinfo = '';
$c = 0;
foreach ($locations_data as $key => $info):
if ($c != $total):
$markerinfo .="['<div class='"info_content'"><h2>" . $info['loctitle'] . "</h2><h3><a href=" . $info['site'] . ">" . $info['site'] . "</a></h3><h3><a href=" . $info['locpermalink'] . ">View Full Office Details</a></h3></div>'],";
else:
$markerinfo .="['<div class='"info_content'"><h2>" . $info['loctitle'] . "</h2><h3><a href=" . $info['site'] . ">" . $info['site'] . "</a></h3><h3><a href=" . $info['locpermalink'] . ">View Full Office Details</a></h3></div>']";
endif;
$c++;
endforeach;
?>
// Info Window Content
var infoWindowContent = [<?php echo $markerinfo; ?>]; //markerdata;
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
// Loop through our array of markers & place each one on the map
for (i = 0; i < markers.length; i++) {
var position = new google.maps.LatLng(markers[i]["lat"], markers[i]["long"]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i]["address"],
icon: '<?php bloginfo('template_directory'); ?>/images/venue-direct-icon.png'
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {
this.setZoom(7);
google.maps.event.removeListener(boundsListener);
});
map.fitBounds(markerBounds);
}
尝试添加这样的"center"选项:
var mapOptions = {
center: new google.maps.LatLng(51.5287718, -0.2416803),
mapTypeId: 'roadmap',
zoom: 15,
draggable: map_draggable,
scrollwheel: map_scrollwheel
};
编辑:为了帮助您,这是一个类似于我在项目中使用的代码:多个位置,如果只有一个,则以第一个为中心,否则我对所有位置使用谷歌自动中心。
jQuery( document ).ready( function( $ ) {
if($(window).width()>991){
var map_draggable = true;
var map_scrollwheel = false;
} else {
var map_draggable = false;
var map_scrollwheel = false;
}
var offices = [];
$('#map-google-container')
.find('.hidden')
.each(function(){
offices[offices.length] =
{
"latitude":$(this).attr('latitude'),
"longitude":$(this).attr('longitude'),
"content":$(this).attr('content')
};
});
var mapOptions = {
zoom: 6,
center: new google.maps.LatLng(offices[0].latitude, offices[0].longitude),
draggable: map_draggable,
scrollwheel: map_scrollwheel,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
panControl: true
};
var map = new google.maps.Map(document.getElementById('map-google'), mapOptions);
var map_styles = [
{
featureType: "all",
stylers: [
{ hue: "#F69200" },
{ saturation: -50 }
]
},{
featureType: "water",
stylers: [
{ color: "#efefef" }
]
}
];
map.setOptions({styles: map_styles});
var image = '/include/images/marker.png';
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < offices.length; i++)
{
var content = offices[i].content;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(offices[i].latitude, offices[i].longitude),
map: map,
icon: image,
animation: google.maps.Animation.DROP,
title: content
});
bounds.extend(marker.position);
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,infowindow));
}
if (offices.length > 1) { map.fitBounds(bounds); }
});
在我的项目中,我必须列出所有的办公室,并展示一张包含所有办公室的独特地图。因此,我在HTML中放入了所有办公室的信息(为用户隐藏)。在JavaScript中,我循环浏览这些属性并填充映射。
希望它能帮助
是的,我可以找到解决方案,因为那些面临类似问题的人可能会觉得它很有帮助,
var map_center_position = new google.maps.LatLng(34.0059657 ,-118.4440441);
bounds.extend(map_center_position);
map.fitBounds(bounds);
相关文章:
- 传单缩放控制位置错误
- 计算CSS3缩放框在另一个框中的最高位置
- 当涉及css缩放时,如何获得页面上的点击位置
- 谷歌地图 v3 缩放特定位置
- 有没有办法防止浏览器缓存滚动位置和缩放级别等值
- 如果地理位置成功,请调整缩放级别
- 传单使用多个链接缩放位置
- 如何使用滑动、缩放和裁剪在 jquery 中操作图像的大小或位置
- CSS 缩放与绝对位置
- 如何在不同浏览器中以及缩放时使单词始终保持在当前行上的位置
- 为什么鼠标光标和红线在将缩放 0.5 添加到正文标签时位置不同
- 缩放后确定鼠标在 HTML5 画布上的位置
- 背景大小设置为包含的正文标签上的位置和缩放元素
- 自动缩放的 SVG 中的鼠标位置
- 使用 jQuery 滑块缩放图像的位置不正确
- 缩放时保留元素大小和位置
- Raphael JS:IE中缩放路径后的错误位置
- 获取CSS位置转换,以不对窗口缩放做出反应
- 在缩放图像上获得正确的单击位置
- HTML5画布缩放位置