2

アプリにユーザーの位置情報がありますが、現在のユーザーの位置情報に注釈をドロップするにはどうすればよいですか? ユーザーの場所の長さと緯度を取得し、そのように注釈をドロップする必要がありますか? またはどうすればいいですか?

4

2 に答える 2

2
  1. 最初に必要なフレームワーク (CoreLocation と MapKit) をインポートします。
  2. 次に、Objective-C NSObject クラスの注釈を作成します
  3. その .h をセットアップします。

    #import <Foundation/Foundation.h>
    
    #import <CoreLocation/CoreLocation.h>
    
    #import <MapKit/MapKit.h>
    
    @interface Annotation : NSObject <MKAnnotation> 
    
    @property (nonatomic) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    
    @end
    
  4. .m をセットアップします。

    #import "Annotation.h"
    
    @implementation Annotation
    @synthesize coordinate, title, subtitle;
    
    @end
    
    1. 設定viewDidLoad

         if ([CLLocationManager locationServicesEnabled]) {
      
         locationManager = [[CLLocationManager alloc] init];
      
         [locationManager setDelegate:self];
      
         [locationManager setDesiredAccuracy: kCLLocationAccuracyBestForNavigation];
      
         [locationManager startUpdatingLocation];
      
         }
      
        self.mapView.delegate = self; 
      
    2. セットアップdidUpdateToLocation

         // IMPORT ANNOTATION
      
         - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      
      
         [locationManager stopUpdatingLocation];
      
         double miles = 3.10686;
         double scalingFactor = ABS((cos(2 * M_PI * newLocation.coordinate.latitude / 360.0)));
      
         MKCoordinateSpan span;
      
         span.latitudeDelta = miles/69.0;
         span.longitudeDelta = miles/(scalingFactor * 69.0);
      
         MKCoordinateRegion region;
         region.span = span;
         region.center = newLocation.coordinate;
      
         [self.mapView setRegion:region animated:YES];
      
         Annotation *annot = [[Annotation alloc] init];
         annot.coordinate = newLocation.coordinate;
      
         [self.mapView addAnnotation:annot];
      
           }
      
于 2012-08-12T01:07:08.950 に答える
0

最も簡単な方法は、に設定showsUserLocationYESMKMapView実装することです

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
    [mapView setRegion:region animated:NO];
}

MKMapViewDelegateユーザーの場所が見つかったときにマップビューをその場所に移動するようにします。

これにより、マップアプリと同様に、ユーザーの場所のマップビューに青い点が表示されます。

于 2012-08-12T02:51:37.497 に答える