开发者

i have latitude and Longitude how can i display Annotation in the mapview?

i have got the Latitude and Longitude fr开发者_如何学JAVAom XML Parsing how can i show MapAnnotation(pin) on the Map,Please help me....


  1. Create a new class that implements the MKAnnotation protocol. This will hold the lat/long, and will also have a title and description which will be displayed if you select the annotation after it has been rendered on your map.
  2. The controller for the view that will display the map will need to implement the - - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation; method of the MKMapViewDelegate protocol. The code in this method will look something like the code at the bottom (apologies for the poor formatting, I couldn't get right in here or at the bottom).

  3. Then at some point in your controller code you will need to call something along the lines of [self.mapView addAnnotation: annotation]; where annotation is an instance of your custom annotation class created in step 1, with the lat/long set etc.

  4. Finally, so that the viewForAnnotation method gets called correctly, and is something that is easy to miss, in interface builder, make sure that you set the delegate outlet of the MKMapView to be your controller (that implements the MKMapViewDelegate protocol.

    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    
      static NSString *AnnotationViewIdentifier = @"annotationViewIdentifier";
    
      MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: AnnotationViewIdentifier];
    
      if (annotationView == nil) {
        annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: AnnotationViewIdentifier] autorelease];
    
    // This is all optional and depends on your requirements
        annotationView.animatesDrop = NO;
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annotationView.enabled = YES;
        annotationView.pinColor = MKPinAnnotationColorGreen;
      }
      return annotationView;
    }
    


First you want to create MKAnnotation Protocol class (Here MyAnnotation class implements the MKAnnotation Protocol).This class should be includes lat, long, title, subtitle and also whatever you want.

Second, In your view controller, where you want to display the pin and you will implement this code,

 AnnObj = [[MyAnnotation alloc] init];

 AnnObj.latitude =  [[latitude objectAtIndex:storyIndex] floatValue];

 AnnObj.longitude = [[longitude objectAtIndex:storyIndex] floatValue];

 MKCoordinateRegion region;

 region.center = location;

 MKCoordinateSpan span;

 region.center.latitude = [[latitude objectAtIndex:storyIndex] floatValue];   

 region.center.longitude = [[longitude objectAtIndex:storyIndex] floatValue]; 

 span.latitudeDelta = 0.0005;  

 span.longitudeDelta = 0.0005;

 region.span = span;

 [mapview setRegion:region animated:TRUE];

 AnnObj.title = titleString;

 AnnObj.subTitle = subTitleString;

 NSString * titleString = [[buildingNames objectAtIndex:storyIndex]    stringByReplacingOccurrencesOfString:@"\n\t" withString:@""];

 [eventPoints addObject:AnnObj];

 [mapview addAnnotations:eventPoints];

 Third, Implement the MKAnnotation delegate method,

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

    if (lmapView.userLocation == annotation){

        return nil;
      }

    MKAnnotationView* myCusAnn =  (MKAnnotationView*)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"eventview"];

    if(myCusAnn == nil)
    {
      myCusAnn = [[[ MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"eventview"] autorelease];

    }

    myCusAnn.canShowCallout = YES;

    [myCusAnn setEnabled:YES];

    return myCusAnn;

  }

I hope it will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜