간단한 큰 바늘 지도 표시 연습(xib 없음)

7353 단어 지도.
4.2로 만든 빈 모듈러 클래스에 추가된 UIViewController 클래스의 이름은 맵View입니다.(정식 명칭 방식대로라면 MapView여야 합니다. 양해해 주십시오.)
다음은 추가된 코드입니다
mapView.h
#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>

#import <QuartzCore/QuartzCore.h>



@interface mapView : UIViewController <CLLocationManagerDelegate>

{

    CLLocationManager *locManager;

    MKMapView *map;

    CLLocation *bestLocation;

}

@property (retain) CLLocationManager *locManager;

@property (retain) CLLocation *bestLocation;

@end


 mapView.m
//
// mapView.m
// Map
//
// Created by SuperHappy on 12-1-31.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "mapView.h"
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
@implementation mapView
@synthesize locManager;
@synthesize bestLocation;

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Location manager error :%@",[error description]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (!self.bestLocation) {
self.bestLocation = newLocation;
}
else if(newLocation.horizontalAccuracy < bestLocation.horizontalAccuracy)
self.bestLocation = newLocation;
map.region = MKCoordinateRegionMake(self.bestLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f));
map.showsUserLocation = YES;
map.zoomEnabled = NO;

}
- (void)findme
{
// disable right button
self.navigationItem.rightBarButtonItem = nil;

// Search for the best location
self.bestLocation = nil;
self.locManager.delegate = self;
[self.locManager startUpdatingLocation];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (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


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];
map = [[MKMapView alloc]initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:map];
[map release];

self.navigationController.navigationBar.tintColor = [UIColor redColor];

self.locManager = [[[CLLocationManager alloc] init] autorelease];
if (!self.locManager.locationServicesEnabled)
{
NSLog(@"User has opted out of location services");
return;
}
else
{
// User generally allows location calls
self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Find Me", @selector(findme));
}

}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

좋은 웹페이지 즐겨찾기