我创建了一个拥有mapkit
的应用,我可以在地图中添加特定注释,我希望何时点击(点击)任何注释做一个方法或一个动作,但我不知道。
请指导我。我在谷歌搜索了很多次但没有给出正确答案。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
locationManager = [[CLLocationManager alloc] init];
[self GetMyLocation];
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 500)];
mapView.mapType = MKMapTypeStandard;
mapView.zoomEnabled = YES;
mapView.scrollEnabled = YES;
mapView.showsUserLocation = YES;
[mapView.userLocation setTitle:@"I'm Here"];
[mapView.userLocation setSubtitle:@"Rahnova Corpration"];
[self.view addSubview:mapView];
[self addAnnonations];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[self.mapView addGestureRecognizer:longPressGesture];
}
- (void) addAnnonations{
NSArray *title = [[NSArray alloc]initWithObjects:@"Rome",@"Chelsea", nil];
NSArray *subtitle = [[NSArray alloc]initWithObjects:@"Red",@"Blue", nil];
NSArray *latitude = [[NSArray alloc]initWithObjects:@"35.738000",@"35.739901", nil];
NSArray *longitude = [[NSArray alloc]initWithObjects:@"51.310029",@"51.312018", nil];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setObject:title forKey:@"title"];
[dictionary setObject:subtitle forKey:@"subtitle"];
[dictionary setObject:latitude forKey:@"latitude"];
[dictionary setObject:longitude forKey:@"longitude"];
for (int i = 0; i <= 1; i++) {
NSString *Name = [[dictionary objectForKey:@"title"]objectAtIndex:i];
NSString *SubName = [[dictionary objectForKey:@"subtitle"]objectAtIndex:i];
double Lati = [[[dictionary objectForKey:@"latitude"]objectAtIndex:i] doubleValue];
double Long = [[[dictionary objectForKey:@"longitude"]objectAtIndex:i] doubleValue];
[self SetTitle:Name SetSubtitle:SubName SetLatitude:Lati SetLongitude:Long];
}
}
- (void) SetTitle:(NSString *)title SetSubtitle:(NSString *)subtitle SetLatitude:(double)latitude SetLongitude:(double)longitude{
//create coordinate for use annotation
CLLocationCoordinate2D annoLocation;
annoLocation.latitude = latitude;
annoLocation.longitude = longitude;
Annotation *myAnnonation = [[Annotation alloc]init];
myAnnonation.coordinate = annoLocation;
myAnnonation.title = title;
myAnnonation.subtitle = subtitle;
[self.mapView addAnnotation:myAnnonation];
}
- (void) centerLocation{
//create region
MKCoordinateRegion myRegion;
CLLocationCoordinate2D center;
center.latitude = latitudes;
center.longitude = longitudes;
//span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
[mapView setRegion:myRegion animated:YES];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudes = currentLocation.coordinate.longitude;
latitudes = currentLocation.coordinate.latitude;
[self centerLocation];
}
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
答案 0 :(得分:0)