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

2

You can use this custom function to center the Map around two points

- (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、緯度、経度、つまりズームする位置の 3 つを渡す必要があります。

于 2013-07-29T11:15:00.307 に答える