2012-01-23 12 views
1

私はすべてのユーザーの所在地の押しピンに目に見えるように押しボタンを収めたいと考えています。私はそれがユーザーの場所を中心に次のコードを書いたが、いくつかの押しピンは地図から外れますか?Javascript Google map api V3の中心に位置するfitbound

FYI:userPinLocはすでに、私はあなたから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

を移入されるプッシュピンオブジェクトです。これをやってみる:

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

fitCenterまたはfitBoundsがパラメータとしてLatLngオブジェクトが必要、覚えておいてください。

このコードは、から構成されている:私はこの前にやったときhttp://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/

+0

忘れ申し訳ありませんが、私はすでに「Googleマップを持っています。 fitBounds(bounds); "私の実装ではこの行は、あなたのコードではそれ以外の変更は表示されません。 –

+0

私の悪い。あなたの最後のリビジョンを作成する前にこのメッセージを開始しました(最後の 'googleMap.fitBounds(bounds);'行はありません) – ninty9notout

0

、私は非常に最後の一つとして、マップ中央ではなく、最初の1のために)(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

同じことをして境界を拡張し、次に中心位置を境界として追加してからフィット境界を追加します。それは完璧なfitboundと他のピンに囲まれた中心ピンに結果として100%になるだろうか? –

関連する問題