使用谷歌地图获取地图角坐标 - javascript api

时间:2016-08-01 02:55:43

标签: javascript angularjs google-maps google-maps-api-3 coordinates

我正在尝试获取API要求的谷歌地图的四个角落。

swLat swLng neLat neLng

我有以下代码,但这些是返回对象,我不确定这是否是它甚至要求的。这些配对(一旦它们返回校正基)NE - > ?感谢

   var map = new google.maps.Map(document.getElementById("map"), mapOptions); 


        google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
            var rectangle = new google.maps.Rectangle({
               bounds: map.getBounds()
            })
            var bounds = rectangle.getBounds();
            var NE = bounds.getNorthEast();
                var NEmark = new google.maps.Marker({
            map: map,
            position: NE,
            title: "NE"
          });
          var SW = bounds.getSouthWest();
          var SWmark = new google.maps.Marker({
            map: map,
            position: SW,
            title: "SW"
          });
          // North West
          var NW = new google.maps.LatLng(NE.lat(), SW.lng());
          var NWmark = new google.maps.Marker({
            map: map,
            position: NW,
            title: "NW"
          });
          // South East
          var SE = new google.maps.LatLng(SW.lat(), NE.lng());
          var SEmark = new google.maps.Marker({
            map: map,
            position: SE,
            title: "SE"
          });

          $scope.NE = NE
          $scope.NW = NW
          $scope.SW = SW
          $scope.SE = SE


          var polygon = new google.maps.Polygon({
            map: map,
            paths: [
              [NE, NW, SW, SE]
            ]
          });
          map.setZoom(map.getZoom() - 1);
        });
        console.log("SE" + $scope.SE) //all undefined
        console.log("NW" + $scope.NW)
        console.log("SW" + $scope.SW)
        console.log("NE" + $scope.NE)

1 个答案:

答案 0 :(得分:0)

试试这个:

    google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
        var swLat = map.getBounds().getSouthWest().lat();
        var swLng = map.getBounds().getSouthWest().lng();
        var neLat = map.getBounds().getNorthEast().lat();
        var neLng = map.getBounds().getNorthEast().lng();       
        console.log('swLat: ' + swLat);
        console.log('swLng: ' + swLng);
        console.log('neLat: ' + neLat);
        console.log('neLng: ' + neLng);
    });

希望这有帮助!