2016.4.15 武汉 小雨
by SevenJohs.
- 概述
通过已经绘制的线的基础上,得到JSON数据,使用geometry的JSON字符串的原始数据重绘折线。 内容
- 绘制出断点折线;
- 不使用定时器一次性画出;
- 使用定时器重绘。
绘制出断点折线
/**
* 绘制线条
*/
-(void)drawPolyLine{
_pointIndex = 0;
_polyline = nil;
_polylineSymbol = [AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor blueColor] width:4];
[self timerDefination];
}
/**
* 更新PolyLine,使其绘制出每隔20个点,绘制出独立的线段。
*/
-(void)updatePloyline {
if(!_mutPointArr || [_mutPointArr count] <= _pointIndex) {
return;
}
if(!_polyline) {
_polyline = [[AGSMutablePolyline alloc]init];
[_polyline addPathToPolyline];
}
// if(_pointIndex%2 == 0 ){
// [_polyline addPathToPolyline];
//
// }
[_polyline addPointToPath:_mutPointArr[_pointIndex]];
if(_pointIndex>0) {
if(!_graphic) {
[_graphicLayer removeGraphic:_graphic];
}
}
//设置折线属性
_graphic = [AGSGraphic graphicWithGeometry:_polyline symbol:_polylineSymbol attributes:nil infoTemplateDelegate:nil];
[_graphicLayer addGraphic:_graphic];
_pointIndex++;
}
- 不使用定时器一次性画出
-(void)drawTrack {
if(!_mapView || ![_mapView loaded]){
return;
}
NSDictionary* geoJSON = [_polyline encodeToJSON];
NSString* jsonStr = [geoJSON description];
_trackDataLine = [AGSPolyline polylineWithJSON:geoJSON];
NSInteger paths = [_trackDataLine numPaths];
AGSMutablePolyline* trackLine = [[AGSMutablePolyline alloc]init];
[trackLine addPathToPolyline];
for (int path = 0; path < paths; path++) {
NSInteger numPntOnPath = [_trackDataLine numPointsInPath:path];
for (int i = 0; i < numPntOnPath; i++) {
AGSPoint* pnt = [_trackDataLine pointOnPath:path atIndex:i];
AGSPoint* newPnt = [AGSPoint pointWithX:pnt.x+0.01 y:pnt.y spatialReference:_mapView.spatialReference];
[trackLine addPointToPath:newPnt];
}
[trackLine addPathToPolyline];
}
if(![trackLine isEmpty]){
AGSSimpleLineSymbol* symbol = [AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor purpleColor] width:5];
AGSGraphic* graphic = [AGSGraphic graphicWithGeometry:trackLine symbol:symbol attributes:nil infoTemplateDelegate:nil];
[_graphicLayer addGraphic:graphic];
}
_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTrack) userInfo:nil repeats:YES];
[_timer fire];
}
- 使用定时器重绘。
-(void)updateTrack{
if(_currentPath == 0 && _currentIndex == 0){
[_trackLine addPathToPolyline];
}
NSInteger paths = [_trackDataLine numPaths];
if(_currentPath>= paths){
[self stopTimer];
return;
}
NSInteger numPntOnPath = [_trackDataLine numPointsInPath:_currentPath];
if(_currentIndex >= numPntOnPath){
_currentPath++;
_currentIndex = 0;
[_trackLine addPathToPolyline];
return;
}else{
AGSPoint* pnt = [_trackDataLine pointOnPath:_currentPath atIndex:_currentIndex];
AGSPoint* newPnt = [AGSPoint pointWithX:pnt.x+0.02 y:pnt.y spatialReference:_mapView.spatialReference];
[_trackLine addPointToPath:newPnt];
_currentIndex++;
}
if(![_trackLine isEmpty]){
if(_trackGraphic){
[_graphicLayer removeGraphic:_trackGraphic];
}
AGSSimpleLineSymbol* symbol = [AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor orangeColor] width:5];
_trackGraphic = [AGSGraphic graphicWithGeometry:_trackLine symbol:symbol attributes:nil infoTemplateDelegate:nil];
[_graphicLayer addGraphic:_trackGraphic];
}
}
END
回家了~