如何在IOS中添加地图中的图钉和注释?

时间:2014-06-07 07:43:51

标签: ios objective-c json

如何在ios中的Map中添加Pin和Annotation?

1 个答案:

答案 0 :(得分:2)

#import <MapKit/MapKit.h>   

@property (strong, nonatomic) IBOutlet MKMapView *mapvie;   //insert the one map view
<。>文件中的

NSMutableArray *allocations=[[NSMutableArray alloc]init];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cbapcertprep.com/service/index.php?/Download_vehicletrack/DownloadTrackData/35170"]];

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSArray *largeArray= [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

NSArray *smallArray = [largeArray subarrayWithRange:NSMakeRange(0, 20)]; //this is range for what the result u need



for (int i=0; i<[smallArray count]; i++) {
     CLLocationCoordinate2D annotationCoord;

    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationCoord.latitude = [[[smallArray objectAtIndex:i]objectForKey:@"latitude"]doubleValue ];
    annotationCoord.longitude = [[[smallArray objectAtIndex:i]objectForKey:@"longitude"] doubleValue];

    annotationPoint.coordinate = annotationCoord;
    annotationPoint.title = [[smallArray objectAtIndex:i]objectForKey:@"address"];

    [allocations addObject:annotationPoint];


    MKCoordinateRegion myRegion;
    MKCoordinateSpan span;

    span.latitudeDelta=THE_SPAN;
    span.longitudeDelta=THE_SPAN;

    myRegion.center=annotationCoord;
    myRegion.span=span;

    [self.mapvie setRegion:myRegion animated:YES];

}


[self.mapvie addAnnotations:allocations];