簡體   English   中英

在地圖上顯示用戶位置並停止位置

[英]show user location on a map and stop location

這是我的目標:每次打開具有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,它獲取沒有地圖的用戶位置(這里的位置工作得很好)。 如果在該視圖上啟動該位置並返回到包含地圖的視圖,則會更新該視圖。 為什么?

任何想法?

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

以及在地圖上顯示它

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

並停止更新

[locationManager stopUpdatingLocation];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM