您的位置:首页 > 移动开发 > IOS开发

iOS开发之在地图上绘制出你运行的轨迹

2014-05-09 11:36 369 查看
首先我们看下如何在地图上绘制曲线。在MapKit中提供了一个叫MKPolyline的类,我们可以利用它来绘制曲线,先看个简单的例子。

使用下面代码从一个文件中读取出经纬度,然后创建一个路径:MKPolyline实例。

-(void)loadRoute
{
NSString*filePath=[[NSBundlemainBundle]pathForResource:@”route”ofType:@”csv”];
NSString*fileContents=[NSStringstringWithContentsOfFile:filePathencoding:NSUTF8StringEncodingerror:nil];
NSArray*pointStrings=[fileContentscomponentsSeparatedByCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

//whilewecreatetheroutepoints,wewillalsobecalculatingtheboundingboxofourroute
//sowecaneasilyzoominonit.
MKMapPointnorthEastPoint;
MKMapPointsouthWestPoint;

//createacarrayofpoints.
MKMapPoint*pointArr=malloc(sizeof(CLLocationCoordinate2D)*pointStrings.count);

for(intidx=0;idx<pointStrings.count;idx++)
{
//breakthestringdownevenfurthertolatitudeandlongitudefields.
NSString*currentPointString=[pointStringsobjectAtIndex:idx];
NSArray*latLonArr=[currentPointStringcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

CLLocationDegreeslatitude=[[latLonArrobjectAtIndex:0]doubleValue];
CLLocationDegreeslongitude=[[latLonArrobjectAtIndex:1]doubleValue];

//createourcoordinateandaddittothecorrectspotinthearray
CLLocationCoordinate2Dcoordinate=CLLocationCoordinate2DMake(latitude,longitude);

MKMapPointpoint=MKMapPointForCoordinate(coordinate);

//
//adjusttheboundingbox
//

//ifitisthefirstpoint,justusethem,sincewehavenothingtocomparetoyet.
if(idx==0){
northEastPoint=point;
southWestPoint=point;
}
else
{
if(point.x>northEastPoint.x)
northEastPoint.x=point.x;
if(point.y>northEastPoint.y)
northEastPoint.y=point.y;
if(point.x<southWestPoint.x)
southWestPoint.x=point.x;
if(point.y<southWestPoint.y)
southWestPoint.y=point.y;
}

pointArr[idx]=point;

}

//createthepolylinebasedonthearrayofpoints.
self.routeLine=[MKPolylinepolylineWithPoints:pointArrcount:pointStrings.count];

_routeRect=MKMapRectMake(southWestPoint.x,southWestPoint.y,northEastPoint.x-southWestPoint.x,northEastPoint.y-southWestPoint.y);

//clearthememoryallocatedearlierforthepoints
free(pointArr);

}
将这个路径MKPolyline对象添加到地图上

[self.mapViewaddOverlay:self.routeLine];
显示在地图上:

-(MKOverlayView*)mapView:(MKMapView*)mapViewviewForOverlay:(id)overlay
{
MKOverlayView*overlayView=nil;

if(overlay==self.routeLine)
{
//ifwehavenotyetcreatedanoverlayviewforthisoverlay,createitnow.
if(nil==self.routeLineView)
{
self.routeLineView=[[[MKPolylineViewalloc]initWithPolyline:self.routeLine]autorelease];
self.routeLineView.fillColor=[UIColorredColor];
self.routeLineView.strokeColor=[UIColorredColor];
self.routeLineView.lineWidth=3;
}

overlayView=self.routeLineView;

}

returnoverlayView;

}




然后我们在从文件中读取位置的方法改成从用gprs等方法获取当前位置。

第一步:创建一个CLLocationManager实例

第二步:设置CLLocationManager实例委托和精度
第三步:设置距离筛选器distanceFilter

第四步:启动请求
代码如下:

-(void)viewDidLoad{ [superviewDidLoad]; noUpdates=0; locations=[[NSMutableArrayalloc]init]; locationMgr=[[CLLocationManageralloc]init]; locationMgr.delegate=self; locationMgr.desiredAccuracy=kCLLocationAccuracyBest; locationMgr.distanceFilter=1.0f; [locationMgrstartUpdatingLocation]; }

上面的代码我定义了一个数组,用于保存运行轨迹的经纬度。

每次通知更新当前位置的时候,我们将当前位置的经纬度放到这个数组中,并重新绘制路径,代码如下:

-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation{
noUpdates++;

[locationsaddObject:[NSStringstringWithFormat:@"%f,%f",[newLocationcoordinate].latitude,[newLocationcoordinate].longitude]];

[selfupdateLocation];
if(self.routeLine!=nil){
self.routeLine=nil;
}
if(self.routeLine!=nil)
[self.mapViewremoveOverlay:self.routeLine];
self.routeLine=nil;
//createtheoverlay
[selfloadRoute];

//addtheoverlaytothemap
if(nil!=self.routeLine){
[self.mapViewaddOverlay:self.routeLine];
}

//zoominontheroute.
[selfzoomInOnRoute];

}


我们将前面从文件获取经纬度创建轨迹的代码修改成从这个数组中取值就行了:

//createstheroute(MKPolyline)overlay
-(void)loadRoute
{

//whilewecreatetheroutepoints,wewillalsobecalculatingtheboundingboxofourroute
//sowecaneasilyzoominonit.
MKMapPointnorthEastPoint;
MKMapPointsouthWestPoint;

//createacarrayofpoints.
MKMapPoint*pointArr=malloc(sizeof(CLLocationCoordinate2D)*locations.count);
for(intidx=0;idx<locations.count;idx++)
{
//breakthestringdownevenfurthertolatitudeandlongitudefields.
NSString*currentPointString=[locationsobjectAtIndex:idx];
NSArray*latLonArr=[currentPointStringcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@","]];

CLLocationDegreeslatitude=[[latLonArrobjectAtIndex:0]doubleValue];
CLLocationDegreeslongitude=[[latLonArrobjectAtIndex:1]doubleValue];

CLLocationCoordinate2Dcoordinate=CLLocationCoordinate2DMake(latitude,longitude);

MKMapPointpoint=MKMapPointForCoordinate(coordinate);

if(idx==0){
northEastPoint=point;
southWestPoint=point;
}
else
{
if(point.x>northEastPoint.x)
northEastPoint.x=point.x;
if(point.y>northEastPoint.y)
northEastPoint.y=point.y;
if(point.x<southWestPoint.x)
southWestPoint.x=point.x;
if(point.y<southWestPoint.y)
southWestPoint.y=point.y;
}

pointArr[idx]=point;

}

self.routeLine=[MKPolylinepolylineWithPoints:pointArrcount:locations.count];

_routeRect=MKMapRectMake(southWestPoint.x,southWestPoint.y,northEastPoint.x-southWestPoint.x,northEastPoint.y-southWestPoint.y);

free(pointArr);

}
这样我们就将我们运行得轨迹绘制google地图上面了。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: