2

我的应用程序在以下覆盖区域中获取数据。我想使用这些边界值在地图上绘制矩形。你有什么主意吗?提前致谢

左上界:30.439217,-95.899668;

右上边界:30.443953,-94.685679;

左下边界:28.930662,-95.908595;

右下边界:28.930061,-94.690486;

4

3 回答 3

5

如果你想用线条画一些基本的东西,你可以使用这个 MKPolyline

试试下面的代码,确保您的委托与您的地图视图对象连接

- (void) drawRect
{

    CLLocation *coordinates1 =  [[CLLocation alloc] initWithLatitude:30.439217 longitude:-95.899668];

    CLLocation *coordinates2 =  [[CLLocation alloc] initWithLatitude:30.443953 longitude:-94.685679];

    CLLocation *coordinates3 =  [[CLLocation alloc] initWithLatitude:28.930061 longitude:-94.690486];

    CLLocation *coordinates4 =  [[CLLocation alloc] initWithLatitude:28.930662 longitude:-95.908595];


    NSMutableArray *locationCoordinates = [[NSMutableArray alloc] initWithObjects:coordinates1,coordinates2,coordinates3,coordinates4,coordinates1, nil];

    int numberOfCoordinates = [locationCoordinates count];

    CLLocationCoordinate2D coordinates[numberOfCoordinates];


    for (NSInteger i = 0; i < [locationCoordinates count]; i++) {

        CLLocation *location = [locationCoordinates objectAtIndex:i];
        CLLocationCoordinate2D coordinate = location.coordinate;

        coordinates[i] = coordinate;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
    [self.myMapView addOverlay:polyLine];


}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 1.0;

    return polylineView;
}

更新:这是 drawRect 函数的快速版本

func drawRect() {
    let coordinates1 = CLLocation(latitude: 30.439217, longitude: -95.899668)
    let coordinates2 = CLLocation(latitude: 30.443953, longitude: -94.685679)
    let coordinates3 = CLLocation(latitude: 28.930061, longitude: -94.690486)
    let coordinates4 = CLLocation(latitude: 28.930662, longitude: -95.908595)
    let locationCoordinates = [coordinates1,coordinates2,coordinates3,coordinates4,coordinates1]

    let coordinates = locationCoordinates.map { $0.coordinate }

    let polyLine = MKPolyline(coordinates: coordinates, count: coordinates.count)
    mapView.addOver(polyLine)
}

更换

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay (deprecated)

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
于 2012-11-04T00:37:04.577 回答
1

既然你问了一个一般性的问题,我只能给你一个一般性的提示。

MKMapView 有一个方法convertCoordinate:toPointToView:可以将坐标转换为视图中的一个点。您可以使用它在叠加层上绘制。

于 2012-11-04T00:14:43.077 回答
0

第一的:

#import <QuartzCore/QuartzCore.h>

然后在您想要的地方执行此操作:

CLLocationCoordinate2D koor = CLLocationCoordinate2DMake(30.439217,-95.899668);
CLLocationCoordinate2D koor2 = CLLocationCoordinate2DMake(30.443953,-94.685679);
CLLocationCoordinate2D koor3 = CLLocationCoordinate2DMake(28.930662,-95.908595);
CGPoint point1 = [mapView convertCoordinate:koor toPointToView:mapView];
CGPoint point2 = [mapView convertCoordinate:koor2 toPointToView:mapView];
CGPoint point3 = [mapView convertCoordinate:koor3 toPointToView:mapView];
UIView *someView = [[UIView alloc]initWithFrame:CGRectMake(point1.x, point1.y, point2.x-point1.x, point3.y-point1.y)];
[self viewBicimle:someView];
[someView setBackgroundColor:[UIColor clearColor]];
[someView.layer setBorderColor:[[[UIColor blackColor] colorWithAlphaComponent:1.0] CGColor]];
[someView.layer setBorderWidth:1.0];
[mapView addSubview:someView];
[someView release];

但这仅适用于不可移动的某些 MapView。当您移动地图时,它会滑动...

于 2012-11-04T00:21:11.863 回答