繁体   English   中英

iOS-MKMapView-Buggy注解

[英]iOS - MKMapView - Buggy Annotation

当按下时,我有一个按钮,希望它获得用户位置并在其上放置注释。 但是似乎我按过一次,它在海中添加了注释,然后出现了我的用户位置(有效)。 我第二次按下它,它在我的位置上添加了注释。

码:

mapView.showsUserLocation = YES;

MKCoordinateRegion newRegion;

newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude;
newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude;

newRegion.span.latitudeDelta = 0.0004f;
newRegion.span.longitudeDelta = 0.0004f;

[mapView setRegion: newRegion animated: YES];


CLLocationCoordinate2D coordinate;
coordinate.latitude = mapView.userLocation.location.coordinate.latitude;
coordinate.longitude = mapView.userLocation.location.coordinate.longitude;

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

[annotation setCoordinate: coordinate];
[annotation setTitle: @"Your Car"];
[annotation setSubtitle: @"Come here for pepsi"];

[mapView addAnnotation: annotation];
[mapView setZoomEnabled: YES];
[mapView setScrollEnabled: YES];

听起来好像您在CLLocationManager找到用户位置之前要求它。

通过设置mapView.showsUserLocation = YES; mapview开始使用locationManager异步定位用户,因此可能不会立即找到正确的位置。

您需要设置一个自定义CLLocationManager并在按下按钮时开始更新其位置,然后在找到位置时在其委托中设置注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM