iPhone:如何在 MapKit 上的两点之间画线? [英] iPhone: How to draw line between two points on MapKit?

查看:24
本文介绍了iPhone:如何在 MapKit 上的两点之间画线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个点的纬度和经度,想在 MapKit 上用 Pin 在这两个点之间画线.

I have Latitude and Longitude of two points and Want to Draw line between these two points with Pin on MapKit.

我用谷歌搜索但找不到合适的解决方案,因为我发现的解决方案是用数据点数组绘制叠加层,但我在这两个点之间没有任何点数组.

I have googled but Could not find some suitable solution because the one I found was drawing overlay with array of Data points but I do not have any array of points between these two points.

只有两点,想在这两点之间画一条线.

Just two points and want to draw line between these two points.

请帮忙.

推荐答案

首先让你的视图控制器实现 MKMapViewDelegate 协议并声明你需要的属性:

First make your view controller implement the MKMapViewDelegate protocol and declare the properties you will need:

@property (nonatomic, retain) MKMapView *mapView; //this is your map view
@property (nonatomic, retain) MKPolyline *routeLine; //your line
@property (nonatomic, retain) MKPolylineView *routeLineView; //overlay view

然后在 viewDidLoad(例如,或您初始化的任何地方)

then in viewDidLoad (for example, or wherever you initialize)

//initialize your map view and add it to your view hierarchy - **set its delegate to self***
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = CLLocationCoordinate2DMake(lat1, lon1); 
coordinateArray[1] = CLLocationCoordinate2DMake(lat2, lon2);


self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible

[self.mapView addOverlay:self.routeLine];

然后实现MKMapViewDelegate的方法-(MKOverlayView *)mapView:viewForOverlay:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if(overlay == self.routeLine)
    {
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 5;

        }

        return self.routeLineView;
    }

    return nil;
}

您可以调整代码以满足您的需要,但对于 2 分或更多分来说,这非常简单.

You can adjust the code to fit your need, but it's pretty much straight forward for 2 or more points.

这篇关于iPhone:如何在 MapKit 上的两点之间画线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆