アプリにユーザーの位置情報がありますが、現在のユーザーの位置情報に注釈をドロップするにはどうすればよいですか? ユーザーの場所の長さと緯度を取得し、そのように注釈をドロップする必要がありますか? またはどうすればいいですか?
2 に答える
2
- 最初に必要なフレームワーク (CoreLocation と MapKit) をインポートします。
- 次に、Objective-C NSObject クラスの注釈を作成します
その .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
.m をセットアップします。
#import "Annotation.h" @implementation Annotation @synthesize coordinate, title, subtitle; @end
設定
viewDidLoad
if ([CLLocationManager locationServicesEnabled]) { locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy: kCLLocationAccuracyBestForNavigation]; [locationManager startUpdatingLocation]; } self.mapView.delegate = self;
セットアップ
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
最も簡単な方法は、に設定showsUserLocation
しYES
てMKMapView
実装することです
- (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 に答える