- (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;
}
/**
*當(dāng)添加一個(gè)覆蓋層數(shù)據(jù)模型到地圖上時(shí),地圖會(huì)調(diào)用這個(gè)方法僵腺,查找對應(yīng)的覆蓋層視圖(渲染圖層)
*
*? @param mapView地圖
*? @param overlay覆蓋層數(shù)據(jù)模型
*
*? @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];
//將用戶連續(xù)兩次點(diǎn)擊地圖時(shí)的點(diǎn)畫一條直線
//NSArray *array = [NSArray arrayWithObjects:location, _lastLocation2,nil];
//[self drawLineWithLocationArray:array];
}else{
_lastLocation2= location;
}
//每點(diǎn)擊一下地圖,就在該位置和用戶的位置畫一條直線
//NSArray
*array = [NSArray arrayWithObjects:location,
self.mapView.userLocation.location, nil];
//[self drawLineWithLocationArray:array];
}