Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found a Objective-C answer to this and have translated it for anyone who wants it.</p> <p>To use, place this in a helper class somewhere and call <strong>SetCenterCoordinate</strong>()</p> <p>Source: <a href="http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/" rel="nofollow">http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/</a></p> <pre><code>void SetCenterCoordinate (MKMapView MapToCenter, CLLocationCoordinate2D centerCoordinate, int zoomLevel, bool animated) { // clamp large numbers to 28 zoomLevel = Math.Min (zoomLevel, 28); // use the zoom level to compute the region MKCoordinateSpan span = CoordinateSpanWithMapView (MapToCenter, centerCoordinate, zoomLevel); MKCoordinateRegion region = new MKCoordinateRegion (centerCoordinate, span); // set the region like normal MapToCenter.SetRegion (region, animated); } static double MERCATOR_OFFSET = 268435456; static double MERCATOR_RADIUS = 85445659.44705395; double LongitudeToPixelSpaceX (double longitude) { return Math.Round (MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * Math.PI / 180.0); } double LatitudeToPixelSpaceY (double latitude) { return Math.Round (MERCATOR_OFFSET - MERCATOR_RADIUS * Math.Log ((1 + Math.Sin (latitude * Math.PI / 180.0)) / (1 - Math.Sin (latitude * Math.PI / 180.0))) / 2.0); } double PixelSpaceXToLongitude (double pixelX) { return ((Math.Round (pixelX) - MERCATOR_OFFSET) / MERCATOR_RADIUS) * 180.0 / Math.PI; } double PixelSpaceYToLatitude (double pixelY) { return (Math.PI / 2.0 - 2.0 * Math.Tan (Math.Exp ((Math.Round (pixelY) - MERCATOR_OFFSET) / MERCATOR_RADIUS))) * 180.0 / Math.PI; } MKCoordinateSpan CoordinateSpanWithMapView (MKMapView mapView, CLLocationCoordinate2D centerCoordinate, int zoomLevel) { // convert center coordiate to pixel space double centerPixelX = LongitudeToPixelSpaceX (centerCoordinate.Longitude); double centerPixelY = LatitudeToPixelSpaceY (centerCoordinate.Latitude); // determine the scale value from the zoom level int zoomExponent = 20 - zoomLevel; double zoomScale = Math.Pow (2, zoomExponent); // scale the map’s size in pixel space SizeF mapSizeInPixels = mapView.Bounds.Size; double scaledMapWidth = mapSizeInPixels.Width * zoomScale; double scaledMapHeight = mapSizeInPixels.Height; // figure out the position of the top-left pixel double topLeftPixelX = centerPixelX - (scaledMapWidth / 2); double topLeftPixelY = centerPixelY - (scaledMapHeight / 2); // find delta between left and right longitudes double minLng = PixelSpaceXToLongitude (topLeftPixelX); double maxLng = PixelSpaceXToLongitude (topLeftPixelX + scaledMapWidth); double longitudeDelta = maxLng - minLng; // find delta between top and bottom latitudes double minLat = PixelSpaceYToLatitude (topLeftPixelY); double maxLat = PixelSpaceYToLatitude (topLeftPixelY + scaledMapHeight); double latitudeDelta = -1 * (maxLat - minLat); // create and return the lat/lng span MKCoordinateSpan span = new MKCoordinateSpan (latitudeDelta, longitudeDelta); return span; } </code></pre> <p>and another useful one:</p> <pre><code> public static void CenterMapToAnnotations(MKMapView Map, double latlongPadding) { if (Map == null || Map.Annotations == null) return; List&lt;MKAnnotation&gt; annotations = new List&lt;MKAnnotation&gt;(); foreach (var a in Map.Annotations) { annotations.Add(a as MKAnnotation); } if (annotations.Count &gt; 1) { double maxLat = annotations.OrderByDescending (e =&gt; e.Coordinate.Latitude).FirstOrDefault ().Coordinate.Latitude; double maxLong = annotations.OrderByDescending (e =&gt; e.Coordinate.Longitude).FirstOrDefault ().Coordinate.Longitude; double minLat = annotations.OrderBy (e =&gt; e.Coordinate.Latitude).FirstOrDefault ().Coordinate.Latitude; double minLong = annotations.OrderBy (e =&gt; e.Coordinate.Longitude).FirstOrDefault ().Coordinate.Longitude; MKCoordinateRegion region = new MKCoordinateRegion (); region.Center.Latitude = (maxLat + minLat) / 2; region.Center.Longitude = (maxLong + minLong) / 2; region.Span.LatitudeDelta = maxLat - minLat+latlongPadding ; region.Span.LongitudeDelta = maxLong - minLong+latlongPadding; Map.Region = region; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload