Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.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
在MKMap iPhone上显示多个PIN_Iphone - Fatal编程技术网

在MKMap iPhone上显示多个PIN

在MKMap iPhone上显示多个PIN,iphone,Iphone,你好,朋友,在我的应用程序中,我在mapview上显示了多个PIN。我有两个可变数组,第一个用于纬度,第二个用于经度,每个数组有10个值,正如我在方法中显示的以下代码: - (void)showAddressAllBusiness { annotationsarray=[[NSMutableArray alloc] init]; NSLog(@"%d",[listOfLatitude count]); NSLog(@"%d",[listOfLongitude co

你好,朋友,在我的应用程序中,我在mapview上显示了多个PIN。我有两个可变数组,第一个用于纬度,第二个用于经度,每个数组有10个值,正如我在方法中显示的以下代码:

- (void)showAddressAllBusiness {
    annotationsarray=[[NSMutableArray alloc] init];

      NSLog(@"%d",[listOfLatitude count]);
     NSLog(@"%d",[listOfLongitude count]);
     NSLog(@"%d",[listOfPlaceName count]);

    for (int i =0;i < [listOfLatitude count]; i++) {
        MKCoordinateRegion region =  { {0.0, 0.0 }, { 0.0, 0.0 } };
        region.center.latitude = [[listOfLatitude objectAtIndex:i] doubleValue];
        region.center.longitude = [[listOfLongitude objectAtIndex:i] doubleValue];
        region.span.longitudeDelta = 0.1f;
        region.span.latitudeDelta = 0.1f;
        [self.mapViewCityGuide setRegion:region animated:YES];
        [self.mapViewCityGuide setZoomEnabled:YES];
        addAnnotation = [[AddressAnnotation alloc]init];

        addAnnotation.mTitle = [NSString stringWithFormat:@"%@",[listOfPlaceName objectAtIndex:i]];
        addAnnotation.coordinate = region.center;
        [self.mapViewCityGuide addAnnotation:addAnnotation];
        [annotationsarray addObject:addAnnotation];


    }
    NSLog(@"%d",[self.mapViewCityGuide.annotations count]);
    [self centerMap];
}
*请尝试此代码:-*

-(void)viewDidLoad
{

    CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);

    mapView.delegate=self;

    NSMutableArray* annotations=[[NSMutableArray alloc] init];
    MyAnnotation* myAnnotation;

    for (int i = 0; i<[DataArr count]; i++) 
    {
        CLLocationCoordinate2D theCoordinate;

        double a = [[[DataArr objectAtIndex:i] valueForKey:@"lat"] doubleValue];
        double b =[[[DataArr objectAtIndex:i] valueForKey:@"lon"] doubleValue];

        theCoordinate.latitude = a;
        theCoordinate.longitude =b;

        myAnnotation=[[MyAnnotation alloc] init];
        NSString *str=[NSString stringWithFormat:@"%d",i];
        [myAnnotation indexOfAccessibilityElement:str];

        myAnnotation.coordinate=theCoordinate;
        myAnnotation.title=[[DataArr objectAtIndex:i] valueForKey:@"name"];
        myAnnotation.ID_map=i;
        [annotations addObject:myAnnotation];
    }

    for (int i =0; i<[annotations count]; i++) 
    {
        [mapView addAnnotation:[annotations objectAtIndex:i]];
    }

   // Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
    for (id <MKAnnotation> annotation in annotations) 
    {
        NSLog(@"fly to on");
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) 
        {
            flyTo = pointRect;
        } else 
        {
            flyTo = MKMapRectUnion(flyTo, pointRect);
            //NSLog(@"else-%@",annotationPoint.x);
        }
    }

   // Position the map so that all overlays and annotations are visible on screen.
    mapView.visibleMapRect = flyTo;

}

#pragma mark MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

    NSLog(@"didSelectAnnotationView");
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"welcome into the map view annotation");

    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor=MKPinAnnotationColorPurple;


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;



    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
    pinView.leftCalloutAccessoryView = profileIconView;
    [profileIconView release];


    return pinView;
}
-(void)viewDidLoad
{
CLLocation*userLoc=mapView.userLocation.location;
CLLocationCoordinate2D用户坐标=userLoc.coordinate;
NSLog(@“用户纬度=%f”,userCoordinate.latitude);
NSLog(@“用户经度=%f”,userCoordinate.longitude);
mapView.delegate=self;
NSMutableArray*注释=[[NSMutableArray alloc]init];
MyAnnotation*MyAnnotation;
对于(int i=0;i
-(void)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
mapView.delegate=self;
[mapView removeAnnotations:mapView.annotations];
NSMutableArray*注释=[[NSMutableArray alloc]init];
对于(int j=0;j
-(void)viewDidLoad
{

    CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);

    mapView.delegate=self;

    NSMutableArray* annotations=[[NSMutableArray alloc] init];
    MyAnnotation* myAnnotation;

    for (int i = 0; i<[DataArr count]; i++) 
    {
        CLLocationCoordinate2D theCoordinate;

        double a = [[[DataArr objectAtIndex:i] valueForKey:@"lat"] doubleValue];
        double b =[[[DataArr objectAtIndex:i] valueForKey:@"lon"] doubleValue];

        theCoordinate.latitude = a;
        theCoordinate.longitude =b;

        myAnnotation=[[MyAnnotation alloc] init];
        NSString *str=[NSString stringWithFormat:@"%d",i];
        [myAnnotation indexOfAccessibilityElement:str];

        myAnnotation.coordinate=theCoordinate;
        myAnnotation.title=[[DataArr objectAtIndex:i] valueForKey:@"name"];
        myAnnotation.ID_map=i;
        [annotations addObject:myAnnotation];
    }

    for (int i =0; i<[annotations count]; i++) 
    {
        [mapView addAnnotation:[annotations objectAtIndex:i]];
    }

   // Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
    for (id <MKAnnotation> annotation in annotations) 
    {
        NSLog(@"fly to on");
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) 
        {
            flyTo = pointRect;
        } else 
        {
            flyTo = MKMapRectUnion(flyTo, pointRect);
            //NSLog(@"else-%@",annotationPoint.x);
        }
    }

   // Position the map so that all overlays and annotations are visible on screen.
    mapView.visibleMapRect = flyTo;

}

#pragma mark MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

    NSLog(@"didSelectAnnotationView");
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"welcome into the map view annotation");

    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor=MKPinAnnotationColorPurple;


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;



    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
    pinView.leftCalloutAccessoryView = profileIconView;
    [profileIconView release];


    return pinView;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    mapView.delegate=self;




    [mapView removeAnnotations:mapView.annotations];




    NSMutableArray* annotations=[[NSMutableArray alloc] init];
    for (int j=0; j<10; j++) 
    {

        CLLocationCoordinate2D theCoordinate1;

        theCoordinate1.latitude = [[NSString stringWithFormat:@"%i",10+j]doubleValue];

        theCoordinate1.longitude = [[NSString stringWithFormat:@"%i",72+j]doubleValue];

       //my annotation is mkannotation class

        MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];

        myAnnotation1.coordinate=theCoordinate1;

        myAnnotation1.title=[NSString stringWithFormat:@"locatiion %i",j];



        [mapView addAnnotation:myAnnotation1];

        [annotations addObject:myAnnotation1];


    }

    NSLog(@"%d",[annotations count]);

    MKMapRect flyTo = MKMapRectNull;

    for (id <MKAnnotation> annotation in annotations) {
        NSLog(@"fly to on");
        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;


}

#pragma mark MKMapViewDelegate
/*
 - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
 {
 return [kml viewForOverlay:overlay];
 }
 */
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"welcome into the map view annotation");

    if ([annotation isKindOfClass:[MyAnnotation class]])
    {
        static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
        if (!pinView)
        {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            pinView.canShowCallout = YES;
            pinView.animatesDrop = YES;
        }
        else
        {
            pinView.annotation = annotation;
            pinView.tag=((MyAnnotation *)annotation).tag;
            pinView.canShowCallout = YES;
            pinView.animatesDrop = NO;
        }

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame=CGRectMake(0, 0, 14, 16);
        [rightButton setBackgroundImage:[UIImage imageNamed:@"arrow_s5.png"] forState:UIControlStateNormal];

        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        rightButton.tag=((MyAnnotation *)annotation).tag;
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;


        return pinView;
    }

    return nil;
}