- (void)drawTestLine
{
CLLocation*location0 = [[CLLocationalloc]initWithLatitude:39.954245longitude:116.312455];//北京杭州
CLLocation*location1 = [[CLLocationalloc]initWithLatitude:30.247871longitude:120.127683];
NSArray*array = [NSArrayarrayWithObjects:location0, location1,nil];
[self drawLineWithLocationArray:array];
}
- (void)drawLineWithLocationArray:(NSArray*)locationArray
{
int pointCount = [locationArray count];
CLLocationCoordinate2D* coordinateArray = (CLLocationCoordinate2D*)malloc(pointCount *sizeof(CLLocationCoordinate2D));
for(int i =0; i < pointCount; ++i) {
CLLocation*location = [locationArray objectAtIndex:i];
coordinateArray[i] = [location coordinate];
}
self.routeLine= [MKPolylinepoly lineWithCoordinates:coordinateArray count:pointCount];
[self.mapView setVisibleMapRect:[self.routeLineboundingMapRect]];
[self.mapView addOverlay:self.routeLine];
free(coordinateArray);
coordinateArray =NULL;
}
/**
*当添加一个覆盖层数据模型到地图上时,地图会调用这个方法,查找对应的覆盖层视图(渲染图层)
*
* @param mapView地图
* @param overlay覆盖层数据模型
*
* @return覆盖层视图
*/
- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id)overlay
{
if( [overlayisKindOfClass:[MKPolylineclass]]) {
MKPolylineRenderer*routeLineView = [[MKPolylineRenderer alloc]initWithPolyline:self.routeLine];
//self.routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor= [UIColorredColor];
routeLineView.lineWidth=1;
return routeLineView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
UITouch* t = [touches allObjects].firstObject;
CGPoint point = [t locationInView:self.mapView];
CLLocationCoordinate2D coordinate2D = [self.mapView convertPoint:point toCoordinate FromView:self.mapView];
CLLocation* location =
[[CLLocation alloc]initWithLatitude:coordinate2D.latitude longitude:coordinate2D.longitude];
if(_lastLocation2) {
CLLocationDistance distance = [location distanceFromLocation:_lastLocation2];
//将用户连续两次点击地图时的点画一条直线
//NSArray *array = [NSArray arrayWithObjects:location, _lastLocation2,nil];
//[self drawLineWithLocationArray:array];
}else{
_lastLocation2= location;
}
//每点击一下地图,就在该位置和用户的位置画一条直线
//NSArray
*array = [NSArray arrayWithObjects:location,
self.mapView.userLocation.location, nil];
//[self drawLineWithLocationArray:array];
}