我有一个带有一个引脚的mapView和用户位置。我已经设置了VisibleMapRect
,因此可以看到两个引脚。
我遇到的问题是我使用MKUserTrackingModeFollowWithHeading
来显示用户移动动作,当我设置这样的矩形时,动作非常不稳定。我相信这是因为用户位置引脚不在地图的中心。
如何让两个引脚都可见,但将用户位置保持在中心位置?
这是我的代码,它启动位置并设置矩形。 [locationManager startUpdatingLocation];
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = kCLHeadingFilterNone;
[locationManager startUpdatingHeading];
//set the view to fit both the pins
MKMapPoint annotationPoint = MKMapPointForCoordinate(MapView.userLocation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
for (id <MKAnnotation> annotation in MapView.annotations)
{
if (![annotation isKindOfClass:[MKUserLocation class]] ) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
NSLog(@"%f",zoomRect.size.height);
NSLog(@"%f",zoomRect.size.width);
[MapView setVisibleMapRect:zoomRect animated:NO];
}
我已经尝试注释掉设置矩形的部分,并且用户的移动是平滑的,所以我可以告诉我的问题是这段代码。
我也尝试使用
设置矩形之后的中心点[MapView setCenterCoordinate:MapView.userLocation.coordinate animated:YES];
这似乎不起作用。在此之后设置它可以使得其他引脚不再显示。
感谢您的帮助。
答案 0 :(得分:2)
显示所有当前位置的注释使用以下方法
- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> annotation in mapView.annotations) {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
我试过这个方法.. 可能对你有帮助.. 谢谢:))
答案 1 :(得分:2)
我使用这种方法:
- (void)setRegionFromCoordinates:(NSArray *)waypoints animated:(BOOL)animated {
if (waypoints != nil) {
CLLocationDegrees maxX = -DBL_MAX;
CLLocationDegrees maxY = -DBL_MAX;
CLLocationDegrees minX = DBL_MAX;
CLLocationDegrees minY = DBL_MAX;
for (NSUInteger i=0; i < waypoints.count; i++) {
CLLocationCoordinate2D currentLocation = [waypoints objectAtIndex:i];
MKMapPoint mapPoint = MKMapPointForCoordinate(currentLocation);
if (mapPoint.x > maxX) {
maxX = mapPoint.x;
}
if (mapPoint.x < minX) {
minX = mapPoint.x;
}
if (mapPoint.y > maxY) {
maxY = mapPoint.y;
}
if (mapPoint.y < minY) {
minY = mapPoint.y;
}
}
if (maxX != -DBL_MAX && minX != DBL_MAX) {
MKMapRect mapRect = MKMapRectMake(minX,minY,maxX-minX,maxY-minY);
[map setVisibleMapRect:mapRect edgePadding:UIEdgeInsetsMake(50.f, 50.f, 50.f, 50.f) animated:animated];
}
}
}