请建议我在mkmap中显示x个注释点

时间:2014-07-05 05:40:22

标签: objective-c xcode mkmapview mapkit mkannotation

早上好,我对目标很新 - 现在我正在开发我的第一个应用程序。它是一款车辆追踪应用。我收到了lat-amp;的x号码。长期从json服务。现在我显示了一个注释但我需要显示我从服务中收到的所有注释点我在这里搜索了一整天来找到这个,但是我找不到显示x-num的注释。所以请使用一些示例代码来建议我。在此先感谢...我的代码是......

- (void)viewDidLoad
{
   [super viewDidLoad];
    //MAP VIEW WebService
    NSString *urlMapString=[NSString stringWithFormat:@"http://www.logix.com/logix_webservice/map.php?format=json&truckno=%@",nam2];
    NSURL *urlMap=[NSURL URLWithString:urlMapString];
    NSData *dataMap=[NSData dataWithContentsOfURL:urlMap];
    NSError *errorMap;
    NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap]; NSArray *resultsMap = [jsonMap valueForKey:@"posts"];
    NSArray *resMap = [resultsMap valueForKey:@"post"];
    NSArray *latitudeString=[resMap valueForKey:@"latitude"];
     if([resMap count]){
    NSString *latOrgstring = [latitudeString objectAtIndex:0];
    double latitude=[latOrgstring doubleValue];
    NSArray *longitudeString=[resMap valueForKey:@"longitude"];
    NSString *longOrgstring = [longitudeString objectAtIndex:0];
    double longitude=[longOrgstring doubleValue];
    NSString *ignation=[[resMap valueForKey:@"ignition"]objectAtIndex:0];
    i=[ignation intValue];
    //MAP VIEW Point
    MKCoordinateRegion myRegion;
    //Center
    CLLocationCoordinate2D center;
    center.latitude=latitude;
    center.longitude=longitude;
    //Span
    MKCoordinateSpan span;
    span.latitudeDelta=0.01f;
    span.longitudeDelta=0.01f;
    myRegion.center=center;
    myRegion.span=span;
    //Set our mapView
    [MapViewC setRegion:myRegion animated:YES];
    //Annotation
    //1.create coordinate for use with the annotation
    CLLocationCoordinate2D wimbLocation;
    wimbLocation.latitude=latitude;
    wimbLocation.longitude=longitude;
    Annotation * myAnnotation= [Annotation alloc];
    CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
        addressOutlet=[dictionary valueForKey:@"Street"];
        City=[dictionary valueForKey:@"City"];
        State=[dictionary valueForKey:@"State"];
        myAnnotation.coordinate=wimbLocation;
        if (addressOutlet!=NULL&&City!=NULL)
        {
            myAnnotation.title=addressOutlet;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
        }
        else if (addressOutlet==NULL&&City!=NULL)
        { 
            myAnnotation.title=City;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
        }
        else if (addressOutlet!=NULL&&City==NULL)
        {
            myAnnotation.title=addressOutlet;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@", State];
        }
        else if(addressOutlet==NULL&&City==NULL)
        {

            myAnnotation.title=State;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@",State];

        }
        [self.MapViewC addAnnotation:myAnnotation];
    }];
     }
}

1 个答案:

答案 0 :(得分:1)

请通过导入进行个别课程并使用。 现在用   - (ID)initWithCoordinate:(CLLocationCoordinate2D)C; 和它的.m文件      - (ID)initWithCoordinate:(CLLocationCoordinate2D)C     {      坐标= C;     } 现在在任何地方调用此类并调用此方法并发送您的位置坐标。 然后将其对象添加到NSArray中。 现在调用[YourMapView addAnnotations:arrayOfAnnotations]; 你会得到你想要的东西。在我的情况下,我希望你会发现它支持。

现在在mapView上添加注释:

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

if ([annotation isKindOfClass:[pinAnnotation class]])
{
    static NSString *defaultPinID = @"com.ABC.pin";//Your unique identifier anything
    MKAnnotationView *pinView = nil;

    if(!pinView)
    {
        pinView = (MKAnnotationView *)
       [self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        pinView = [[MKAnnotationView alloc] initWithAnnotation:
annotation reuseIdentifier:defaultPinID] ;
return pinView;
 }
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView* pin =
(MKAnnotationView*) [mapV dequeueReusableAnnotationViewWithIdentifier: 
 AnnotationViewID];

if ( pin == nil ) {

    pin = [(MKAnnotationView*) [MKAnnotationView alloc] 
    initWithAnnotation:annotation   reuseIdentifier: AnnotationViewID] ;

    pin.canShowCallout = YES;

}
else 
{

    [pin setAnnotation: annotation];
}

((MKUserLocation *)annotation).title = @"You are here";

return pin;
}