我想在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;
}
我该怎么做。任何例子都会有用。
答案 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;
}