【问题标题】:Javascript Google map api V3 fitbounds with center location带有中心位置的 Javascript Google 地图 api V3 fitbounds
【发布时间】:2012-02-16 16:34:52
【问题描述】:

我想将图钉装订到用户位置图钉周围可见。我编写了以下代码,它以用户位置为中心,但很少有图钉超出地图??

仅供参考:userPinLoc 是已填充的图钉对象

 function setInitialZoom() {
            mapZoom = googleMap.getZoom(); 
            var bounds = new google.maps.LatLngBounds();
            bounds.extend(userPinLoc);
            for (i in nearestEntitiesToZoom) {
                entity = nearestEntitiesToZoom[i];
                var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
                bounds.extend(googleLatLng);
            }

            google.maps.event.addDomListener(googleMap, 'bounds_changed', function() {
                googleMap.setCenter(userPinLoc);
            });
                 googleMap.fitBounds(bounds);
        }

【问题讨论】:

    标签: google-maps-api-3 location center fitbounds


    【解决方案1】:

    我不确定你从哪里得到userPinLoc。试一试:

    ...
    var bounds = new google.maps.LatLngBounds();
    
    // Go through each...
    for (i in nearestEntitiesToZoom) {
        entity = nearestEntitiesToZoom[i];
        var googleLatLng = new google.maps.LatLng(entity.latitude, entity.longitude);
        bounds.extend(googleLatLng);
    };
    
    // Fit these bounds to the map
    googleMap.fitBounds(bounds);
    ...
    

    请记住,fitCenterfitBounds 需要一个 LatLng 对象作为参数。

    此代码改编自:http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/

    【讨论】:

    • 抱歉忘记了,我已经有了“googleMap.fitBounds(bounds);”在我的实现中这一行,除了你的代码之外,我没有看到任何变化。
    • 我的错。在您对问题进行最后一次修订之前,我开始发送此消息(它没有最后一个 googleMap.fitBounds(bounds); 行)
    【解决方案2】:

    我是用 java 和 javascript 做的

    public static void calculateMapFitBounds(GeoLocation userLocation, List<GeoLocation> contents, Map<String, GeoLocation> latlngBounds){
    
        if (Util.isEmtpyGeoLocation(userLocation) || contents == null || contents.isEmpty()) {
            return;
        }
    
        //SW
        double minLat = userLocation.getLatitude();
        double minLng = userLocation.getLongitude();
    
        //NE
        double maxLat = userLocation.getLatitude();
        double maxLng = userLocation.getLongitude();
    
        for(GeoLocation content: contents){
    
            /*
             * Populating Top left cordinate (SW)
             */
            minLat = Math.min(minLat, content.getLatitude());
            minLng = Math.min(minLng, content.getLongitude());
    
            /*
             * Populating Bottom right cordinate (NE)
             */
            maxLng = Math.max(maxLng, content.getLongitude()) ;
            maxLat = Math.max(maxLat, content.getLatitude());
        }
    
        /*
         * Calculating Delta fit bounds
         */
    
        double latDelta = Math.max(Math.abs(userLocation.getLatitude() - minLat), Math.abs(maxLat-userLocation.getLatitude()));
    
        double lngDelta = Math.max(Math.abs(userLocation.getLongitude() - maxLng), Math.abs(minLng - userLocation.getLongitude()));
    
        //Calculating SW
        minLat = userLocation.getLatitude() - latDelta;
        minLng = userLocation.getLongitude()- lngDelta;
    
    
        latlngBounds.put("swLatLng", new GeoLocation(minLat, minLng));
    
    
        //Calculating NE
        maxLat = userLocation.getLatitude() + latDelta;
        maxLng = userLocation.getLongitude()+ lngDelta;
    
        latlngBounds.put("neLatLng", new GeoLocation(maxLat, maxLng));
    
    }
    

    我正在使用速度视图,所以这里是速度和 js 代码

    #if($swLatLng && $neLatLng)
            var swLatLn = new google.maps.LatLng($!swLatLng.latitude, $!swLatLng.longitude, false);
            var neLatLn = new google.maps.LatLng($neLatLng.latitude, $neLatLng.longitude, false);
    
            var bounds = new google.maps.LatLngBounds(swLatLn, neLatLn);
            googleMap.fitBounds(bounds);
    
            #end
    

    【讨论】:

      【解决方案3】:

      当我之前完成此操作时,我已将地图中心的 bounds.extend() 作为最后一个,而不是第一个。出于某种原因,这似乎效果更好。

      function initialize() {
          var points = [
              {
                  lat: 51.498725,
                  lng: -0.17312
              },
              {
                  lat: 51.4754091676,
                  lng: -0.186810493469
              },
              {
                  lat: 51.4996066187,
                  lng: -0.113682746887
              },
              {
                  lat: 51.51531272,
                  lng: -0.176296234131
              }
          ];
      
          var centerLatLng = {lat: 51.532315, lng: -0.1544};
      
          var map = new google.maps.Map(document.getElementById("map"), {
              zoom:               15,
              center:             centerLatLng,
              mapTypeId:          google.maps.MapTypeId.ROADMAP
          });
      
          var bounds = new google.maps.LatLngBounds();
      
          var homeMarker = new google.maps.Marker({
              position: centerLatLng,
              map: map,
              icon: "http://maps.google.com/mapfiles/ms/micons/green-dot.png"
          });
      
          for (var i = 0; i < points.length; i++) {       
              var marker = new google.maps.Marker({
                  position: points[i],
                  map: map
              });
      
              bounds.extend(points[i]);
          }
      
          bounds.extend(centerLatLng);
      
          map.fitBounds(bounds);
      }
      

      【讨论】:

      • 做同样的事情扩展边界然后添加中心位置作为边界然后调用拟合边界。你确定它会 100% 导致中心销被其他销包围并完美贴合吗?
      猜你喜欢
      • 2016-09-03
      • 2011-01-30
      • 2012-05-03
      • 1970-01-01
      • 2012-01-25
      • 2020-10-07
      • 2015-05-15
      • 1970-01-01
      • 2012-05-30
      相关资源
      最近更新 更多