我在我的應用程序中有MAP視圖,我想在地圖視圖中心添加註釋針(紅色針)。
現在當用戶滾動地圖視圖引腳應該按照中心來調整。
如何做到這一點?
感謝如何在iPhone中添加地圖視圖中心的註釋?
回答
如果你想用一個實際的註解,而不是定位在地圖視圖的中心,只是一個普通視圖,您可以:
- 使用註解類與設定的座標屬性(預-defined
MKPointAnnotation
class eg)。這避免了在中心改變時必須移除和添加註釋。 - 在viewDidLoad中創建註釋
- 保持對它的引用它的屬性,說centerAnnotation
- 更新其座標(和標題等),在地圖視圖中的
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
現在,這將移動註釋,但並不順利。如果你需要的註釋更平滑地移動,你可以添加一個UIPanGestureRecognizer
和UIPinchGestureRecognizer
地圖視圖,並更新了手勢處理機註釋:
// (Also add UIGestureRecognizerDelegate to the interface.)
// In viewDidLoad:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
panGesture.delegate = self;
[mapView addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
pinchGesture.delegate = self;
[mapView addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//let the map view's and our gesture recognizers work at the same time...
return YES;
}
感謝它的工作完美.. – dks1725
從我的+1,仍然高於解決方案並沒有給出「仍然」的註釋,但也是一個完美的基礎以及提到在地圖解決方案上方查看。 –
有人可以用swift寫這段代碼:)? –
安娜的回答是聰明的做法,以保持在中心的註解使用標準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
唯一真正有趣的事情不在於你有一定的量,以抵消MKPinAnnotationView佔左下角和銷頭之間的距離。我不喜歡在代碼中使用這些與資產相關的常量,因此如果任何人都可以找到更好的方法來實現這一點,那麼我完全可以接受。
我創建了一個帶有地圖控制器的github項目,它可以做到這一點,以及其他一些與使用mapview來讓用戶選擇一個位置相關的東西。看看這裏:https://github.com/scottrhoyt/CenterPinMapViewController
這太好了。謝謝。有關如何將縮放鎖定在引腳上的任何想法?因此,縮放時只能放大引腳,反之亦然。 http://stackoverflow.com/questions/25577813/make-mkmapview-only-zoom-in-on-the-centercoordinate/25577949#25577949 –
斯威夫特版本
在類:
在viewDidLoad中:
if #available(iOS 9.0, *) {
centerAnnotationView.pinTintColor = customColor
} else {
// Fallback on earlier versions
centerAnnotationView.pinColor = MKPinAnnotationColor.Red
}
self.view.addSubview(centerAnnotationView)
在viewDidAppear:
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)
}
- 1. 如何在地圖視圖中添加註釋在Iphone
- 2. 如何在地圖視圖中添加多個註釋
- 3. 如何在地圖的註釋標註中添加自定義視圖
- 4. 在oracle的視圖中添加註釋
- 5. 在iPhone中的地圖中添加路線註釋
- 6. iPhone地圖視圖自定義註釋
- 7. 添加地圖註釋
- 8. 如何在地圖中添加註釋符號的文本
- 9. 如何在iPhone中的地圖中創建自定義註釋?
- 10. 如何手動添加註釋以在iOS中映射視圖?
- 11. 地圖註釋添加圖片
- 12. 在地層圖外添加註釋
- 13. 如何顯示從表視圖到地圖視圖的註釋?
- 14. 圖中添加註釋,以MATLAB圖
- 15. 我無法使用緯度和經度在地圖視圖中添加註釋
- 16. 如何在Mapview中使用圖片和標籤在iPhone中添加註釋?
- 17. 向地圖視圖添加註釋時出錯
- 18. 如何在圖表中添加圖表js註釋?
- 19. 如何在地圖疊加上添加註釋ios
- 20. iPhone - 爲我自己的地圖添加註釋
- 21. 如何在mkmap中添加註釋中的圖像
- 22. 我們如何在iPhone地圖視圖中將圖像添加爲多段線?
- 23. 如何將自定義註釋添加到Swift 2中帶有圖像的地圖視圖中?
- 24. 如何在同一個地圖視圖中將不同的圖像添加到註釋
- 25. 設置地圖視圖中心座標後,自定義註釋視圖正從其超級視圖中移除
- 26. 使用地圖任務添加註釋。
- 27. 在Google地圖中添加街景視圖-Android和iPhone
- 28. 如何在iphone應用中的Mapview中添加多個註釋
- 29. MapKit iPhone開發 - 添加註釋到地圖 - iOS版SDK
- 30. 將註釋添加到Google地圖中的形狀疊加
這意味着你被改變中心改變銷的位置。 –
@Muhammad Zeeshan - 是的,當用戶滾動地圖時,紅色的別針(註解)應該改變以適應用戶可見的地圖視圖的新中心。 – dks1725
嘗試將註釋放到地圖中心 –