[英]How to get annotation indexpath in mapview
我有個問題。
我有一個mapView,並且我的mapView中有很多注釋。
如何知道我選擇注釋的indexPath
。
我找到了函數didSelectAnnotationView
但是我不知道如何使用它。
這是我的代碼:
BOOL firstLocationReceived;
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *currentLocation = locations.lastObject; //current location
if(firstLocationReceived == NO)
{
MKCoordinateRegion region = _shopMapView.region;
region.center = currentLocation.coordinate;
region.span.latitudeDelta = 0.05;
region.span.longitudeDelta = 0.05;
[_restaurantMapView setRegion:region animated:true];
//Add Annotation
for (int i = 0; i < self.items.count ; i++)
{
MKPointAnnotation *annotation = [MKPointAnnotation new];
NSDictionary *item = self.items[i];
CLLocationDegrees latitude = [item[@"latitude"] doubleValue];
CLLocationDegrees longitude = [item[@"longitude"] doubleValue];
annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
annotation.title = item[@"name"];
annotation.subtitle = item[@"address"];
[_shopMapView addAnnotation:annotation];
firstLocationReceived == YES;
}
}
}
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
NSString *identifier = @"shop";
MKAnnotationView *result = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(result ==nil)
{
result = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
}
else
{
result.annotation = annotation;
}
result.canShowCallout = true;
UIImage *annotationViewImage = [UIImage imageNamed:@"point.png"];
result.image = annotationViewImage;
result.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:annotationViewImage];
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
result.rightCalloutAccessoryView = button;
return result;
}
謝謝!
您可以通過這種方式獲取所選的注釋索引,
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init]; annotation = view.annotation; NSString * selectedTitle = [NSString stringWithFormat:@"%@",annotation.title]; NSInteger index = [[self.items valueForKey:@"name"] indexOfObject:selectedTitle]; //Considering self.items as NSDictionary consist of all annotation names. NSLog(@"You selected index: %ld",(long)index); }
迅速
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { var annotation = MKPointAnnotation() if let anAnnotation = view.annotation as? MKPointAnnotation { annotation = anAnnotation } let selectedTitle = "\\(annotation.title ?? "")" let index: Int? = (items["name"] as? NSArray)?.index(of: selectedTitle) //Considering self.items as NSDictionary consist of all annotation names. print("You selected index: \\(Int(index))") }
獲取所選注釋點的annotation title
,並在我們所有名稱的數組中找到它的索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.