Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps api 3 Google Maps getBounds()返回未定义的_Google Maps Api 3 - Fatal编程技术网

Google maps api 3 Google Maps getBounds()返回未定义的

Google maps api 3 Google Maps getBounds()返回未定义的,google-maps-api-3,Google Maps Api 3,我正在编写一个代码,它将: -加载地图并将其置于KML的中心 -根据地图的边界绘制多边形 下面是代码。我犯了一个错误 未捕获的TypeError:无法调用未定义的方法“getNorthEast” function initialize() { var mapOptions = { zoom: 19, mapTypeId: google.maps.MapTypeId.ROADMAP //higer zoom }; var KML1 = ne

我正在编写一个代码,它将:

-加载地图并将其置于KML的中心

-根据地图的边界绘制多边形

下面是代码。我犯了一个错误

未捕获的TypeError:无法调用未定义的方法“getNorthEast”

    function initialize()
{
    var mapOptions =
    {
    zoom: 19,
    mapTypeId: google.maps.MapTypeId.ROADMAP //higer zoom
    };

    var KML1 = new google.maps.KmlLayer(
            {
            clickable: false,
            url: 'https://s3.amazonaws.com/navizon.its.fp/1001/f43l9uvts1_a.kml' //kml link for the floor-plan
            });
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    KML1.setMap(map);


    var bounds = new google.maps.LatLngBounds();
    bounds = map.getBounds();
    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var QLat = Math.abs((ne.lat()-sw.lat())/5);
    var QLng = Math.abs((sw.lng()-ne.lng())/5);

    var swLat = sw.lat()+QLat;
    var swLng = sw.lng()+QLng;

    var neLat = ne.lat()-QLat;
    var neLng = ne.lng()-QLng;

    ne = new google.maps.LatLng(neLat,neLng);
    sw = new google.maps.LatLng(swLat,swLng);

    var Coords = [
                ne, new google.maps.LatLng(ne.lat(), sw.lng()),
                sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
            ];


    surface = new google.maps.Polygon(
    {
    paths: Coords,
    strokeColor: '#00AAFF',
    strokeOpacity: 0.6,
    strokeWeight: 2,
    fillColor: '#00CC66',
    fillOpacity: 0.15,
    editable: true,
    draggable: true,
    geodesic:true
    });

    surface.setMap(map);
    google.maps.event.addListener(surface, 'mousemove', ciao) //add listener for changes
    //$("#results").append(coordinates[0]);
  //Let's update area and price as the poly changes
function ciao(event)
{
        var vertices = this.getPath();
        // Iterate over the vertices.
        for (var i =0; i < vertices.getLength(); i++) {
            var xy = vertices.getAt(i);
            Coords = []
            Coords.push(xy);  
            };
}
}
有什么建议吗

谢谢


Daniele

您需要将所有依赖于映射边界的代码放在事件侦听器中

    var surface = null;
    function initialize()
    {
      var mapOptions =
        {
          zoom: 19,
          mapTypeId: google.maps.MapTypeId.ROADMAP //higer zoom
        };

      var KML1 = new google.maps.KmlLayer(
            {
              clickable: false,
              url: 'https://s3.amazonaws.com/navizon.its.fp/1001/f43l9uvts1_a.kml' //kml link for the floor-plan
            });
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      KML1.setMap(map);

      google.maps.event.addListener(map,'bounds_changed', function() 
      {
        var bounds = new google.maps.LatLngBounds();
        bounds = map.getBounds();
        var ne = bounds.getNorthEast();
        var sw = bounds.getSouthWest();

        var QLat = Math.abs((ne.lat()-sw.lat())/5);
        var QLng = Math.abs((sw.lng()-ne.lng())/5);

        var swLat = sw.lat()+QLat;
        var swLng = sw.lng()+QLng;

        var neLat = ne.lat()-QLat;
        var neLng = ne.lng()-QLng;

        ne = new google.maps.LatLng(neLat,neLng);
        sw = new google.maps.LatLng(swLat,swLng);

        var Coords = [
                ne, new google.maps.LatLng(ne.lat(), sw.lng()),
                sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
            ];


        surface = new google.maps.Polygon(
        {
          paths: Coords,
          strokeColor: '#00AAFF',
          strokeOpacity: 0.6,
          strokeWeight: 2,
          fillColor: '#00CC66',
          fillOpacity: 0.15,
          editable: true,
          draggable: true,
          geodesic:true
        });

        surface.setMap(map);
     }); // end of listener callbck

     google.maps.event.addListener(surface, 'mousemove', ciao) //add listener for changes
     //$("#results").append(coordinates[0]);
     //Let's update area and price as the poly changes
     function ciao(event)
     {
       var vertices = this.getPath();
       // Iterate over the vertices.
       for (var i =0; i < vertices.getLength(); i++) {
         var xy = vertices.getAt(i);
         Coords = []
         Coords.push(xy);  
       };
     }

   } // end of initialize
可能重复的