选定的地图引脚显示相同的坐标

时间:2014-10-19 11:35:59

标签: ios map mkmapview

我正在尝试获取我在地图上选择的每个引脚的坐标。引脚是用户在地图上搜索的本地搜索结果。我有一个问题,我选择的每个引脚都给我相同的值。有人可以帮我这个吗?提前谢谢。

- (IBAction)searchAction:(id)sender {
    [sender resignFirstResponder];
    [self.mapView removeAnnotations:[self.mapView annotations]];
    [self performSearch]; }

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"current"];
    //mypin.pinColor = MKPinAnnotationColorPurple;
    mypin.backgroundColor = [UIColor clearColor];
    UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    mypin.rightCalloutAccessoryView = goToDetail;
    mypin.draggable = NO;
    mypin.highlighted = YES;
    mypin.animatesDrop = TRUE;
    mypin.canShowCallout = YES;
    return mypin;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

    MKPointAnnotation *annotation = view.annotation;
    self.tappedCoord = annotation.coordinate;

    // Longs are the same for every pin I select
    NSLog(@"%f", [[view annotation] coordinate].longitude);
}

- (void)performSearch {
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = _searchText.text;
    request.region = _mapView.region;

    self.matchingItems = [[NSMutableArray alloc] init];

    MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];

    [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if (response.mapItems.count == 0)
            NSLog(@"No Matches");
        else
            for (MKMapItem *item in response.mapItems)
            {
                [self.matchingItems addObject:item];
                MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
                annotation.coordinate = item.placemark.coordinate;
                annotation.title = item.name;
                annotation.subtitle = [NSString stringWithFormat:@"%@, %@ %@",
                                       item.placemark.addressDictionary[@"Street"],
                                       item.placemark.addressDictionary[@"State"],
                                       item.placemark.addressDictionary[@"ZIP"]];

                //NSLog(@"%@", item.placemark.addressDictionary);
                [_mapView addAnnotation:annotation];
            }
    }];
}

0 个答案:

没有答案