gpt4 book ai didi

iphone - 通过按下按钮在 map 上放置图钉——iOS

转载 作者:行者123 更新时间:2023-11-28 17:37:52 26 4
gpt4 key购买 nike

虽然我的最终项目不是另一个我把应用程序停在哪里的项目。我认为这是开始很好地掌握 MKMapLocations 的好地方。

好的,这样我就可以设置图钉、查看自己并让我的当前位置显示在标签中。

我不能做的是:

一个。按一下按钮
1)存储用户当前位置。 2)在当前用户位置放置一个图钉(红色)。 (所以即使用户(蓝色)移动,新的 pin 仍然存在)

B.在一个单独的按钮上 1) 从 map 上清除用户丢弃的图钉(红色)。

我似乎无法为按钮中的新图钉设置 map 注释。数字发生变化并且 map 不会刷新(我猜)以显示我的图钉,其中包含或不包含正确的坐标集。

这是我目前所拥有的。 (没有窃笑):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];
ann1.title=@"Here";
ann1.subtitle=@"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];
ann1.title=@"You Parked Here";
ann1.subtitle=@"";
ann1.coordinate= location1.center;
[mapView addAnnotation:ann1];

}
@end

最佳答案

当您获得委托(delegate)回调 - (void)locationUpdate:(CLLocation *)location 时,为什么不使用 CLLocationCoordinate2D 来存储您的当前位置?没有理由使用 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 作为坐标属性来创建和添加注释。

至于删除注释 - 请参阅 Mundi 的回答。

关于iphone - 通过按下按钮在 map 上放置图钉——iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9233099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com