KkMIW
KkMIW

Reputation: 1112

How to show multiple location points on mkmapview

How to get multiple location pin points on mkmapview.

on my current location I view my friends register with me.

i have got there location latitude and longitude values.

Below are dummy values for understanding.

{
friendA:
{
latitude: "12.1234";
longtitude: "12.1234";
},

friendB:
{
latitude: "12.1234";
longtitude: "12.1234";
},

friendD:
{
latitude: "12.1234";
longtitude: "12.1234";
}

}

Want to show all my friends on mapView with there location Placing with PinPointer Marker.png

//Created the mapview.

mapView = [[MKMapView alloc] init];
mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-60);
mapView.delegate = self;
mapView.mapType = MKMapTypeStandard;
mapView.showsUserLocation = YES;
[self.view addSubview:mapView];

MKAnnotation MarkerPin Pointer

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationView";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

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

    annotationView.image = [UIImage imageNamed:@"Marker.png"];
    annotationView.annotation = annotation;

    return annotationView;
}

Upvotes: 1

Views: 2873

Answers (1)

nburk
nburk

Reputation: 22751

You need to loop through your friends (do you have them stored in an array or something??) and then call addAnnotation for each item.

Something like:

for (Friend *friend in friends){
   CLLocationCoordinate2D coordinate;
   coordinate.latitude = latitude.doubleValue;
   coordinate.longitude = longitude.doubleValue;
   MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
   myAnnotation.coordinate = coordinate;
   [mapView addAnnotation:annotation];
}

Then, the method that you already implemented, mapView:viewForAnnotation: gets called automatically and the annotation is added. Check out this tutorial for more info:http://www.devfright.com/mkpointannotation-tutorial/

Upvotes: 0

Related Questions