Using MapKit, But default location is in CA, USA. Need to change it to default
Okay using mapkit. The user can move and drag the red pin. However when i run it, the default location is in CA. When i want it to where the user currently is. Here is the code.
#import "MapKitDragAndDropViewController.h"
#import "DDAnnotation.h"
#import "DDAnnotationView.h"
@interface MapKitDragAndDropViewController ()
- (void)coordinateChanged_:(NSNotification *)notification;
@end
@implementation MapKitDragAndDropViewController
@synthesize mapView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = 37.810000;
theCoordinate.longitude = -122.477989;
DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease];
annotation.title = @"Drag to Move Pin";
annotation.subtitle = [NSString stringWithFormat:@"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
[self.mapView addAnnotation:annotation];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// NOTE: This is optional, DDAnnotationCoordinateDidChangeNotification only fired in iPhone OS 3, not in iOS 4.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coordinateChanged_:) name:@"DDAnnotationCoordinateDidChangeNotification" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// NOTE: This is optional, DDAnnotationCoordinateDidChangeNotification only fired in iPhone OS 3, not in iOS 4.
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"DDAnnotationCoordinateDidChangeNotification" object:nil];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (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;
self.mapView.delegate = nil;
self.mapView = nil;
}
- (void)dealloc {
mapView.delegate = nil;
[mapView release];
[super dealloc];
}
#pragma mark -
#pragma mark DDAnnotationCoordinateDidChangeNotification
// NOTE: DDAnnotationCoordinateDidChangeNotification won't fire in iOS 4, use -mapView:annotationView:didChangeDragState:fromOldState: instead.
- (void)coordinateChanged_:(NSNotification *)notification {
DDAnnotation *annotation = notification.object;
annotation.subtitle = [NSString stringWithFormat:@"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
}
#pragma mark -
#pragma mark MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
if (oldState == MKAnnotationViewDragStateDragging) {
DDAnnotation *annotation = (DDAnnotation *)annotationView.annotation;
annotation.subtitle = [NSString stringWithFormat:@"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString * const kPinAnnotationIdentifier = @"PinIdentifier";
MKAnnotationView *draggablePinView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (draggablePinView) {
draggablePinView.annotation = annotation;
} else {
// Use class method to create DDAnnotationView (on 开发者_如何学CiOS 3) or built-in draggble MKPinAnnotationView (on iOS 4).
draggablePinView = [DDAnnotationView annotationViewWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier mapView:self.mapView];
if ([draggablePinView isKindOfClass:[DDAnnotationView class]]) {
// draggablePinView is DDAnnotationView on iOS 3.
} else {
// draggablePinView instance will be built-in draggable MKPinAnnotationView when running on iOS 4.
}
}
return draggablePinView;
}
You need to have code similar to the following:
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
[lm startUpdatingLocation];
This uses the CLLocationManager object to locate the current position of the user
精彩评论