0

我在使用 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];

                 }
             }
 ];
4

2 回答 2

1

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

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

当您告诉位置管理器Allowedlocationmanager调用其委托方法时获取位置时,请参见下文

-(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];

                 }
             }
 ];
}
于 2013-04-26T09:12:46.850 回答
1

供您考虑。将任何图像或 url 或 pin 拖放到用户当前位置很容易。

为此,您必须实施。

- (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;

}
}
于 2013-04-26T09:13:54.673 回答