2014-01-30 98 views
0

我有一個MKMapView上的三角形多邊形,我想快速重繪或錨定在手機上的特定位置。iOS 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]; 
} 

有沒有辦法保持疊加總是指向屏幕的頂部,無論在地圖旋轉的?地圖的縮放使覆蓋層總是在屏幕上佔據相同的大小也是很好的,但它不像其他點那麼重要。

+0

因此,要仔細檢查,無論用戶如何操作地圖,都希望三角形在用戶的屏幕上始終具有相同的物理尺寸和相同的旋轉角度? –

回答

0

嘗試將此作爲MKAnnotationView而不是覆蓋圖實現,因爲您希望三角形始終具有相同的大小和方向,但仍然附加到地圖座標系中的特定點。

最簡單的方法是創建一個PNG文件,並將其UIImage分配給mapView:viewForAnnotation:委託方法中MKAnnotationViewimage屬性。或者,您可以使用Core Graphics以編程方式繪製三角形並在運行時將其變爲UIImage。您可以使用Core Graphics渲染圖像一次,並保留對UIImage對象的引用,以便每次調用viewForAnnotation時不重繪它。