ios - 当前位置的注释图钉未出现
<p><p>我试图利用 map 工具包通过 MKMapView 及其委托(delegate)方法 <code>didUpdateUserLocation</code> 显示我当前的位置,但是当我放大并非常接近时,我会看到多个图钉。然后经过一些建议,我意识到,每次更新位置,都会重新创建引脚。所以,我从委托(delegate)方法中取出了我的添加注释代码。</p>
<p>这是我的代码-</p>
<pre><code>#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController ()<MKMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate> {
MKPointAnnotation *myAnnotation;
}
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) IBOutlet UITextField *searchTextField;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation MapViewController
- (void)viewDidLoad {
;
self.mapView.delegate=self;
self.mapView.showsUserLocation=YES;
self.locationManager = [ init];
self.locationManager.delegate = self;
if () {
;
}
myAnnotation = [init];
myAnnotation.coordinate = self.mapView.userLocation.coordinate;
myAnnotation.title = @"Current Location";
;
}
- (void)didReceiveMemoryWarning {
;
}
#pragma mark-
#pragma mark- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
animated:YES];
}
@end
</code></pre>
<p>但现在没有别针了。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>=> 当您设置 mapView.showsUserLocation = YES 时,MKMapView 会自动放置 MKUserLocation 类的注释:</p>
<p>您可以通过在 mapView:viewForAnnotation 中执行此操作来将此注释的默认 View 替换为您想要的任何默认注释 View :</p>
<pre><code>- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (]) {
// replace the following with code to generate and return custom user position annotation view
return customAnnotationView;
}
//*** other code ***//
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 当前位置的注释图钉未出现,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34692534/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34692534/
</a>
</p>
页:
[1]