2012-02-10 28 views
1

虽然我的最终项目不是另一个我在哪里停放应用程序。我认为这将是一个很好的开始掌握MKMapLocations的好地方。在按钮按下地图上放下Pin - iOS

好吧,我可以设置引脚,看看我自己,并获得当前位置以显示在标签中。

什么我不能做的是:

A.在按钮按下
1)存储用户的当前位置。 2)在当前用户位置放置一个别针(红色)。 (因此,即使用户(蓝色)移动,新引脚也会保留)

B.在单独的按钮上按下 1)清除用户丢弃引脚(红色)的引脚不在地图上。

我似乎无法为按钮中新引脚设置贴图注释。数字发生变化,地图不刷新(我猜测)显示我的引脚与其中的正确组合。

这是我到目前为止。 (NO窃笑):P

#import "Find_My_CarViewController.h" 
#import "MapAnnotation.h" 

@implementation Find_My_CarViewController 

@synthesize CLController; 
@synthesize hereIamLat; 
@synthesize hereIamLong; 
@synthesize mapView; 

- (void)dealloc 
{ 
    [hereIamLat release]; 
    [hereIamLong release]; 
    [CLController release]; 
    [super dealloc]; 
} 

- (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 viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    CLController = [[CoreLocationController alloc] init]; 
    CLController.delegate = self; 
    [CLController.locMgr startUpdatingLocation]; 
} 

- (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); 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    MKCoordinateRegion region; 
    region.center.latitude =40.798356; 
    region.center.longitude= -81.411158; 
    region.span.longitudeDelta=0.3; 
    region.span.latitudeDelta =0.3; 
    [mapView setRegion:region animated:YES]; 
} 

- (void)locationUpdate:(CLLocation *)location 
{ 
    latitudeLabel.text = [NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude]; 
    longitudeLabel.text = [NSString stringWithFormat:@"LONGITUDE: %f", location.coordinate.longitude]; 

    //hereIamLat = [NSString stringWithFormat:@"%g", location.coordinate.latitude]; 
    //hereIamLong = [NSString stringWithFormat:@"%g", location.coordinate.longitude]; 
} 

- (void)locationError:(NSError *)error 
{ 
//speedLabel.text = [error description]; 
} 

-(IBAction)PushToMark 
{ 
    NSScanner *strLat = [NSScanner scannerWithString:latitudeLabel.text]; 
    double dblLat; 
    [strLat scanDouble:&dblLat]; 
    NSScanner *strLong = [NSScanner scannerWithString:longitudeLabel.text]; 
    double dblLong; 
    [strLong scanDouble:&dblLong]; 

    NSLog(@"lat: %f",dblLat); 
    NSLog(@"long: %f",dblLong); 
    MKCoordinateRegion location1; 
    location1.center.latitude =dblLat; 
    location1.center.longitude= dblLong; 
    location1.span.longitudeDelta=0.1; 
    location1.span.latitudeDelta =0.1; 

    MapAnnotation *ann1 =[[[MapAnnotation alloc] init] autorelease]; 
    [email protected]"Here"; 
    [email protected]"I AM"; 
    ann1.coordinate= location1.center; 
    [mapView addAnnotation:ann1]; 
} 

@end 

编辑: 好了,所以我去了这条路线的感谢。

#import "LocationTestViewController.h" 
#import "CoreLocation/CoreLocation.h" 
#import "MapAnnotation.h" 

@implementation LocationTestViewController 
@synthesize locationManager; 
@synthesize mapView; 

- (void)dealloc 
{ 
[mapView release]; 
[locationManager release]; 
[super dealloc]; 
} 

- (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 viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
} 
*/ 

- (void)viewDidUnload 
{ 
[self setMapView:nil]; 
[self setLocationManager:nil]; 
[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); 
} 

- (IBAction)getLocation:(id)sender { 


locationManager = [[CLLocationManager alloc] init]; 
locationManager.distanceFilter=kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
[locationManager startUpdatingLocation]; 

[mapView setMapType:MKMapTypeStandard]; 
[mapView setZoomEnabled:YES]; 
[mapView setScrollEnabled:YES]; 
MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}}; 
region.center.latitude = locationManager.location.coordinate.latitude; 
region.center.longitude = locationManager.location.coordinate.longitude; 
region.span.longitudeDelta = 0.005f; 
region.span.latitudeDelta = 0.005f; 
[mapView setRegion:region animated:YES]; 
[mapView setDelegate:sender]; 

MKCoordinateRegion location1; 
location1.center.latitude =locationManager.location.coordinate.latitude; 
location1.center.longitude= locationManager.location.coordinate.longitude; 
location1.span.longitudeDelta=0.1; 
location1.span.latitudeDelta =0.1; 

MapAnnotation *ann1 =[[[MapAnnotation alloc] init] autorelease]; 
[email protected]"You Parked Here"; 
[email protected]""; 
ann1.coordinate= location1.center; 
[mapView addAnnotation:ann1]; 

} 
@end 

回答

0

为什么不使用CLLocationCoordinate2D来存储你当前的位置当你得到代理回调- (void)locationUpdate:(CLLocation *)location?没有真正的理由使用MKCoordinateRegionNSScanner

而是执行此操作:

- (void)locationUpdate:(CLLocation *)location 
{ 
    latitudeLabel.text = [NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude]; 
    longitudeLabel.text = [NSString stringWithFormat:@"LONGITUDE: %f", location.coordinate.longitude]; 

    self.myLocation = location; // a property that stores the current location 
} 

然后你- (IBAction)pushToMark您可以创建并添加使用self.myLocation作为坐标性质的注解。

至于删除注释 - 见蒙迪的答案。

0

在您的视图控制器的@property中存储对创建的注释的引用。要删除,请使用

[mapView removeAnnotation:self.ann1]; 
self.ann1 = nil; 

此外,您扫描标签中的字符串以找出坐标的策略也很奇怪。为什么不只是使用位置,你也可以存储在一个变量中?