本文介绍了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.

我用google搜索但无法找到合适的解决方案,因为我发现的是使用数据点数组绘制叠加层但我在这两个点之间没有任何点数组积分。

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上的两点之间画线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:40