我试图用集合坐标在地图上显示一个标记,然后显示用户相对于标记的地理位置。我能够同时显示地图上的标记和地理位置(如果可用的话),但是地图只位于地理位置。我认为我需要使用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);
}}
发布于 2015-06-25 11:17:00
使用bounds作为参数的位置初始化marker:
var bounds = new google.maps.LatLngBounds(center);与评论有关:
若要在视口中同时使用这两个标记,但在中间有一个标记,您可以:
pos为“中心”pos与center的距离作为radiusmap.fitBounds()
var pos =新google.maps.LatLng(position.coords.latitude,position.coords.longitude),循环=新google.maps.Circle({center:pos,radius: google.maps.Marker)},地理定位=新google.maps.Marker({ pos,map: map,title:'Your‘});map.fitBounds(circle.getBounds());https://stackoverflow.com/questions/31048563
复制相似问题