Ios 在地图视图中显示多个注释

Ios 在地图视图中显示多个注释,ios,objective-c,mkmapview,mapkit,mkannotationview,Ios,Objective C,Mkmapview,Mapkit,Mkannotationview,我是ios sdk中地图视图的新手。我想在地图视图中显示多个注释 基本上所有的lat和long都来自服务器端 json格式。我正在解析所有的lat和long并将其保存在不同的数组中。 但是如何一次显示所有注释。我只能显示一个注释 下面是我正在使用的单个注释的代码 zoomLocation.latitude = latmpa.doubleValue; zoomLocation.longitude = logmpa.doubleValue; annotationPoint = [[MKPointAn

我是ios sdk中地图视图的新手。我想在地图视图中显示多个注释 基本上所有的lat和long都来自服务器端 json格式。我正在解析所有的lat和long并将其保存在不同的数组中。 但是如何一次显示所有注释。我只能显示一个注释 下面是我正在使用的单个注释的代码

zoomLocation.latitude = latmpa.doubleValue;
zoomLocation.longitude = logmpa.doubleValue;
annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = @"masjid....";
[mapView selectAnnotation:annotationPoint animated:YES];


[mapView addAnnotation:annotationPoint];

mapView.centerCoordinate = annotationPoint.coordinate;

MKCoordinateSpan span;
span.latitudeDelta = 1.5;
span.longitudeDelta = 1.0;
MKCoordinateRegion newRegion;
newRegion.center = zoomLocation;
newRegion.span = span;
[mapView setRegion:newRegion animated:YES];

获取数组中的所有纬度和经度,然后按照user1673099所说的进行
for
循环

并添加以下
zoomtofit
代码,用于通过调用该方法缩放地图视图以覆盖地图视图中的所有注释

[self zoomToFitMapAnnotations:self.mapView];

- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

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

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
[self zoomtofitmanotations:self.mapView];
-(void)ZoomTofitPannotations:(MKMapView*)mapView{
如果([mapView.annotations count]==0)返回;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.纬度=-90;
topLeftCoord.longitude=180;
CLLocationCoordinate2D底部右坐标;
bottomRightCoord.纬度=90;
bottomRightCoord.经度=-180;
用于(mapView.annotations中的id注释){
topLeftCoord.longitude=fmin(topLeftCoord.longitude,annotation.coordinate.longitude);
topLeftCoord.latitude=fmax(topLeftCoord.latitude,annotation.coordinate.latitude);
bottomRightCoord.longitude=fmax(bottomRightCoord.longitude,annotation.coordinate.longitude);
bottomRightCoord.latitude=fmin(bottomRightCoord.latitude,annotation.coordinate.latitude);
}
协调区域;
region.center.latitude=topLeftCoord.latitude-(topLeftCoord.latitude-bottomRightCoord.latitude)*0.5;
region.center.longitude=topLeftCoord.longitude+(bottomRightCoord.longitude-topLeftCoord.longitude)*0.5;
region.span.latitudeDelta=fabs(topLeftCoord.latitude-bottomRightCoord.latitude)*1.1;
region.span.longitudeDelta=fabs(底部右坐标经度-顶部左坐标经度)*1.1;
region=[mapView RegionAtfits:region];
[地图视图设置区域:区域动画:是];
}

获取数组中的所有纬度和经度,然后按照用户1673099所说的那样,进行
for
循环

并添加以下
zoomtofit
代码,用于通过调用该方法缩放地图视图以覆盖地图视图中的所有注释

[self zoomToFitMapAnnotations:self.mapView];

- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

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

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
[self zoomtofitmanotations:self.mapView];
-(void)ZoomTofitPannotations:(MKMapView*)mapView{
如果([mapView.annotations count]==0)返回;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.纬度=-90;
topLeftCoord.longitude=180;
CLLocationCoordinate2D底部右坐标;
bottomRightCoord.纬度=90;
bottomRightCoord.经度=-180;
用于(mapView.annotations中的id注释){
topLeftCoord.longitude=fmin(topLeftCoord.longitude,annotation.coordinate.longitude);
topLeftCoord.latitude=fmax(topLeftCoord.latitude,annotation.coordinate.latitude);
bottomRightCoord.longitude=fmax(bottomRightCoord.longitude,annotation.coordinate.longitude);
bottomRightCoord.latitude=fmin(bottomRightCoord.latitude,annotation.coordinate.latitude);
}
协调区域;
region.center.latitude=topLeftCoord.latitude-(topLeftCoord.latitude-bottomRightCoord.latitude)*0.5;
region.center.longitude=topLeftCoord.longitude+(bottomRightCoord.longitude-topLeftCoord.longitude)*0.5;
region.span.latitudeDelta=fabs(topLeftCoord.latitude-bottomRightCoord.latitude)*1.1;
region.span.longitudeDelta=fabs(底部右坐标经度-顶部左坐标经度)*1.1;
region=[mapView RegionAtfits:region];
[地图视图设置区域:区域动画:是];
}
试试这个

for ( int i=0; i<[yourLatLongarray count]; i++)
{
CLLocationCoordinate2D coord;

    coord.latitude=[[NSString stringWithFormat:@"%@",[yourLatitudeArray objectAtIndex:i]] floatValue];
    coord.longitude=[[NSString stringWithFormat:@"%@",
                      [yourLongitudeArray objectAtIndex:i]] floatValue];
    MKCoordinateRegion region1;
    region1.center=coord;
    region1.span.longitudeDelta=20 ;
    region1.span.latitudeDelta=20;
    [mapview setRegion:region1 animated:YES];

    NSString *titleStr =[namesArr objectAtIndex:i] ;
   // NSLog(@"title is:%@",titleStr);

  MyAnnotation*  annotObj =[[MyAnnotation alloc]initWithCoordinate:coord title:titleStr];
    [mapview addAnnotation:annotObj];

}
试试这个

for ( int i=0; i<[yourLatLongarray count]; i++)
{
CLLocationCoordinate2D coord;

    coord.latitude=[[NSString stringWithFormat:@"%@",[yourLatitudeArray objectAtIndex:i]] floatValue];
    coord.longitude=[[NSString stringWithFormat:@"%@",
                      [yourLongitudeArray objectAtIndex:i]] floatValue];
    MKCoordinateRegion region1;
    region1.center=coord;
    region1.span.longitudeDelta=20 ;
    region1.span.latitudeDelta=20;
    [mapview setRegion:region1 animated:YES];

    NSString *titleStr =[namesArr objectAtIndex:i] ;
   // NSLog(@"title is:%@",titleStr);

  MyAnnotation*  annotObj =[[MyAnnotation alloc]initWithCoordinate:coord title:titleStr];
    [mapview addAnnotation:annotObj];

}

转到
for循环
以向地图添加注释。转到
for循环
以向地图添加注释。MyAnnotation*annotObj=[[MyAnnotation alloc]initWithCoorde:coord title:titleStr];[地图视图添加注释:annotObj];这是什么?MyAnnotation是一个类。。。在这个类中,我们必须实现注释标题和副标题…现在根据NAZIK的说法,我们需要添加他的代码,但它不起任何作用?因为所有的lat和long都太远了,但我需要zoomtofit??好的,谢谢,我已经实现了。很好。现在需要设置缩放级别,以便所有位置只能在一个屏幕上看到,这样用户就不需要滚动MyAnnotation*annotObj=[[MyAnnotation alloc]initWithCoorde:coord title:titleStr];[地图视图添加注释:annotObj];这是什么?MyAnnotation是一个类。。。在这个类中,我们必须实现注释标题和副标题…现在根据NAZIK的说法,我们需要添加他的代码,但它不起任何作用?因为所有的lat和long都太远了,但我需要zoomtofit??好的,谢谢,我已经实现了。很好。现在需要设置缩放级别,以便所有位置只能在一个屏幕上看到,这样用户就不需要滚动添加
-(void)zoomtofitmanotations:(MKMapView*)mapView[self-zoomtofitmanotations:self.mapView]添加
-(void)ZoomTofitPannotations:(MKMapView*)mapView[self-zoomtofitmanotations:self.mapView]