MKMapRectMake如何在设置后缩小

时间:2013-07-29 11:14:25

标签: ios objective-c cocoa-touch mkmapview mkmaprect

我使用MKMapRectMake标记东北和西南以显示区域。以下是我的表现方式:

routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
[self.mapView setVisibleMapRect:routeRect];

设置此显示区域后,如何缩小地图?这样做的最佳方式是什么?

更新

这是我用rect函数获取setVisibleMapRect的代码:

for(Path* p in ar)
    {
        self.routeLine = nil;
        self.routeLineView = nil;

        // while we create the route points, we will also be calculating the bounding box of our route
        // so we can easily zoom in on it.
        MKMapPoint northEastPoint;
        MKMapPoint southWestPoint;

        // create a c array of points.
        MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * ar.count);

        for(int idx = 0; idx < ar.count; idx++)
        {
            Path *m_p = [ar objectAtIndex:idx];
            [NSCharacterSet characterSetWithCharactersInString:@","]];



            CLLocationDegrees latitude  = m_p.Latitude;
            CLLocationDegrees longitude = m_p.Longitude;

            // create our coordinate and add it to the correct spot in the array
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);


            MKMapPoint point = MKMapPointForCoordinate(coordinate);

            // adjust the bounding box
            // if it is the first point, just use them, since we have nothing to compare to yet.
            if (idx == 0) {
                northEastPoint = point;
                southWestPoint = point;
            }
            else
            {
                if (point.x > northEastPoint.x)
                    northEastPoint.x = point.x;
                if(point.y > northEastPoint.y)
                    northEastPoint.y = point.y;
                if (point.x < southWestPoint.x)
                    southWestPoint.x = point.x;
                if (point.y < southWestPoint.y)
                    southWestPoint.y = point.y;
            }

            pointArr[idx] = point;
        }

        // create the polyline based on the array of points.
        self.routeLine = [MKPolyline polylineWithPoints:pointArr count:ar.count];

        _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
        // clear the memory allocated earlier for the points
        free(pointArr);


        [self.mapView removeOverlays: self.mapView.overlays];
        // add the overlay to the map
        if (nil != self.routeLine) {
            [self.mapView addOverlay:self.routeLine];
        }

        // zoom in on the route.
        [self zoomInOnRoute];

    } 

4 个答案:

答案 0 :(得分:4)

试试这个:(编辑)

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    CLLocationCoordinate2D zoomLocation = newLocation.coordinate;
    region.center = zoomLocation;
    region.span = span;
    region = [mapViewObject regionThatFits:region];
    [mapViewObject setRegion:region animated:NO];

答案 1 :(得分:2)

您可以使用此自定义功能将地图围绕两个点居中

- (void)centerMapAroundSourceAndDestination
{
  MKMapRect rect = MKMapRectNull;
  MKMapPoint sourcePoint = MKMapPointForCoordinate(southWestPoint);
  rect = MKMapRectUnion(rect, MKMapRectMake(sourcePoint.x, sourcePoint.y, 0, 0));
  MKMapPoint destinationPoint = MKMapPointForCoordinate(_northEastPoint);
  rect= MKMapRectUnion(rect, MKMapRectMake(destinationPoint.x, destinationPoint.y, 0, 0));
  MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
  [_mapView setRegion:region animated:YES];
}

答案 2 :(得分:1)

因此,在这种情况下,您需要找到多边形的Centroid,然后将该质心值传递给此方法,以便它可以缩放到多边形的中心,即Centroid。

- (void)zoomMapView:(MKMapView *)mapview withLatitude:(Float32 )latitude andLongitude:(Float32 )longitude {
    MKCoordinateRegion region;
    region.span.latitudeDelta =0.005;  //Change values to zoom. lower the value to zoom in and vice-versa
    region.span.longitudeDelta = 0.009;//Change values to zoom. lower the value to zoom in and vice-versa
    CLLocationCoordinate2D location;
    location.latitude = latitude;   // Add your Current Latitude here.
    location.longitude = longitude; // Add your Current Longitude here.
    region.center = location;
    [mapview setRegion:region];
}

要使用此方法,您需要传递三个mapView,纬度和经度,即位置放大。

答案 3 :(得分:0)

  

如何稍微缩小地图?

Unfortunatley MkMapView setRegion表现得如此奇怪,以至于这在iPhone上无效。 (ios 6.1.3) 它适用于iPad(ios 6.1.3)

setRegionsetVisibleMapRect

两者仅以两步为单位更改缩放系数。因此,您无法以编程方式缩小10%。尽管Apple地图是基于矢量的,但它们仍然会捕捉下一个更高的缩放级别,该级别(将)适合地图图块像素1:1。也许与地图卫星显示模式兼容,后者使用预渲染的位图。

虽然如果你提供一个lat / lon跨度并不完美,但是bot方法只应该校正宽高比,它们会额外捕捉到所述缩放级别。

尝试一下:显示地图,然后按下按钮:获取当前区域,使经度跨度增大10%(因子1.1),设置区域,然后读回来,你会看到iphone simu和iphone4设备的经度范围现在是规模的两倍,而不是因子1.1。

直到今天还没有好的解决方案 羞辱你Apple。