2013-10-09 2 views
0

많은 링크를 확인하고 많이 봤습니다. 나는 그 코드들도 시도했지만 성공하지 못했다. 그래서 나는 그것을 여기에 올리고있다.사용자 업데이트 유지 방법 ios MapKit을 사용하여 파란 점

사용자 위치 업데이트와 관련하여 도움을 받으실 수 있습니다.

  • 나는 그냥 체크 showUserLocation의 사용자의 현재 위치를 표시하는 방법을 알고있다.

  • 위치 유지 관리를위한 위치 관리 및 위임이 있음을 알고 있습니다.

사람이 위치 관리자 업데이트 대리인의지도를 업데이트하는 코드를 약간만 시도했습니다. 하지만 그것은 나를 위해 작동하지 않습니다. 그것은 단지 사용자의 현재 위치에 푸른 포인트를 보여 주지만 내가 움직이면 계속 업데이트되지 않습니다.

그래서 어느 누구도 내가해야 할 일부터 시작할 수 있습니까? 사용자의 현재 위치를 표시하는 방법은 언제 어디서나 사용자가 이동할 때지도에서 업데이트를 유지합니다.

답변

1

헤더 파일에 CLLocationManagerDelegate 및 MKMapviewDelegate를 추가하는 것을 잊지 마세요이 방법

  1. 를 사용하여 보시기 바랍니다.

  2. 먼저 재 장전지도 방법 또는 viewDidLoad에 또는 viewWillappear 방법에 아래 라인을 구현합니다

    [mapView setShowsUserLocation:YES] 
    
  3. 과 같이 아래 방법 변경보다 :

    (MKAnnotationView *) mapView:(MKMapView *)mapsView viewForAnnotation:(id <MKAnnotation>) annotation 
    { 
    
         if ([annotation isKindOfClass:[MKUserLocation class]]) 
    
         return nil; 
    } 
    
1

당신은 할 수 있습니다 사용하지 않고 혼자서 그것 showUserLocation

당신은 MKAnnotation

@interface MyAnnotation : NSObject<MKAnnotation>{ 
    CLLocationCoordinate2D _coordinate; 
    NSString *_title; 
    NSString *_subtitle; 
} 
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle; 
@end 
@implementation MyAnnotation 
- (NSString *)title {return _title;} 
- (NSString *)subtitle {return _subtitle;} 
- (CLLocationCoordinate2D)coordinate {return _coordinate;} 
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {_coordinate = newCoordinate;} 
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle { 
    if (self = [super init]) { 
     _title = title.copy; 
     _subtitle = subtitle.copy; 
     _coordinate = coordinate; 
    } 
    return self; 
} 
@end 

만들기 구현 현재 위치를 얻을 수 CLLocationManager를 사용하여지도보기

에 추가해야 1 UIViewController

@interface MyViewController() <CLLocationManagerDelegate>{ 
    CLLocationManager *locationManager; 
    MyAnnotation *userAnnotation; 
} 
@property (strong, nonatomic) MKMapView *mapView; 
@end 

@implementation MyViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 
    [self.view addSubview:self.mapView]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 
} 

#pragma mark - DELEGATE 
//for iOS 5 
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 
    // [manager stopUpdatingLocation]; 
    if (newLocation) { 
     if (!userAnnotation) { 
      userAnnotation = [[MyAnnotation alloc] 
           initWithCoordinate:newLocation.coordinate 
           title:@"I'm here" 
           subtitle:nil]; 
      [self.mapView addAnnotation:userAnnotation]; 
     } else { 
      [self.mapView removeAnnotation:userAnnotation]; 
      [userAnnotation setCoordinate:newLocation.coordinate]; 
      [self.mapView addAnnotation:userAnnotation]; 
     } 
    } 
} 
//for iOS 6 
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    CLLocation *loc = locations.lastObject; 
    [self locationManager:manager didUpdateToLocation:loc fromLocation:nil]; 
} 
- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error { 
    //open GPS services please 
} 
- (void)dealloc { 
    [locationManager stopUpdatingLocation]; 
} 
@end 

참고 :

  • remov을 기억 새 주석을받은 후 다시 추가하십시오. 위치
  • iOS 5 및 ARC를 사용하십시오.