Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios MKMapView缩放以适应注释,同时锁定中心_Ios_Mkmapview - Fatal编程技术网

Ios MKMapView缩放以适应注释,同时锁定中心

Ios MKMapView缩放以适应注释,同时锁定中心,ios,mkmapview,Ios,Mkmapview,我正在尝试缩放以适应地图上的注释,同时锁定中心并提供一些插图 - (void)fitAnnotations:(NSArray *)annotations edgeInsets:(UIEdgeInsets)insets { CLLocationCoordinate2D originalCenter = self.centerCoordinate; MKMapRect mapRect = MKMapRectNull; for (id<MKAnnotation>

我正在尝试缩放以适应地图上的注释,同时锁定中心并提供一些插图

- (void)fitAnnotations:(NSArray *)annotations edgeInsets:(UIEdgeInsets)insets
{
    CLLocationCoordinate2D originalCenter = self.centerCoordinate;
    MKMapRect mapRect = MKMapRectNull;

    for (id<MKAnnotation> annotation in annotations) {
        MKMapPoint p = MKMapPointForCoordinate([annotation coordinate]);
        mapRect = MKMapRectUnion(mapRect, MKMapRectMake(p.x, p.y, 0, 0));
    }

    mapRect = [self mapRectThatFits:mapRect edgePadding:insets];
    MKCoordinateRegion mapRegion = MKCoordinateRegionForMapRect(mapRect);

    // we now try to go back to the original center, while increasing the span by neccessary amount
    MKCoordinateSpan centeringDelta = MKCoordinateSpanMake(fabs(mapRegion.center.latitude - originalCenter.latitude), fabs(mapRegion.center.longitude - originalCenter.longitude));
    mapRegion.center = originalCenter;
    mapRegion.span.latitudeDelta += centeringDelta.latitudeDelta * 2.0;
    mapRegion.span.longitudeDelta += centeringDelta.longitudeDelta * 2.0;
    mapRegion = [self regionThatFits:mapRegion];
    [self setRegion:mapRegion animated:YES];
}
-(void)fit注释:(NSArray*)注释边集:(UIEdgeInsets)插入
{
CLLocationCoordinate2D originalCenter=self.centerCoordinate;
MKMapRect mapRect=MKMapRectNull;
用于(注释中的id注释){
MKMapPoint p=MKMapPointForCoordinate([注释坐标]);
mapRect=MKMapRectUnion(mapRect,MKMapRectMake(p.x,p.y,0,0));
}
mapRect=[self-mapRectThatFits:mapRect edgeadding:insets];
mkCoordinatereRegion mapRegion=mkCoordinatereRegionformaprect(mapRect);
//我们现在尝试回到原来的中心,同时增加必要的跨度
MKCoordinateSpan centeringDelta=MKCoordinateSpanMake(fabs(mapRegion.center.latitude-originalCenter.latitude),fabs(mapRegion.center.latitude-originalCenter.latitude));
mapRegion.center=原始中心;
mapRegion.span.latitudeDelta+=中心delta.latitudelta*2.0;
mapRegion.span.longitudeDelta+=中心delta.longitudeDelta*2.0;
mapRegion=[self RegionAtfits:mapRegion];
[自设置区域:映射区域动画:是];
}
这里代码的第一部分如预期的那样工作:它根据插图进行缩放。然而,它转移了中心


之后我尝试重新调整中心,但失败了。我不确定我关于重新定心的数学是否正确。

尝试类似的方法,通过
MKCoordinateRegionMake
方法使用计算的mapRect以原始中心创建新区域

MKCoordinateRegion mapRegion = MKCoordinateRegionForMapRect(mapRect);
mapRegion = MKCoordinateRegionMake(originalCenter, mapRegion.span);
mapView.region = mapRegion;

代码的第一部分计算符合注释的边界映射矩形是正确的

只需调整“最小”贴图矩形,以使“锁定”中心实际位于中心

我认为,主要的问题是,问题中的代码在调用
maprectthatthafits:
(它本身已经给出了您实际请求的rect的一个稍微修改的版本)后,正在调整跨度以重新居中区域并考虑插入

相反,您的代码应该只计算它想要的实际最小矩形,然后最后调用
setVisibleMapRect:edgePadding:animated:
,让地图视图计算出“适合的矩形”和插入

请尝试以下操作:

- (void)fitAnnotations:(NSArray *)annotations edgeInsets:(UIEdgeInsets)insets
{
    MKMapPoint centerMapPoint = MKMapPointForCoordinate(originalCenter);

    //--- First create minimal bounding map rect to tightly fit annotations...
    MKMapRect minimalMapRect = MKMapRectNull;

    for (id<MKAnnotation> annotation in annotations) {
        MKMapPoint annMapPoint = MKMapPointForCoordinate(annotation.coordinate);
        minimalMapRect = MKMapRectUnion(minimalMapRect, MKMapRectMake(annMapPoint.x, annMapPoint.y, 0, 0));
    }


    //--- Now create adjusted map rect so center coordinate is in the center...

    //distance of desired center from minimal left edge...
    double centerXOffset = centerMapPoint.x - minimalMapRect.origin.x;

    //raw amount width needs to be adjusted to get center in center...
    //negative/positive indicates whether center is in left/right half
    double widthOffset = 2.0 * centerXOffset - minimalMapRect.size.width;

    //add absolute value of raw width offset to minimal width...
    double adjustedWidth = minimalMapRect.size.width + fabs(widthOffset);

    //distance of desired center from minimal top edge...
    double centerYOffset = centerMapPoint.y - minimalMapRect.origin.y;

    //raw amount height needs to be adjusted to get center in center...
    //negative/positive indicates whether center is in top/bottom half
    double heightOffset = 2.0 * centerYOffset - minimalMapRect.size.height;

    //add absolute value of raw height offset to minimal height...
    double adjustedHeight = minimalMapRect.size.height + fabs(heightOffset);

    //adjust origin if necessary (if center is in top/left half)...
    MKMapPoint adjustedOrigin = minimalMapRect.origin;
    if ((centerXOffset / minimalMapRect.size.width) < 0.5)
    {
        adjustedOrigin.x = adjustedOrigin.x + widthOffset;
    }
    if ((centerYOffset / minimalMapRect.size.height) < 0.5)
    {
        adjustedOrigin.y = adjustedOrigin.y + heightOffset;
    }

    //create adjusted MKMapRect...
    MKMapRect adjustedMapRect = MKMapRectMake(adjustedOrigin.x, adjustedOrigin.y, adjustedWidth, adjustedHeight);


    //--- Apply the adjusted map rect with insets to map view...
    [mapView setVisibleMapRect:adjustedMapRect edgePadding:insets animated:YES];
}
-(void)fit注释:(NSArray*)注释边集:(UIEdgeInsets)插入
{
MKMapPoint centerMapPoint=MKMapPointForCoordinate(原始中心);
//---首先创建最小边界映射矩形以紧密匹配注释。。。
MKMapRect minimaprect=MKMapRectNull;
用于(注释中的id注释){
MKMapPoint annMapPoint=MKMapPointForCoordinate(annotation.coordinate);
minimamaprect=MKMapRectUnion(minimamaprect,MKMapRectMake(annMapPoint.x,annMapPoint.y,0,0));
}
//---现在创建调整后的地图矩形,使中心坐标位于中心。。。
//所需中心距最小左边缘的距离。。。
double centerXOffset=centerMapPoint.x-MinimaMapRect.origin.x;
//需要调整原始数量宽度以使中心位于中心。。。
//负/正表示中心是否在左/右半部分
double widthOffset=2.0*centerXOffset-MinimapRect.size.width;
//将原始宽度偏移的绝对值添加到最小宽度。。。
双重调整宽度=MinimapRect.size.width+fabs(宽度偏移);
//所需中心距最小上边缘的距离。。。
double centerYOffset=centerMapPoint.y-MinimaMapRect.origin.y;
//需要调整原始量高度以使中心位于中心。。。
//负/正表示中心是否在上/下半部分
双倍高度偏移=2.0*中心偏移-最小maprect.size.height;
//将原始高度偏移的绝对值添加到最小高度。。。
双重调整高度=最小maprect.size.height+fabs(高度偏移);
//如有必要,调整原点(如果中心位于上半部分/左半部分)。。。
MKMapPoint adjustedOrigin=minimaprect.origin;
if((中心偏移/最小映射矩形尺寸宽度)<0.5)
{
adjustedOrigin.x=adjustedOrigin.x+宽度偏移;
}
如果((中心偏移/最小映射矩形尺寸高度)<0.5)
{
adjustedOrigin.y=adjustedOrigin.y+高度偏移;
}
//创建调整后的MKMapRect。。。
MKMapRect adjustedMapRect=MKMapRectMake(adjustedOrigin.x,adjustedOrigin.y,adjustedWidth,adjustedHeight);
//---将调整后的带插入的地图矩形应用于地图视图。。。
[地图视图设置VisibleMaprect:adjustedMapRect EdgeAdding:insets animated:YES];
}
试试这个

MKMapPoint center = MKMapPointForCoordinate(self.mapView.centerCoordinate);

double maxX = 0;
double maxY = 0;
for (MKPointAnnotation *a in self.mapView.annotations)
{
    MKMapPoint p = MKMapPointForCoordinate(a.coordinate);
    double deltaX = fabs(center.x - p.x);
    double deltaY = fabs(center.y - p.y);
    maxX = MAX(maxX, deltaX);
    maxY = MAX(maxY, deltaY);
}


MKMapRect rect = MKMapRectMake(center.x - maxX, center.y - maxY, maxX * 2, maxY * 2);
rect = [self.mapView mapRectThatFits:rect edgePadding:UIEdgeInsetsMake(20, 20, 20, 20)];

[self.mapView setVisibleMapRect:rect animated:1];

@莫比,我在想另一种方法。不如把地图中心的位置改成你已经做过的位置。现在计算从该中心坐标到每个注释的距离,直到找到最长的注释(如“requiredDistance”)。 使用以下代码获得一个新的map rect,其中所有注释都以相同的中心绘制:

    MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:self.centerCoordinate radius:requiredDistance];
    [self.mapView setVisibleMapRect:circleLine.boundingMapRect];

进入插图时,您希望应用的插图应应用于“requiredDistance”变量,使“requiredDistance”变量的值始终大于或等于中心坐标和最长注释之间的距离,以确保所有注释始终可见。

您有没有是否尝试使用
MKCoordinateRegionMake
创建新地图区域?这将允许您将
中心坐标
span
@rmp传递到右侧,但当您移动中心时,您确保适合的一些注释现在已超出范围。如果在(0,0)上有注释,并且我将中心移动(0,-5),您应该能够在原始中心传递而不移动它,那么顶部注释现在将位于(0,-5)。这就是我试图避免的。如果你用
分传递你新计算的
span