使用这 4 个类 - 它就像一个魅力!
//MapViewController.h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *buttonBack;
IBOutlet MKMapView *mapView;
IBOutlet UILabel *labelTitle;
NSString *lon,*lat,*nickName;
BOOL isFirstLaunch;
}
@property(nonatomic,retain) NSString *lon,*lat,*nickName;
@property(nonatomic,retain) IBOutlet UIButton *buttonBack;
@property(nonatomic,retain) IBOutlet UILabel *labelTitle;
@property(nonatomic,retain) IBOutlet MKMapView *mapView;
-(IBAction)buttonBackAction:(UIButton *)_btn;
-(void)zoomToFitMapAnnotations;
@end
// // MapViewController.m //
#import "MapViewController.h"
#import "Annotation.h"
@implementation MapViewController
@synthesize buttonBack,mapView,labelTitle;
@synthesize lon,lat,nickName;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
-(IBAction)buttonBackAction:(UIButton *)_btn{
[self.navigationController popViewControllerAnimated:1];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
CLLocationManager* locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
NSLog(@"MapViewController");
labelTitle.text = @"anscacorona.blogspot.in"
CLLocationCoordinate2D location;
location.latitude = [self.lat floatValue];
location.longitude = [self.lon floatValue];
Annotation *annotation = [[Annotation alloc] init];
annotation.theCoordinate = location;
annotation.theTitle = [NSString stringWithFormat:@"%@",labelTitle.text];
[mapView addAnnotation:annotation];
[annotation release];
mapView.showsUserLocation = 1;
[self zoomToFitMapAnnotations];
[super viewDidLoad];
}
// Shows the location of both users. called when the device location changes to move the map accordinly. necessary ONLY once when the window is opened. afterwards the user can move the map as he choises.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (isFirstLaunch) {
CLLocationCoordinate2D topLeftCoord;
CLLocationCoordinate2D bottomRightCoord;
topLeftCoord.longitude = fmin([self.lon floatValue], newLocation.coordinate.longitude);
topLeftCoord.latitude = fmax([self.lat floatValue], newLocation.coordinate.latitude);
bottomRightCoord.longitude = fmax([self.lon floatValue], newLocation.coordinate.longitude);
bottomRightCoord.latitude = fmin([self.lat floatValue], newLocation.coordinate.latitude);
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.5; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; // Add a little extra space on the sides
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
isFirstLaunch = NO;
}
}
-(void)zoomToFitMapAnnotations
{
if([mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
CLLocationCoordinate2D bottomRightCoord;
topLeftCoord.longitude = fmin(mapView.userLocation.location.coordinate.longitude, [self.lon floatValue]);
topLeftCoord.latitude = fmax(mapView.userLocation.location.coordinate.latitude, [self.lat floatValue]);
bottomRightCoord.longitude = fmax(mapView.userLocation.location.coordinate.longitude, [self.lon floatValue]);
bottomRightCoord.latitude = fmin(mapView.userLocation.location.coordinate.latitude, [self.lat floatValue]);
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
CLLocationCoordinate2D userCoord = {[self.lat floatValue],[self.lon floatValue]};
region.center = userCoord;
region.span.latitudeDelta = 0.05f;//fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = 0.05f;//fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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.
}
#pragma mark - View Lifecycle
-(void)viewWillAppear:(BOOL)animated{
isFirstLaunch=YES;
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
//注释类-Annotation.h
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation>{
CLLocationCoordinate2D theCoordinate;
NSString *theTitle;
NSString *restId;
NSString *theSubtitle;
}
@property(nonatomic,retain) NSString *restId;
@property(nonatomic,retain) NSString *theTitle;
@property(nonatomic,retain) NSString *theSubtitle;
@property CLLocationCoordinate2D theCoordinate;
@end
//注解.m
#import "Annotation.h"
@implementation Annotation
@synthesize theCoordinate,theTitle,theSubtitle,restId;
- (CLLocationCoordinate2D)coordinate;
{
return theCoordinate;
}
// required if you set the MKPinAnnotationView's "canShowCallout" property to YES
- (NSString *)title
{
return theTitle;
}
// optional
- (NSString *)subtitle
{
return theSubtitle;
}
- (void)dealloc
{
[super dealloc];
}
@end