我在 MKMapView 上有一个三角形多边形,我想快速重绘或锚定在手机上的特定位置。
这个多边形将始终指向手机的顶部,但地图本身将旋转到提供的标题。目前我想出的唯一方法是删除覆盖然后重新添加它。问题在于它非常不稳定。
我的下一个想法是在地图上添加一个三角形图像,并根据需要调整地图的大小,这个三角形图像可以准确地可视化添加为地图叠加层时的多边形。这几乎可以工作,但不是很准确。由于某种原因,它几乎从不响应纬度缩放级别的变化和其他一些问题。
我的方法是:
- (void)setMapVisibleRect {
//we have to mock the heading to 0 so that the distances show up in lat/long correctly
//create a new instance of the cone model with a fake heading of 0 for calculations
ConePolygonModel *conePolygonModel = [[ConePolygonModel alloc] init:[self.userLocationModel getLocation].coordinate
withHeading:0
withLatLongMultiplier:[self.conePolygonModel getLatLongMultiplier]
withDistanceMultiplier:[self.conePolygonModel getDistanceMultiplier]];
//reset the map heading to 0
[self.mapView.camera setHeading:0];
//top left setup
CLLocationCoordinate2D topLeftCoordinate;
topLeftCoordinate.latitude = [[[conePolygonModel getXCoordinates] objectAtIndex:1] doubleValue];
topLeftCoordinate.longitude = [[[conePolygonModel getYCoordinates] objectAtIndex:1] doubleValue];
CLLocation *topLeftLocation = [[CLLocation alloc] initWithLatitude:topLeftCoordinate.latitude longitude:topLeftCoordinate.longitude];
//top right setup
CLLocationCoordinate2D topRightCoordinate;
topRightCoordinate.latitude = [[[conePolygonModel getXCoordinates] objectAtIndex:2] doubleValue];
topRightCoordinate.longitude = [[[conePolygonModel getYCoordinates] objectAtIndex:2] doubleValue];
CLLocation *topRightLocation = [[CLLocation alloc] initWithLatitude:topRightCoordinate.latitude longitude:topRightCoordinate.longitude];
//center setup
CLLocationCoordinate2D topCenterCoordinate = [conePolygonModel midpointBetweenCoordinate:topLeftCoordinate andCoordinate:topRightCoordinate];
CLLocation *topCenterLocation = [[CLLocation alloc] initWithLatitude:topCenterCoordinate.latitude longitude:topCenterCoordinate.longitude];
//set distances
CLLocationDistance latitudeDistance = [topLeftLocation distanceFromLocation:topRightLocation];
CLLocationDistance longitudeDistance = [topCenterLocation distanceFromLocation:[self.userLocationModel getLocation]];
//set the map zoom
NSLog(@"zoom levels: %f %f", latitudeDistance, longitudeDistance);
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance([self.userLocationModel getLocation].coordinate, latitudeDistance, longitudeDistance);
[self.mapView setRegion:viewRegion];
//move the map to the actual heading
[self.mapView.camera setHeading:self.currentHeading];
}
无论地图旋转如何,有没有办法让覆盖始终指向屏幕顶部?缩放地图以使叠加层在屏幕上始终占据相同的大小也很好,但它不如另一点重要。