玩转Map Kit (part2)

原文地址:http://blog.objectgraph.com/index.php/2009/04/03/iphone-sdk-30-playing-with-map-kit-part-2/

说明:本人E文水平有限,如果有不足之处还请指正

-----------------------------------------------------------

今天我将拿出一些时间介绍一下有关Map kit的更多的内容。并且会多加一些注释说明

先上张图:

玩转Map Kit (part2)_第1张图片

如下有一些类需要我们熟悉

MKMapView

这个是我们显示地图组件的主要类。它继承自UIView.因此你可以初始化并把它添加到你的MainView上。

MKPinAnnotationView

如果你不希望子类化MKAnnotationView,那么这个类是很有用的。它提供了一个可用的具有相互作用的针注解(针头注释)。

MKReverseGeoCoder

这个类提供一个地标(PlackMark)再通过异步回调得到一个坐标(Coordinate)。

MKPlacemark

代表了一个标注。

 

另外有一些委托我们也会用到

MKMapViewDelegate

这是一个重要的委托,不管什么时候你在地图组件上添加标注,有一个回调方法你需要实现以显示标注视图.

MKReverseGeocoderDelegate

与MKReverseGeoCoder配合使用,获得多个地标(PlaceMarks)

 

这是我的MainViewController.h的代码:

#import "FlipsideViewController.h" #import <MapKit/MapKit.h> #import <MapKit/MKAnnotation.h> #import <MapKit/MKReverseGeocoder.h> @interface MainViewController : UIViewController <FlipsideViewControllerDelegate,MKReverseGeocoderDelegate,MKMapViewDelegate> { MKMapView *mapView; MKReverseGeocoder *geoCoder; MKPlacemark *mPlacemark; IBOutlet UISegmentedControl *mapType; } - (IBAction)changeType:(id) sender; @end

我的MainViewController.m的代码如下:

#import "MainViewController.h" #import "MainView.h" @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization } return self; } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; mapView=[[MKMapView alloc] initWithFrame:self.view.bounds]; mapView.showsUserLocation=TRUE; mapView.mapType=MKMapTypeStandard; mapView.delegate=self; /*Region and Zoom*/ MKCoordinateRegion region; MKCoordinateSpan span; span.latitudeDelta=0.2; span.longitudeDelta=0.2; CLLocationCoordinate2D location=mapView.userLocation.coordinate; location.latitude=40.814849; location.longitude=-73.622732; region.span=span; region.center=location; /*Geocoder Stuff*/ geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location]; geoCoder.delegate=self; [geoCoder start]; [mapView setRegion:region animated:TRUE]; [mapView regionThatFits:region]; [self.view insertSubview:mapView atIndex:0]; } - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller { [self dismissModalViewControllerAnimated:YES]; } - (IBAction)showInfo { FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; controller.delegate = self; controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:controller animated:YES]; [controller release]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } - (IBAction)changeType:(id)sender{ if(mapType.selectedSegmentIndex==0){ mapView.mapType=MKMapTypeStandard; } else if (mapType.selectedSegmentIndex==1){ mapView.mapType=MKMapTypeSatellite; } else if (mapType.selectedSegmentIndex==2){ mapView.mapType=MKMapTypeHybrid; } } - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{ NSLog(@"Reverse Geocoder Errored"); } - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{ NSLog(@"Reverse Geocoder completed"); mPlacemark=placemark; [mapView addAnnotation:placemark]; } - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; annView.animatesDrop=TRUE; return annView; } @end

 

你可能感兴趣的:(interface,initialization)