iOS MKMAPVIEW快速在地图上重新覆盖



我在mkmapview上有一个三角多边形,我想快速重新绘制或在手机上锚定在特定位置。

此多边形始终将指向手机的顶部,但地图本身将旋转到提供的标题。目前,我想出的唯一方法是删除覆盖层,然后重新添加它。问题在于它是非常 choppy。

我的下一个想法就是在地图上添加一个三角形图像,并根据需要调整地图大小。对于添加作为地图覆盖的多边形的三角形映像是准确的可视化多边形的准确可视化。此几乎有效,但不是很准确。由于某种原因,它几乎永远不会响应纬度变焦级别和其他一些问题的变化。

我的方法是:

- (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文件并将其分配给您的MKAnnotationView属性mapView:viewForAnnotation:中的image属性。另外,您可以在运行时使用核心图形来编程绘制三角形并将其变成uiimage。您可以一次使用核心图形渲染图像,并保留对UIImage对象的引用,这样您就不会每次都称为viewForantation。

最新更新