我正在尝试在设置坐标的地图上显示标记,然后显示与标记相关的用户地理位置。我可以在地图上显示标记和地理位置(如果可用),但地图仅位于地理位置。我想我需要使用fitBounds来适应视图中的两个标记,但我不能让它工作。有人可以帮忙吗?
function initialize() {
var latitude = 51.5039713;
var longitude = -0.114518;
var mapCanvas = document.getElementById('map-directions');
var center = new google.maps.LatLng(latitude, longitude);
var zoom = 18;
var mapOptions = {
center: center,
zoom: zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
}
var map = new google.maps.Map(mapCanvas, mapOptions)
marker = new google.maps.Marker({
map:map,
animation: google.maps.Animation.DROP,
position: center,
icon: '/pickup.png'
});
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var bounds = new google.maps.LatLngBounds();
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var geolocation = new google.maps.Marker({position: pos, map: map, title: 'Your geolocation', });
bounds.extend(geolocation.getPosition());
map.fitBounds(bounds);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
答案 0 :(得分:1)
将bounds
的位置初始化为marker
作为参数:
var bounds = new google.maps.LatLngBounds(center);
与评论相关:
要在视口中同时使用两个标记,但在中心位置1,您可以:
pos
作为“中心pos
与center
之间的距离radius
使用圆的边界作为参数调用map.fitBounds()
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude),
circle = new google.maps.Circle({center:pos,
radius: google.maps.geometry.spherical.computeDistanceBetween(pos,center)}),
geolocation = new google.maps.Marker({position: pos, map: map, title: 'Your geolocation'});
map.fitBounds(circle.getBounds());