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

标签 ios swift google-maps xamarin markers

我想在 Google map 上显示我所有的标记。我正在使用组件商店中的 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;
    }

我该怎么做。任何示例都会很有用。

最佳答案

but this only shows the last marker in the list only.

嗯,是的。在 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;
}

关于ios - 使用 Google Map sdk iOS Xamarin 显示所有标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38006856/

相关文章:

ios - 似乎无法通过 "fatal error: unexpectedly found nil while unwrapping an Optional value"错误

ios - 显示下载进度的通知小部件

Swift 3 替换多个字符串的出现

swift - iOS/swift : QLPreviewController shows blank page in iOS11

javascript - 谷歌地图标记作为链接

ios - Xcode 9.3.1 - iOS Swift 4.1 UITextView.becomeFirstResponder 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x4d555458)

ios - 想通过@IBInspectable实现Keyboard选择

ios - 没有收到详细 View 的数据

android - 如何将 Google map 标记保持在 map 中心而不出现延迟?

javascript - 如何在谷歌地图标记中添加点击事件?