如何在iPhone地图视图的中心添加注释?
我的应用程序中有地图视图,我想在地图视图的中心添加注释pin(红色pin)。如何在iPhone地图视图的中心添加注释?,iphone,annotations,mkmapview,center,Iphone,Annotations,Mkmapview,Center,我的应用程序中有地图视图,我想在地图视图的中心添加注释pin(红色pin)。 现在,当用户滚动地图视图时,pin应根据此中心进行调整。 怎么做? 谢谢如果您想使用实际注释,而不仅仅是位于地图视图中心上方的常规视图,您可以: 使用具有可设置坐标特性的注释类(预定义的MKPointAnnotationclass eg)。这样可以避免在中心更改时删除和添加注释 在viewDidLoad中创建注释 在属性中保留对它的引用,例如centerAnnotation 在地图视图的regionDidChange
现在,当用户滚动地图视图时,pin应根据此中心进行调整。
怎么做?
谢谢如果您想使用实际注释,而不仅仅是位于地图视图中心上方的常规视图,您可以:
- 使用具有可设置坐标特性的注释类(预定义的
class eg)。这样可以避免在中心更改时删除和添加注释MKPointAnnotation
- 在viewDidLoad中创建注释
- 在属性中保留对它的引用,例如centerAnnotation
- 在地图视图的
delegate方法中更新其坐标(和标题等)(确保设置了地图视图的delegate属性)regionDidChangeAnimated
@interface SomeViewController : UIViewController <MKMapViewDelegate> {
MKPointAnnotation *centerAnnotation;
}
@property (nonatomic, retain) MKPointAnnotation *centerAnnotation;
@end
@implementation SomeViewController
@synthesize centerAnnotation;
- (void)viewDidLoad {
[super viewDidLoad];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = mapView.centerCoordinate;
pa.title = @"Map Center";
pa.subtitle = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[mapView addAnnotation:pa];
self.centerAnnotation = pa;
[pa release];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (void)dealloc {
[centerAnnotation release];
[super dealloc];
}
@end
Anna的答案是以标准mapview方式使用注释在地图中保持注释居中的巧妙方法。然而,正如一位评论者指出的,滚动和挤压虽然更好,但仍然显示出明显的滞后,建议的方法是将注释视图添加为mapview的子视图。这是它的样子
@interface SHCenterPinMapViewController () <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) MKPointAnnotation *centerAnnotaion;
@property (strong, nonatomic) MKPinAnnotationView *centerAnnotationView;
@end
@implementation SHCenterPinMapViewController
- (MKPointAnnotation *)centerAnnotaion
{
if (!_centerAnnotaion) {
_centerAnnotaion = [[MKPointAnnotation alloc] init];
}
return _centerAnnotaion;
}
- (MKPinAnnotationView *)centerAnnotationView
{
if (!_centerAnnotationView) {
_centerAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.centerAnnotaion
reuseIdentifier:@"centerAnnotationView"];
}
return _centerAnnotationView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView addSubview:self.centerAnnotationView];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self moveMapAnnotationToCoordinate:self.mapView.centerCoordinate];
}
// These are the constants need to offset distance between the lower left corner of
// the annotaion view and the head of the pin
#define PIN_WIDTH_OFFSET 7.75
#define PIN_HEIGHT_OFFSET 5
- (void)moveMapAnnotationToCoordinate:(CLLocationCoordinate2D) coordinate
{
CGPoint mapViewPoint = [self.mapView convertCoordinate:coordinate toPointToView:self.mapView];
// Offset the view from to account for distance from the lower left corner to the pin head
CGFloat xoffset = CGRectGetMidX(self.centerAnnotationView.bounds) - PIN_WIDTH_OFFSET;
CGFloat yoffset = -CGRectGetMidY(self.centerAnnotationView.bounds) + PIN_HEIGHT_OFFSET;
self.centerAnnotationView.center = CGPointMake(mapViewPoint.x + xoffset,
mapViewPoint.y + yoffset);
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
self.centerAnnotaion.coordinate = mapView.centerCoordinate;
[self moveMapAnnotationToCoordinate:mapView.centerCoordinate];
}
@end
@接口SHCenterPinMapViewController()
@属性(弱,非原子)IBMMapView*mapView;
@属性(强,非原子)MKPointAnnotation*centerAnnotation;
@属性(强,非原子)MKPinAnnotationView*centerAnnotationView;
@结束
@实现SHCenterPinMapViewController
-(MKPointAnnotation*)中心注释
{
如果(!\u中心注释){
_CenterAnnotation=[[MKPointAnnotation alloc]init];
}
返回_中心注释;
}
-(MKPinAnnotationView*)中心注释视图
{
如果(!\u中心注释视图){
_centerAnnotationView=[[MKPinAnnotationView alloc]initWithAnnotation:self.centerAnnotation
reuseIdentifier:@“centerAnnotationView”];
}
返回_centerAnnotationView;
}
-(无效)viewDidLoad
{
[超级视图下载];
self.mapView.delegate=self;
[self.mapView添加子视图:self.centerAnnotationView];
}
-(无效)视图显示:(BOOL)动画
{
[超级视图显示:动画];
[self-moveMapAnnotationToCoordinate:self.mapView.centerCoordinate];
}
//这些是需要偏移屏幕左下角之间距离的常数
//注释视图和销的头部
#定义引脚宽度偏移7.75
#定义销高度偏移量5
-(无效)移动地图注释到坐标:(CLLocationCoordinate2D)坐标
{
CGPoint mapView=[self.mapView convertCoordinate:coordinate toPointToView:self.mapView];
//从偏移视图以考虑从左下角到销头的距离
CGFloat xoffset=CGRectGetMidX(self.centerAnnotationView.bounds)-引脚\宽度\偏移;
CGFloat yoffset=-CGRectGetMidY(self.centerAnnotationView.bounds)+引脚高度偏移;
self.centerAnnotationView.center=CGPointMake(mapViewPoint.x+xoffset,
mapViewPoint.y+yoffset);
}
-(void)地图视图:(MKMapView*)地图视图区域IDChangeAnimated:(BOOL)动画
{
self.centerAnnotation.coordinate=mapView.centerCoordinate;
[self-moveMapAnnotationToCoordinate:mapView.centerCoordinate];
}
@结束
实际上,唯一有趣的是,您必须将MKPinAnnotationView偏移一定量,以考虑左下角和销头之间的距离。我不喜欢在代码中包含这些依赖于资产的常量,所以如果有人能找到更好的方法,我洗耳恭听
我创建了一个github项目,其中有一个地图控制器,它可以完成这项工作,还可以完成一些与使用mapview让用户选择位置相关的其他工作。在这里查看:Swift版本
课堂上:
var centerAnnotation = MKPointAnnotation()
var centerAnnotationView = MKPinAnnotationView()
在viewDidLoad中:
if #available(iOS 9.0, *) {
centerAnnotationView.pinTintColor = customColor
} else {
// Fallback on earlier versions
centerAnnotationView.pinColor = MKPinAnnotationColor.Red
}
self.view.addSubview(centerAnnotationView)
在视图中显示:
self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)
然后:
func moveMapAnnotationToCoordinate(locate : CLLocationCoordinate2D ) {
let mapViewPoint : CGPoint = self.mapView.convertCoordinate(locate, toPointToView: self.mapView)
let pinWidth : CGFloat = 7.75
let pinHeight : CGFloat = 7
let xOffset : CGFloat = CGRectGetMidX(self.centerAnnotationView.bounds) - pinWidth
let yOffset : CGFloat = CGRectGetMidY(self.centerAnnotationView.bounds) - pinHeight
self.centerAnnotationView.center = CGPointMake(mapViewPoint.x - xOffset, mapViewPoint.y - yOffset)
}
要使用位置更改,请添加委派CLLocationManagerDelegate
,然后:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
self.centerAnnotation.coordinate = self.mapView.centerCoordinate
self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)
}
这意味着您正在通过改变中心来改变插针的位置。@Muhammad Zeeshan-是,当用户滚动映射红色插针(注释)时应更改为使用用户可见的新地图视图中心进行调整。尝试将注释放在地图视图的中心。我认为您应该尝试将pin作为子视图添加到地图视图的中心。是的,但我如何获得地图坐标?+1来自我,上面的解决方案不会给出“静止”注释,但对于上面提到的映射解决方案来说,这也是一个完美的基础。我不建议使用实际注释的这种方法。我也不建议添加注释视图作为MKMapView的子视图(最好不要弄乱内部视图层次结构)。相反,在地图视图的父视图中添加一个“pin”视图,但该视图位于其中心上方。这将产生更平滑的效果。看,这太棒了。非常感谢。你知道怎么把变焦锁定在图钉上吗?因此,在缩放时,只需放大管脚,反之亦然。这段代码根本不运行。如果把它清理干净,这将是一个很好的回答
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
self.centerAnnotation.coordinate = self.mapView.centerCoordinate
self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)
}