2012-01-23 74 views
1

我想fitbound图钉周围用户的位置图钉可见。我写了下面的代码中心的用户位置,但很少图钉出地图?Javascript谷歌地图API中心位置V3 fitbounds

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

回答

0

我没有使用它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 
0

我不知道你在哪里从得到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/

+0

对不起,忘了,我已经有“GoogleMap的。 fitBounds(边界);”这行在我的实现中,我没有看到任何改变,然后在你的代码。 –

+0

我的不好。我在你最后一次修改你的问题(它没有最后一个'googleMap.fitBounds(bounds);'line) – ninty9notout

0

当我这样做之前,我已经做了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); 
} 
+0

做同样的事情扩展边界然后添加中心位置作为边界然后调用适合边界之前开始了这条消息。你确定它将100%合成到由其他引脚包围的中心引脚,并具有完美的配合? –