【问题标题】:Getting map corner coordinates with google maps - javascript api使用谷歌地图获取地图角坐标 - javascript api
【发布时间】:2023-03-23 01:40:02
【问题描述】:

我正在尝试获取 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)

【问题讨论】:

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


    【解决方案1】:

    试试这个:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多