【问题标题】:How to control zoom after search has been executed?执行搜索后如何控制缩放?
【发布时间】:2015-09-11 02:01:49
【问题描述】:

当我在这张地图上搜索地址时,它会在地图无用的地方放大太多。我试过调整地图边界,但没有帮助。我认为我在正确的道路上,但它只是不工作。有人可以帮我解决这个问题吗?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style>
 #googft-mapCanvas {
    height: 600px;
    margin: 0;
    padding: 0;
    width: 100%;
}
.controls {
    margin-top: 10px;
    border: 1px solid transparent;
    border-radius: 2px 0 0 2px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    height: 32px;
    outline: none;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
    background-color: #fff;
    font-family: Roboto;
    font-size: 15px;
    font-weight: 300;
    margin-left: 12px;
    padding: 0 11px 0 13px;
    text-overflow: ellipsis;
    width: 300px;
}
#pac-input:focus {
    border-color: #4d90fe;
}
.pac-container {
    font-family: Roboto;
}
#type-selector {
    color: #fff;
    background-color: #4d90fe;
    padding: 5px 11px 0px 11px;
}
#type-selector label {
    font-family: Roboto;
    font-size: 13px;
    font-weight: 300;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<input id="pac-input" class="controls" type="text" placeholder="Enter your address here">
<div id="googft-mapCanvas"></div>
<script>
function initialize() {
    google.maps.visualRefresh = true;
    var isMobile = (navigator.userAgent.toLowerCase().indexOf('android') > -1) || (navigator.userAgent.match(/(iPod|iPhone|iPad|BlackBerry|Windows Phone|iemobile)/));
    if (isMobile) {
        var viewport = document.querySelector("meta[name=viewport]");
        viewport.setAttribute('content', 'initial-scale=1.0, user-scalable=no');
    }

    var mapDiv = document.getElementById('googft-mapCanvas');
    mapDiv.style.width = isMobile ? '100%' : '1000px';
    mapDiv.style.height = isMobile ? '100%' : '300px';
    var map = new google.maps.Map(mapDiv, {
        center: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.HYBRID
    });
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend-open'));
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend'));

    layer = new google.maps.FusionTablesLayer({
        map: map,
        heatmap: {
            enabled: false
        },
        query: {
            select: "col26",
            from: "11Q0B7iRayT2JIOBl8_VRUmitimhX1W01byuFDnAv",
            where: ""
        },
        options: {
            styleId: 2,
            templateId: 2
        }
    });
    if (isMobile) {
        var legend = document.getElementById('googft-legend');
        var legendOpenButton = document.getElementById('googft-legend-open');
        var legendCloseButton = document.getElementById('googft-legend-close');
        legend.style.display = 'none';
        legendOpenButton.style.display = 'block';
        legendCloseButton.style.display = 'block';
        legendOpenButton.onclick = function () {
            legend.style.display = 'block';
            legendOpenButton.style.display = 'none';
        }
        legendCloseButton.onclick = function () {
            legend.style.display = 'none';
            legendOpenButton.style.display = 'block';
        }
    }
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
        map: map,
        draggable: true,
        title: "Your New Home",
    });
     // Create the search box and link it to the UI element.
  var input = document.getElementById('pac-input');
  var searchBox = new google.maps.places.SearchBox(input);
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  // Bias the SearchBox results towards current map's viewport.
  map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
  });

  // Listen for the event fired when the user selects a prediction and retrieve
  // more details for that place.
  searchBox.addListener('places_changed', function() {
    var places = searchBox.getPlaces();
    if (places.length == 0) {
      return;
    }
    // For each place, get the icon, name and location.
    var bounds = new google.maps.LatLngBounds();
    places.forEach(function(place) {
      if (place.geometry.viewport) {
        // Only geocodes have viewport.
        bounds.union(place.geometry.viewport);
      } 
      else {
        bounds.extend(place.geometry.location);
      }
      // now let's move the marker
      marker.setPosition(place.geometry.location);
    });
    map.fitBounds(bounds);
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body>
</body>
</html>

【问题讨论】:

  • 你所说的““它在地图没有用处的地方放大了太多”是什么意思?你能提供一个地址的例子吗?你期望会发生什么?什么是您想看到的最大缩放比例?
  • 如果您搜索 700 Clark Ave, St. Louis, MO 63102,它将放大,以便您看到的只是您正在搜索的建筑物。我不确定最大变焦,我想玩一下。我想看看寄宿学区。

标签: javascript jquery google-maps google-maps-api-3


【解决方案1】:

在您的代码中,您使用map.fitBounds(bounds);,其中bounds 是从搜索结果返回的place.geometry.viewport,默认情况下会尽可能放大。相反,您希望平移到新位置,这会保存您当前的缩放级别。更多信息请查看this jsfiddle demo

【讨论】:

猜你喜欢
  • 2019-03-07
  • 2011-12-24
  • 1970-01-01
  • 2010-11-01
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
相关资源
最近更新 更多