[英]iOS MKMapView quickly redraw overlay on map
我在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];
}
有沒有一種方法可以使疊加層始終指向屏幕頂部,而與地圖旋轉無關? 縮放地圖以使覆蓋層始終占據屏幕上相同的大小也是不錯的選擇,但它不如其他點重要。
嘗試將其實現為MKAnnotationView而不是疊加層,因為您希望三角形始終具有相同的大小和方向,但仍要附加到地圖坐標系中的特定點。
最簡單的方法是創建一個PNG文件,並將其UIImage分配給mapView:viewForAnnotation:
委托方法中MKAnnotationView
的image
屬性。 或者,您可以使用Core Graphics在運行時以編程方式繪制三角形並將其變成UIImage。 您可以使用Core Graphics渲染一次圖像,並保留對UIImage對象的引用,這樣就不必在每次調用viewForAnnotation時都重新繪制它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.