自动设置中心和放大谷歌地图API

automatic set center and zoom in google maps api

本文关键字:谷歌地图 API 放大 设置中心      更新时间:2023-09-26

当我在输入框中自动完成这两个位置并单击提交时,会在两个位置之间绘制一条折线。我希望地图居中并相应地缩放,以便在最大缩放时完全可见线条。我无法弄清楚自动居中和缩放。请帮忙。

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        width:100%;
      }
    </style>
     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
    <script>
   var poly, map,place,place1;
   var bounds=new google.maps.LatLngBounds();
function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(-33.8688, 151.2195),
        zoom: 13
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    var input = /** @type {HTMLInputElement} */
    (
    document.getElementById('pac-input'));
    var input1 = (document.getElementById('pac-input1'));

    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input1);
    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo('bounds', map);
    var autocomplete1 = new google.maps.places.Autocomplete(input1);
    autocomplete1.bindTo('bounds', map);
    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
        map: map,
        anchorPoint: new google.maps.Point(0, -29)
    });
    var marker1 = new google.maps.Marker({
        map: map,
        anchorPoint: new google.maps.Point(0, -29)
    });
    var polyOptions = {
        strokeColor: '#000000',
        strokeOpacity: 1.0,
        strokeWeight: 3
    };
    poly = new google.maps.Polyline(polyOptions);
    google.maps.event.addDomListener(document.getElementById('submit'), 'click', function () {
    marker.setVisible(true);
    marker1.setVisible(true);
    map.fitBounds(place.geometry.viewport);
    map.setZoom(5);
   // map.fitBounds(bounds);
   //  map.setCenter(new google.maps.LatLng(((place.geometry.location.k+place1.geometry.location.k)/2),((place.geometry.location.D+place1.geometry.location.D)/2));
        poly.setMap(map);
    });
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        infowindow.close();
        marker.setVisible(false);
         place = autocomplete.getPlace();
        if (!place.geometry) {
            return;
        }
        // If the place has a geometry, then present it on a map.
        if (place.geometry.viewport) {
         //   map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17); // Why 17? Because it looks good.
        }
        marker.setIcon( /** @type {google.maps.Icon} */ ({
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(35, 35)
        }));
        marker.setPosition(place.geometry.location);
       // marker.setVisible(true);
        poly.getPath().setAt(0, marker.getPosition());
     //  var myLatLng=(place.geometry.location.k,place.geometry.location.D);
       //bounds.extend(myLatLng);
        var address = '';
        if (place.address_components) {
            address = [
            (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')].join(' ');
        }
      //  infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
       // infowindow.open(map, marker);
    });
    google.maps.event.addListener(autocomplete1, 'place_changed', function () {
        infowindow.close();
        marker1.setVisible(false);
        place1 = autocomplete1.getPlace();
        if (!place1.geometry) {
            return;
        }
        // If the place has a geometry, then present it on a map.
        if (place1.geometry.viewport) {
            //map.fitBounds(place1.geometry.viewport);
        } else {
            map.setCenter(place1.geometry.location);
            map.setZoom(11); // Why 17? Because it looks good.
        }
        marker1.setIcon( /** @type {google.maps.Icon} */ ({
            url: place1.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(35, 35)
        }));
        marker1.setPosition(place1.geometry.location);
        //locations.push(place.geometry.location);
      //  marker1.setVisible(true);
        poly.getPath().setAt(1, marker1.getPosition());
        //var myLatLng=(place1.geometry.location.k, place1.geometry.location.D);
       //bounds.extend(myLatLng);
        var address = '';
        if (place1.address_components) {
            address = [
            (place1.address_components[0] && place1.address_components[0].short_name || ''), (place1.address_components[1] && place1.address_components[1].short_name || ''), (place1.address_components[2] && place1.address_components[2].short_name || '')].join(' ');
        }
      //  infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
      //  infowindow.open(map, marker1);
    });

}
google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>

<input id="pac-input"  type="text" placeholder="Enter a location"style="width:300px; height:30px;border-width:2px;
 border-style:solid; padding-left:4px;" />
<input id="pac-input1" type="text" placeholder="Enter another location" style="width:300px; height:30px;margin-left:20px; border-width:2px;
 border-style:solid; padding-left:4px;"/>
    <input type="submit" class="btn btn-default"name="submit" id="submit" value="SUBMIT" style="background: rgb(162, 194, 250);
margin-top: 2px;
margin-bottom: 2px;
margin-left: 720px;
" />

<div id="map-canvas"></div>">
 </body>
 </html>
如果要在

提交时使地图适合折线的边界,请使用折线路径的坐标扩展空边界对象:

google.maps.event.addDomListener(document.getElementById('submit'), 'click', function () {
    poly.setMap(map);
    var bounds = new google.maps.LatLngBounds();
    bounds.extend(poly.getPath().getAt(0));
    bounds.extend(poly.getPath().getAt(1));
    map.fitBounds(bounds);
});

斯菲德尔