var bounds在我的谷歌地图上不起作用

时间:2011-09-27 23:03:24

标签: javascript html5 google-maps

我遇到了与我的谷歌地图一起工作的问题。所有标记都指向正确的坐标,但当它们出现在地图上时,自动变焦无法正确调整。有人可以帮我解决我的var界限吗。

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);
}

1 个答案:

答案 0 :(得分:1)

你有bounds.extend(myLatLng);但是myLatLng没有创建。你似乎只有两个标记,所以你只需要调用两次边界,每个latlng一次。

bounds.extend(myLatLng);替换为

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