在当前位置和单独位置mkmapview上删除地标

时间:2013-04-26 08:44:38

标签: ios xcode mkmapview mkannotation mkplacemark

我在使用MKMapView时遇到了一些问题!我是映射的新手! 我想做什么:

Mapview会在用户的当前位置以及所选客户的地址上加载和删除地标。 这段代码在所选客户的地址上删除了一个地标标记,但我似乎无法在用户的当前位置删除它!

任何想法为什么?

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:postcodeForMap
             completionHandler:^(NSArray* placemarks, NSError* error){
                     NSLog(@"Co-ordinate geocode is %@", [placemarks objectAtIndex:0]);
                 for (CLPlacemark* aPlacemark in placemarks)

                 {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                     MKPlacemark *mPlacemark = [[MKPlacemark alloc] initWithCoordinate:mapView.userLocation.location.coordinate addressDictionary:nil];



                     MKCoordinateRegion region = mapView.region;
                     region.center = placemark.region.center;
                     region.span.longitudeDelta /= 8.0;
                     region.span.latitudeDelta /= 8.0;

                     [mapView setRegion:region animated:YES];
                     [mapView addAnnotation:placemark];
                     [mapView addAnnotation:mPlacemark];

                 }
             }
 ];

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取用户位置

-(void)viewDidLoad{
    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;//THIS IS IMPORTANT
    [locationManager startUpdatingLocation];
}

如果Allowed locationmanager调用其委托方法,请告诉位置管理员获取位置

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
          //newLocation is your current Location

   self.userLocation=newLocation;//Once you got the location generate the places..
   [self generatePlaces];
   [manager stopUpdatingLocation];

}

-(void)generatePlaces{
  CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:postcodeForMap
             completionHandler:^(NSArray* placemarks, NSError* error){
                     NSLog(@"Co-ordinate geocode is %@", [placemarks objectAtIndex:0]);
                 for (CLPlacemark* aPlacemark in placemarks)

                 {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                     //EDITED
                     MKPlacemark *mPlacemark = [[MKPlacemark alloc] initWithCoordinate:self.userLocation.coordinate addressDictionary:nil];



                     MKCoordinateRegion region = mapView.region;
                     region.center = placemark.region.center;
                     region.span.longitudeDelta /= 8.0;
                     region.span.latitudeDelta /= 8.0;

                     [mapView setRegion:region animated:YES];
                     [mapView addAnnotation:placemark];
                     [mapView addAnnotation:mPlacemark];

                 }
             }
 ];
}

答案 1 :(得分:1)

供您考虑。可以轻松地将任何图像或网址或引脚放到用户当前位置。

为此,你必须实施。

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

if (annotation == mapView.userLocation)
{

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    annView.pinColor = MKPinAnnotationColorRed;
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;

    return annView;

}
}