CLPlacemark多个地方

时间:2014-11-05 19:59:07

标签: ios objective-c mapkit

我使用下面的代码在地图上搜索地点,但每次我搜索添加到地图的新引脚时,我想在搜索地点时删除以前的引脚并添加新引脚

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.searchMap resignFirstResponder];

//inistantiate geoLocation
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder geocodeAddressString:self.searchMap.text completionHandler:^(NSArray *placemarks, NSError *error) {

    //Mark location and center
    CLPlacemark *placemark=[placemarks lastObject];
    if(placemark){
    MKCoordinateRegion region;

    CLLocationCoordinate2D newlocation=[placemark.location coordinate];
    region.center=[(CLCircularRegion *)placemark.region center];

    // Drop pin


    NSString *address=[NSString stringWithFormat:@"%@", ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO)];
    MKPointAnnotation *annotation=[[MKPointAnnotation alloc]init];
    [annotation setCoordinate:newlocation];
    [annotation setTitle:address];
    [self.mapview addAnnotation:annotation];

    //scroll to search result
    MKMapRect mr=[self.mapview visibleMapRect];
    MKMapPoint pt=MKMapPointForCoordinate([annotation coordinate]);
    mr.origin.x=pt.x-mr.size.width*0.5;
    mr.origin.y=pt.y-mr.size.height*0.5;
    [self.mapview setVisibleMapRect:mr animated:YES];

    aLng=newlocation.longitude;
    aLat=newlocation.longitude;
        searchMap.text=address;}
}];
}

0 个答案:

没有答案