2016-06-24 38 views
0

我想在Google Map上顯示我所有的標記。我正在使用組件商店中的Google map sdk組件。我可以顯示一個標記。但是我想顯示列表中的所有標記。我確實找到了建議使用GMSCoordinateBounds的解決方案,但我無法使用我正在使用的sdk找到它。我知道我必須使用CameraUpdate.FitBounds()用Google Map sdk顯示所有標記iOS Xamarin

我已經實現了下面的代碼,但是這隻顯示列表中的最後一個標記。

private float CalculateMarkerInclusiveZoomLevel(MapView mapView, List<Marker> markers, int minVisible) 
     { 
      try 
      { 
       var bounds = 
        new CoordinateBounds(mapView.Projection.VisibleRegion); 
       var initialZoomLevel = mapView.Camera.Zoom; 
       markerInclusiveZoomLevel = initialZoomLevel; 
      var count = markers.Count(
       m => bounds.ContainsCoordinate(m.Position)); 

      while (count < markers.Count && count < minVisible) 
      { 
       // Each zoom level doubles the viewable area 
       var latGrowth = 
        (bounds.NorthEast.Latitude - bounds.SouthWest.Latitude)/2; 
       var lngGrowth = 
        (bounds.NorthEast.Longitude - bounds.SouthWest.Longitude)/2; 
       markerInclusiveZoomLevel--; 

       bounds = new CoordinateBounds(
        new CLLocationCoordinate2D(
         bounds.NorthEast.Latitude + latGrowth, 
         bounds.NorthEast.Longitude + lngGrowth), 
        new CLLocationCoordinate2D(
         bounds.SouthWest.Latitude - latGrowth, 
         bounds.SouthWest.Longitude - lngGrowth)); 

       count = markers.Count(m => bounds.ContainsCoordinate(m.Position)); 
       return markerInclusiveZoomLevel; 
      } 

     } 
     catch (Exception ex) 
     { 

     } 

     return markerInclusiveZoomLevel; 
    } 

我該怎麼做。任何示例都會有用。

回答

1

但這隻顯示列表中的最後一個標記。

好吧。在您的while循環中,您將返回作爲最終聲明。

您可能想要將該return語句移出while循環。所以:

private float CalculateMarkerInclusiveZoomLevel(MapView mapView, List<Marker> markers, int minVisible) 
{ 
    try 
    { 
     var bounds = new CoordinateBounds(mapView.Projection.VisibleRegion); 
     var initialZoomLevel = mapView.Camera.Zoom; 
     markerInclusiveZoomLevel = initialZoomLevel; 
     var count = markers.Count(m => bounds.ContainsCoordinate(m.Position)); 

     while (count < markers.Count && count < minVisible) 
     { 
      // Each zoom level doubles the viewable area 
      var latGrowth = 
       (bounds.NorthEast.Latitude - bounds.SouthWest.Latitude)/2; 
      var lngGrowth = 
       (bounds.NorthEast.Longitude - bounds.SouthWest.Longitude)/2; 
      markerInclusiveZoomLevel--; 

      bounds = new CoordinateBounds(
       new CLLocationCoordinate2D(
        bounds.NorthEast.Latitude + latGrowth, 
        bounds.NorthEast.Longitude + lngGrowth), 
       new CLLocationCoordinate2D(
        bounds.SouthWest.Latitude - latGrowth, 
        bounds.SouthWest.Longitude - lngGrowth)); 

      count = markers.Count(m => bounds.ContainsCoordinate(m.Position)); 
     } 
     return markerInclusiveZoomLevel; 
    } 
    catch (Exception ex) 
    { 

    } 
    return markerInclusiveZoomLevel; 
} 
+0

嗨,我試着做同樣的最初,但回報聲明永遠不會達成。 –

相關問題