【问题标题】:Monotouch: Center a Map with Zoom with MapView (iPhone)Monotouch:使用 MapView 缩放地图居中 (iPhone)
【发布时间】:2011-07-31 17:38:44
【问题描述】:

有没有一种简单的方法可以使用 MapView 设置谷歌地图的中心和缩放?

在 javascript 中,我们只是这样做:

map.setCenter(new google.maps.LatLng(44, -44), 13);

但除了大量的数学运算之外,没有简单的方法可以在 MapView 中执行此操作。

【问题讨论】:

标签: iphone xamarin.ios mkmapview


【解决方案1】:

我找到了一个 Objective-C 的答案,并为任何需要它的人翻译了它。

若要使用,请将其放在某个辅助类中并调用 SetCenterCoordinate()

来源:http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/

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;
    }

还有一个有用的:

  public static void CenterMapToAnnotations(MKMapView Map, double latlongPadding)
  {
     if (Map == null || Map.Annotations == null)
        return;


     List<MKAnnotation> annotations = new List<MKAnnotation>();
     foreach (var a in Map.Annotations)
     {
        annotations.Add(a as MKAnnotation);
     }
     if (annotations.Count > 1)
     {

        double maxLat = annotations.OrderByDescending (e => e.Coordinate.Latitude).FirstOrDefault ().Coordinate.Latitude;
        double maxLong = annotations.OrderByDescending (e => e.Coordinate.Longitude).FirstOrDefault ().Coordinate.Longitude;

        double minLat = annotations.OrderBy (e => e.Coordinate.Latitude).FirstOrDefault ().Coordinate.Latitude;
        double minLong = annotations.OrderBy (e => 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;
     }

  }

【讨论】:

    【解决方案2】:
    -(void) centerMap 
    {
    
    MKCoordinateRegion region;
    
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 120;
    CLLocationDegrees minLon = 150;
    
    for (int i=0; i<[self.arallData count]; i++) {
        Place* home = [[[Place alloc] init] autorelease];
        home.latitude = [[[self.arallData objectAtIndex:i] valueForKey:@"Latitude"]floatValue];
        home.longitude =[[[self.arallData objectAtIndex:i] valueForKey:@"Longitude"]floatValue];
        PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];     
    
        CLLocation* currentLocation = (CLLocation*)from ;
        if(currentLocation.coordinate.latitude > maxLat)
            maxLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.latitude < minLat)
            minLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.longitude > maxLon)
            maxLon = currentLocation.coordinate.longitude;
        if(currentLocation.coordinate.longitude < minLon)
            minLon = currentLocation.coordinate.longitude;
    
        region.center.latitude     = (maxLat + minLat) / 2;
        region.center.longitude    = (maxLon + minLon) / 2;
        region.span.latitudeDelta  =  maxLat - minLat;
        region.span.longitudeDelta = maxLon - minLon;
    }
    
    
    [mapView setRegion:region animated:YES];
    
    }
    

    【讨论】:

    • 谢谢拉克什。我在 C# 中需要它,所以翻译了我找到的另一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2013-02-17
    相关资源
    最近更新 更多