iOS MKMapView快速重绘地图上的叠加层

时间:2014-01-30 17:57:01

标签: ios mkmapview cllocation cllocationdistance

我在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];
}

有没有办法让覆盖始终指向屏幕顶部而不管地图旋转?地图的缩放使得叠加在屏幕上总是占用相同的大小也不错,但它没有其他点那么重要。

1 个答案:

答案 0 :(得分:0)

尝试将其实现为MKAnnotationView而不是叠加,因为您希望三角形始终具有相同的大小和方向,但仍然附加到地图坐标系中的特定点。

最简单的方法是创建一个PNG文件,并将其UIImage分配给image委托方法中MKAnnotationView的{​​{1}}属性。或者,您可以使用Core Graphics以编程方式绘制三角形并在运行时将其转换为UIImage。您可以使用Core Graphics渲染图像一次并保留对UIImage对象的引用,这样您就不会在每次调用viewForAnnotation时重绘它。