コード例 #1
0
        public static void ChangeRegionSpanDependingOnPinchScale(this MKMapView mapView, MKCoordinateSpan originalSpan, nfloat scale)
        {
            var centerPixelX = LongitudeToPixelSpaceX(mapView.Region.Center.Longitude);
            var centerPixelY = LatitudeToPixelSpaceY(mapView.Region.Center.Latitude);

            var topLeftLongitude = mapView.Region.Center.Longitude - (originalSpan.LongitudeDelta / 2);
            var topLeftLatitude  = mapView.Region.Center.Latitude - (originalSpan.LatitudeDelta / 2);

            var topLeftPixelX = LongitudeToPixelSpaceX(topLeftLongitude);
            var topLeftPixelY = LatitudeToPixelSpaceY(topLeftLatitude);

            var deltaPixelX = Math.Abs(centerPixelX - topLeftPixelX);
            var deltaPixelY = Math.Abs(centerPixelY - topLeftPixelY);

            var scaledDeltaPixelX = deltaPixelX / scale;
            var scaledDeltaPixelY = deltaPixelY / scale;

            var newLongitudeForTopLeft = PixelSpaceXToLongitude(centerPixelX - scaledDeltaPixelX);
            var newLatitudeForTopLeft  = PixelSpaceYToLatitude(centerPixelY - scaledDeltaPixelY);

            var newDeltaLongitude = Math.Abs(newLongitudeForTopLeft - mapView.Region.Center.Longitude) * 2;
            var newDeltaLatitude  = Math.Abs(newLatitudeForTopLeft - mapView.Region.Center.Latitude) * 2;

            newDeltaLatitude  = Math.Max(0.0f, Math.Min(newDeltaLatitude, 90.0f));
            newDeltaLongitude = Math.Max(0.0f, Math.Min(newDeltaLongitude, 90.0f));

            var region = new MKCoordinateRegion(mapView.Region.Center, new MKCoordinateSpan(newDeltaLatitude, newDeltaLongitude));

            mapView.SetRegion(region, false);
        }
コード例 #2
0
        /// <summary>
        /// Sets the map region to given Google Maps zoom level
        /// Will convert the zoom level to the correct MKCoordinateSpan
        /// Original Objective-C code found here: http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
        /// </summary>
        public static void SetCenterCoordinate(this MKMapView mapView, CLLocationCoordinate2D centerCoordinate, float zoomLevel, bool animated)
        {
            // clamp large numbers to 28
            zoomLevel = Math.Min(zoomLevel, 28);

            // use the zoom level to compute the region
            var span   = CoordinateSpanWithMapView(mapView, centerCoordinate, zoomLevel);
            var region = new MKCoordinateRegion(centerCoordinate, span);

            // set the region like normal
            mapView.SetRegion(region, animated);
        }