我上次说的设置大头针的图片的方法,设置完了是不是发现图片位置不对?然后又没法微调位置对不对?
1.重新定义一个类继承BMKAnnotationView,并重写
初始化并返回一个annotation view
@param annotation关联的annotation对象
@param reuseIdentifier如果要重用view,传入一个字符串,否则设为nil,建议重用view
@return初始化成功则返回annotation view,否则返回nil
楼主实例代码
- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString*)reuseIdentifier {
self= [superinitWithAnnotation:annotationreuseIdentifier:reuseIdentifier];这句话必须有
if(self) {
[selfsetBounds:CGRectMake(0.f,0.f,22.f,22.f)];
_imageView= [[UIImageViewalloc]initWithFrame:CGRectMake(0.f,0.f,22.f,22.f)];微调位置
_imageView.image= [UIImage imageNamed:@"sportarrow.png"];
[selfaddSubview:_imageView];
}
returnself;
}
1.初始化轨迹点
1.1你得把你的起点和终点显示出来吧(你得先有经纬度数据)
1.2存储所有位置
BMKMapPoint *points =new BMKMapPoint[count]; C++编译,文件至少保持一个.mm文件
1.3清除其他划线,并添加轨迹
根据指定坐标点生成一段折线
param points指定的直角坐标点数组
count坐标点的个数
新生成的折线对象
self.polyLine= [BMKPolyline polylineWithPoints:points count:count];
1.4添加轨迹
[self.map ViewaddOverlay:self.polyLine];
1.5根据polyline设置地图范围
1.6计算两点之间的距离(有这个才能设置动画时间)
#pragma计算两点距离
-(double)reckonNodeDistance : (CLLocationCoordinate2D)startCoordina andEndNode : (CLLocationCoordinate2D)endCoordina{
CLLocation*orig=[[CLLocationalloc]initWithLatitude:startCoordina.latitudelongitude:startCoordina.longitude] ;
CLLocation* dist=[[CLLocationalloc]initWithLatitude:endCoordina.latitudelongitude:endCoordina.longitude];
CLLocationDistance kilometers=[orig distanceFromLocation:dist]/1000;
return kilometers;(双精度)
}
1.7轨迹运行动画
-(void)creatCarTrailPoint:(CLLocationCoordinate2D)coordina title:(NSString*)title distance : (double)distancecarSpeed : (double) speed{
[_mapViewsetZoomLevel:11];
//_sportAnnotationView.imageView.transform = CGAffineTransformMakeRotation([[[_requestData objectForKey:@"data"][timerNum + 2] objectForKey:@"msg_direction "] doubleValue]);
[UIView animateWithDuration:diatance/speed animations:^{
_car_trail_point.coordinate= coordina;
_car_trail_point.title= title;
[self.mapViewaddAnnotation:_car_trail_point];
BMKCoordinateRegionregion;
region.center= coordina;
[_mapViewsetRegion:regionanimated:YES];
}completion:nil];
}
使用动画效果来改变大头针经纬度可以实现平移效果,用计算的距离和速度来获取动画时间
注:以上纯属个人思路,建议新手学习。大神路过勿喷