使用Google Map sdk iOS Xamarin显示所有标记

时间:2016-06-24 06:26:04

标签: ios swift google-maps xamarin markers

我想在Google地图上显示所有标记。我正在使用Component store中的Google map sdk组件。我可以显示一个标记很好。但我想在列表中显示我所有的标记。我确实找到了建议使用GMSCoordinateBounds的解决方案,但我找不到我正在使用的sdk。我知道我必须使用CameraUpdate.FitBounds()

我已经实现了以下代码,但这只显示了列表中的最后一个标记。

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 个答案:

答案 0 :(得分: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;
}