【问题标题】:show user location on a map and stop location在地图上显示用户位置并停止位置
【发布时间】:2013-01-25 20:54:02
【问题描述】:

这是我的目标: 每次他打开具有 MapView 的视图然后停止位置以节省电池时,我都必须在地图上显示用户位置和他的地址。到目前为止我可以做到,但是如果用户移动到另一个区域并进入视图(离开后),位置不会改变。

这是我的代码:

- (void)viewDidLoad
{
    mapView.mapType = MKMapTypeStandard;
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [super viewDidLoad];

}

- (void)viewDidAppear:(BOOL)animated{

    if(!self.locationManager){
        self.locationManager = [[CLLocationManager alloc] init];
    }

    [self.locationManager startUpdatingLocation];

}

-(void)viewDidDisappear:(BOOL)animated{

    self.locationManager = nil;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    MKCoordinateRegion viewRegion =
    MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
    [mapView setRegion:adjustedRegion animated:YES];

    [manager stopUpdatingLocation];

    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc]
                                   initWithCoordinate:newLocation.coordinate];
    geocoder.delegate = self;
    [geocoder start];    
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {

    MapLocation *annotation = [[MapLocation alloc] init];
    annotation.streetAddress = placemark.thoroughfare;
    annotation.city = placemark.locality;
    annotation.state = placemark.administrativeArea;
    annotation.zip = placemark.postalCode;
    annotation.coordinate = geocoder.coordinate;

    [mapView addAnnotation:annotation];

    geocoder.delegate = nil;
}

- (void)openCallout:(id<MKAnnotation>)annotation {

    [mapView selectAnnotation:annotation animated:YES];
}

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    static NSString *placemarkIdentifier = @"Map Location Identifier";
    if ([annotation isKindOfClass:[MapLocation class]]) {
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
        if (annotationView == nil)  {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
        }
        else{
            annotationView.annotation = annotation;
        }
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image = [UIImage imageNamed:@"rescue"];
        [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];

        return annotationView;

    }
    return nil;
}

我在另一个视图上有一个 CLLocationManager,它可以在没有地图的情况下获取用户位置(这里的位置工作得很好)。如果在该视图上开始位置并返回带有地图的视图,则它会更新。为什么会这样?

有什么想法吗?

【问题讨论】:

  • locationManager 是在viewDidAppear 中创建的,但它的delegate 是在viewDidLoad 中设置的(这将在viewDidAppear 之前发生)。 locationManager 不是 nil 负载如何?无论如何,一个快速的解决方法可能是在viewDidAppear 中创建代理和desiredAccuracy 之后也设置它。

标签: iphone ios objective-c mapkit core-location


【解决方案1】:
- (void)loadView {
    MKMapView *mv = [[MKMapView alloc]init];
    mapView.showsUserLocation=YES;
    mv.delegate=self;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
    mapView.showsUserLocation=NO;
} 

【讨论】:

    【解决方案2】:

    很好地显示在地图上

    MKMapView *mv = [[MKMapView alloc]init];
    CLLocationCoordinate2D c = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
    [mv setCenterCoordinate:c animated:YES];
    

    并停止更新

    [locationManager stopUpdatingLocation];
    

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多