IOSµØͼ¿ª·¢
¼ò½é
IOSµØͼ°ïÖúÎÒÃǶ¨Î»Î»Öã¬IOSµØͼʹÓà MapKit ¿ò¼Ü¡£
ʵÀý²½Öè
1.´´½¨Ò»¸ö¼òµ¥µÄ View based application
2.Ñ¡ÔñÏîÄ¿Îļþ£¬È»ºóÑ¡ÔñÄ¿±ê£¬È»ºóÌí¼ÓMapKit.framework.
3.Ìí¼Ó Corelocation.framework
4.Ïò ViewController.xib Ìí¼ÓµØͼ²é¿´ºÍ´´½¨ ibOutlet ²¢ÇÒÃüÃûΪmapView¡£
5.ͨ¹ý File-> New -> File... ->¡¡Ñ¡Ôñ Objective C class´´½¨Ò»¸öеÄÎļþ£¬µ¥»÷ÏÂÒ»²½
6. sub class of Ϊ NSObject£¬Àà×÷ÃüÃûΪMapAnnotation
7.Ñ¡Ôñ´´½¨
8.¸üÐÂMapAnnotation.h £¬ÈçÏÂËùʾ
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject<MKAnnotation>
@property (nonatomic, strong) NSString *title;
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)title andCoordinate:
¡¡(CLLocationCoordinate2D)coordinate2d;
@end
9.¸üÐÂMapAnnotation.m £¬ÈçÏÂËùʾ
#import¡¡MapAnnotation.h
@implementation MapAnnotation
-(id)initWithTitle:(NSString *)title andCoordinate:
(CLLocationCoordinate2D)coordinate2d{
¡¡¡¡¡¡¡¡self.title = title;
¡¡¡¡self.coordinate =coordinate2d;
¡¡¡¡return self;
}
@end
10.¸üÐÂViewController.h £¬ÈçÏÂËùʾ
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<MKMapViewDelegate>{
¡¡¡¡MKMapView *mapView;
}
@end
11.¸üÐÂViewController.m £¬ÈçÏÂËùʾ
#import¡¡ViewController.h
#import¡¡MapAnnotation.h @interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad{
¡¡ [super viewDidLoad];
¡¡ mapView = [[MKMapView alloc]initWithFrame:
¡¡ CGRectMake(10, 100, 300, 300)];
¡¡ mapView.delegate = self;
¡¡ mapView.centerCoordinate = CLLocationCoordinate2DMake(37.32, -122.03);
¡¡ mapView.mapType = MKMapTypeHybrid;
¡¡ CLLocationCoordinate2D location;
¡¡ location.latitude = (double) 37.332768;
¡¡ location.longitude = (double) -122.030039;
¡¡ // Add the annotation to our map view
¡¡ MapAnnotation *newAnnotation = [[MapAnnotation alloc]
¡¡ initWithTitle:@ Apple Head quaters¡¡andCoordinate:location];
¡¡ [mapView addAnnotation:newAnnotation];
¡¡ CLLocationCoordinate2D location2;
¡¡ location2.latitude = (double) 37.35239;
¡¡ location2.longitude = (double) -122.025919;
¡¡ MapAnnotation *newAnnotation2 = [[MapAnnotation alloc]
¡¡¡¡initWithTitle:@ Test annotation¡¡andCoordinate:location2];
¡¡ [mapView addAnnotation:newAnnotation2];
¡¡ [self.view addSubview:mapView];
}
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
¡¡ MKAnnotationView *annotationView = [views objectAtIndex:0];
¡¡ id <MKAnnotation> mp = [annotationView annotation];
¡¡ MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance
¡¡ ([mp coordinate], 1500, 1500);
¡¡ [mv setRegion:region animated:YES];
¡¡ [mv selectAnnotation:mp animated:YES];
}
- (void)didReceiveMemoryWarning{
¡¡¡¡[super didReceiveMemoryWarning];
¡¡¡¡// Dispose of any resources that can be recreated.
}
@end
ѧϰÁ´½Ó£º
http://www.wyzc.com/ios/?tg=5009295340
ѧϰ½»Á÷Ⱥ£º317140762 |