3

我用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

4 回答 4

5

试试这个:(编辑)

    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];
于 2013-07-29T12:01:35.270 回答
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];
}
于 2013-07-29T11:24:42.167 回答
1

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

- (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、纬度和经度,即要缩放的位置。

于 2013-07-29T11:15:00.307 回答
0

我怎样才能把地图缩小一点?

不幸的是MkMapView,setRegion 的行为很奇怪,以至于这在 iPhone 上不起作用。(ios 6.1.3) 它适用于 iPad (ios 6.1.3)

setRegionsetVisibleMapRect

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

尽管 bot 方法仅在您提供的纬度/经度跨度之一不完美时才应该更正纵横比,但它们还会捕捉到所述缩放级别。

试试看:显示地图,然后按下按钮:获取当前区域,使经度跨度大 10%(因子 1.1),设置区域,然后读回,您将在 iphone simu 和 iphone4 设备上看到经度span 现在是大小的两倍,而不是因子 1.1。

直到今天还没有好的解决方案。
为你苹果感到羞耻。

于 2013-08-11T23:45:22.493 回答