Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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 需要显示显示两个CLP位置引脚的屏幕_Ios_Objective C_Mkmapview_Core Location - Fatal编程技术网

Ios 需要显示显示两个CLP位置引脚的屏幕

Ios 需要显示显示两个CLP位置引脚的屏幕,ios,objective-c,mkmapview,core-location,Ios,Objective C,Mkmapview,Core Location,我有一个mapView,它假设同时包含用户位置pin和对象pin。我想要的是包含两个管脚的显示屏幕,不管它们之间的距离有多大(假设不会太大)。由于某些原因,我无法显示用户位置,屏幕移动到海洋中的某个点。这是我的代码并尝试获取它: -(MKCoordinateRegion)regionForAnnotations:(NSArray*)annotations{ MKCoordinateRegion region; if ([annotations count] ==0){

我有一个mapView,它假设同时包含用户位置pin和对象pin。我想要的是包含两个管脚的显示屏幕,不管它们之间的距离有多大(假设不会太大)。由于某些原因,我无法显示用户位置,屏幕移动到海洋中的某个点。这是我的代码并尝试获取它:

-(MKCoordinateRegion)regionForAnnotations:(NSArray*)annotations{


    MKCoordinateRegion region;

    if ([annotations count] ==0){
        region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000);
    }   else if ([annotations count] ==1 ){
        id <MKAnnotation> annotation = [annotations lastObject];
        region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);
    }   else {
        CLLocationCoordinate2D topLeftCord;
        topLeftCord.latitude = -90;
        topLeftCord.longitude = 180;

        CLLocationCoordinate2D bottomRightCord;
        bottomRightCord.latitude = 90;
        bottomRightCord.longitude = -180;

        for (id <MKAnnotation> annotation in annotations){
            topLeftCord.latitude = fmax(topLeftCord.latitude, annotation.coordinate.latitude);
            topLeftCord.longitude = fmin(topLeftCord.longitude, annotation.coordinate.longitude);

            bottomRightCord.latitude = fmin(bottomRightCord.latitude, annotation.coordinate.latitude);
            bottomRightCord.longitude = fmax(bottomRightCord.longitude, annotation.coordinate.longitude);


        }

        const double extraSpace = 1.1;
        region.center.longitude = topLeftCord.longitude - (topLeftCord.longitude - bottomRightCord.longitude)/2.0;
        region.center.latitude = topLeftCord.latitude - (topLeftCord.latitude - bottomRightCord.latitude)/2.0;

        region.span.latitudeDelta = fabs(topLeftCord.latitude - bottomRightCord.latitude)*extraSpace;
        region.span.longitudeDelta = fabs(topLeftCord.longitude - bottomRightCord.longitude)*extraSpace;


    }

    return [self.mapView regionThatFits:region];

}

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    //2
    static NSString *identifier = @"Location";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView == nil){
        annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
    }

    //3
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;
    annotationView.pinColor = MKPinAnnotationColorPurple;

    //4
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(getRoute:) forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;

    return annotationView;
};


-(void)setEntityCoordiate{

    CLLocationCoordinate2D location;
    location.latitude = [self.latitudeString doubleValue];
    location.longitude = [self.longitudeString doubleValue];

    CLLocationCoordinate2D user;
    user.latitude = self.mapView.userLocation.coordinate.latitude;
    user.latitude = self.mapView.userLocation.coordinate.longitude;

    CLLocation *userLocation = [[CLLocation alloc]initWithLatitude:user.latitude longitude:user.longitude ];
    CLLocation *entityLocation = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];

    MKCoordinateSpan span;
    span.latitudeDelta = 100;
    span.longitudeDelta = 100;

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 400, 400);

    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = location;
    annotation.title = _titleForEntity;

    [_locations addObject:userLocation];
    [_locations addObject:entityLocation];
    [_mapView addAnnotation:annotation];
}

-(void)getRoute:(UIButton*)button{

    MKCoordinateRegion region = [self regionForAnnotations:_locations];
    [self.mapView setRegion:region animated:YES];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setEntityCoordiate];


}
-(MKCoordinateRegion)注释区域:(NSArray*)注释{
协调区域;
如果([annotations count]==0){
region=mkcoordinaeregionmakewithdistance(self.mapView.userLocation.coordinate,10001000);
}else if([annotations count]==1){
id注释=[annotations lastObject];
region=mkcoordinaeregionmakewithdistance(annotation.coordinate,10001000);
}否则{
CLLOCATION协调2 D顶部左侧电源线;
topLeftCord.纬度=-90;
topLeftCord.longitude=180;
CLLOCATIONCoordinated2D右下软线;
bottomRightCord.纬度=90;
bottomRightCord.longitude=-180;
用于(注释中的id注释){
topLeftCord.latitude=fmax(topLeftCord.latitude,annotation.coordinate.latitude);
topLeftCord.longitude=fmin(topLeftCord.longitude,annotation.coordinate.longitude);
bottomRightCord.latitude=fmin(bottomRightCord.latitude,annotation.coordinate.latitude);
bottomRightCord.longitude=fmax(bottomRightCord.longitude,annotation.coordinate.longitude);
}
常数双外部空间=1.1;
region.center.longitude=topLeftCord.longitude-(topLeftCord.longitude-bottomRightCord.longitude)/2.0;
region.center.latitude=topLeftCord.latitude-(topLeftCord.latitude-bottomRightCord.latitude)/2.0;
region.span.latitudeDelta=fabs(topLeftCord.latitude-bottomRightCord.latitude)*外部空间;
region.span.longitudeDelta=fabs(topLeftCord.longide-bottomRightCord.longide)*外部空间;
}
返回[self.mapView regionatfits:region];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
//2
静态NSString*标识符=@“位置”;
MKPinAnnotationView*annotationView=(MKPinAnnotationView*)[self.mapView出列可重用AnnotationViewWithIdentifier:identifier];
如果(注释视图==nil){
annotationView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符];
}
//3
annotationView.enabled=是;
annotationView.canShowCallout=是;
annotationView.animatesDrop=YES;
annotationView.pinColor=MKPinAnnotationColorPurple;
//4
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[rightButton addTarget:self action:@selector(getRoute:)for ControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=rightButton;
返回注释视图;
};
-(void)SetEntityCoordinate{
CLLOCATION坐标2D定位;
location.latitude=[self.latitudeString doubleValue];
location.longitude=[self.longitudeString doubleValue];
CLLocationCoordinate2D用户;
user.latitude=self.mapView.userLocation.coordinate.latitude;
user.latitude=self.mapView.userLocation.coordinate.longitude;
CLLocation*userLocation=[[CLLocation alloc]initWithLatitude:user.lation经度:user.longitude];
CLLocation*entityLocation=[[CLLocation alloc]initWithLatitude:location.latitude经度:location.latitude];
Mk坐标跨度;
span.latitudeDelta=100;
span.longitudeDelta=100;
mkcoordinaereregion=mkcoordinaereregionmakewithdistance(位置,400400);
[self.mapView setRegion:[self.mapView regionatfits:region]已设置动画:是];
MKPointAnnotation*annotation=[[MKPointAnnotation alloc]init];
注释.坐标=位置;
annotation.title=\u titleForEntity;
[\u locations addObject:userLocation];
[\u locations addObject:entityLocation];
[_MapViewAddAnnotation:annotation];
}
-(void)getRoute:(UIButton*)按钮{
MKCoordinateRegion region=[注释的自区域:_位置];
[self.mapView setRegion:区域动画:是];
}
-(无效)viewDidLoad
{
[超级视图下载];
[自我设置实体坐标];
}
有可能是“太多”的代码,但我不知道我在哪里犯了错误,我粘贴了所有。我想我可能对包含位置对象的数组有问题


如何最终使用这两个位置制作屏幕?任何建议都将不胜感激,谢谢

尝试创建一个区域
flyTo
,该区域将“包含”给定的注释:

MKMapRect flyTo = MKMapRectNull;

for (id <MKAnnotation> annotation in annotations) {
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) {
        flyTo = pointRect;
    } else {
        flyTo = MKMapRectUnion(flyTo, pointRect);
    }
}

mapview.visibleMapRect = flyTo;
MKMapRect flyTo=MKMapRectNull;
用于(注释中的id注释){
MKMapPoint annotationPoint=MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect=MKMapRectMake(annotationPoint.x,annotationPoint.y,0,0);
if(MKMapRectIsNull(flyTo)){
flyTo=pointRect;
}否则{
flyTo=MKMapRectUnion(flyTo,pointRect);
}
}
mapview.visibleMapRect=flyTo;

您是否在真正的设备上运行?您使用的坐标是什么?可能是0,0,就在非洲西部…?我试过真的iPhone和模拟器。它指向海底。您的主要问题(很可能)是,在确定坐标之前,您正在尝试读取
self.mapView.userLocation
(假设showsUserLocation首先是YES),因此坐标将保存为0,0(大西洋)
SetEntityCoordidate
viewDidLoad
中调用,此时地图可能尚未设置用户位置。这不是您的问题,但请注意在
setEntityCoordiate
中,您设置了
user.latitude
两次(而不是在第二行中设置
user.longitude
)。噢,谢谢,这可能就是问题所在,您的建议是什么,如何称呼它?在VIEWDID中,或者在整个其他功能中出现?由@Harri给出的答案将起作用