Var边界在我的谷歌地图上不起作用

var bounds not working on my google maps

本文关键字:不起作用 谷歌地图 我的 边界 Var      更新时间:2023-09-26

我有一个问题得到边界工作与我的谷歌地图。所有的标记都指向正确的坐标,但是当它们出现在地图上时,自动缩放不能正确调整。有人能帮我一下吗?

var companyLocale = new google.maps.LatLng(33.206060, -117.111951);
var noGeo = new google.maps.LatLng(40.69847032728747, -73.9514422416687);

function initialize() {
  var myOptions = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
   <!-------------------------------USER MARKERS-------------------> 
  var usericon = new google.maps.MarkerImage(
    'images/userimage.png',
    new google.maps.Size(48,48),
    new google.maps.Point(0,0),
    new google.maps.Point(24,48)
  );
  var usershadow = new google.maps.MarkerImage(
    'images/usershadow.png',
    new google.maps.Size(76,48),
    new google.maps.Point(0,0),
    new google.maps.Point(24,48)
  ); 
  <!-------------------------------COMP MARKERS------------------->
  var icon = new google.maps.MarkerImage(
    'images/image.png',
    new google.maps.Size(48,48),
    new google.maps.Point(0,0),
    new google.maps.Point(24,48)
  );
  var shadow = new google.maps.MarkerImage(
    'images/shadow.png',
    new google.maps.Size(76,48),
    new google.maps.Point(0,0),
    new google.maps.Point(24,48)
  );
function toggleBounce() {
    if (marker.getAnimation() != null) {
      marker.setAnimation(null);
    } else {
      marker.setAnimation(google.maps.Animation.BOUNCE);
    }
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  // Safari supports the W3C Geolocation method
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      var bounds = new google.maps.LatLngBounds();
      var placeMarker = new google.maps.Marker({
        position: initialLocation,
        icon: usericon,
        shadow: usershadow,
        animation: google.maps.Animation.DROP,
        map: map
      });
      map.setCenter(initialLocation);
       var bounds = new google.maps.LatLngBounds();
       var Marker = new google.maps.Marker({
        position: companyLocale,
        icon: icon,
        shadow: shadow,
        animation: google.maps.Animation.DROP,
        map: map
      });
       google.maps.event.addListener(Marker, 'click', function() {
        window.location = "main-export/main.html";
 });
       google.maps.event.addListener(placeMarker, 'click', function() {
        window.location = "main-export/loyalty.html";
 });
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation();
  }
  function handleNoGeolocation() {
    initialLocation = noGeo;
    map.setCenter(initialLocation);
  }
  bounds.extend(myLatLng);
    map.fitBounds(bounds);
}

你有bounds.extend(myLatLng);,但没有地方myLatLng被创建。您似乎只有两个标记,所以您应该只需要调用两次bounds,每个latlng调用一次。

bounds.extend(myLatLng);替换为

bounds.extend(companyLocale);
bounds.extend(initialLocation);