之后,无法添加blue_dot映像
当用户移动时,我想在其旧位置上绘制另一个Mkannotation,这与Objective-C中的当前注释不同
我写了代码
-(void)plotDotted:(double)latitude :(double)longitude
{
_busOldAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.sampleMap addAnnotation:_busOldAnnotation];
}
-(void)plotPoint:(double)latitude :(double)longitude
{
_busAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.sampleMap addAnnotation:_busAnnotation];
}
注释委托
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *Identifier = @"driverView";
MKAnnotationView* myAnnotation = [self.sampleMap dequeueReusableAnnotationViewWithIdentifier:Identifier];
if (myAnnotation == nil)
{
myAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:Identifier];
//myAnnotation.enabled = YES;
}
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(_latituDouble, _longitudeDouble);
if (coordinate.latitude == _latituDouble && coordinate.longitude == _longitudeDouble)
{
myAnnotation.image = [UIImage imageNamed:@"bus_icon.png"];
}
else if (_busOldAnnotation)
{
myAnnotation.image = [UIImage imageNamed:@"blue_dot.png"];
}
return myAnnotation;
}
Called Functions
//1. Remove
[self removeAnnotation];
//3. Add new
[self plotPoint:latitudeDouble :longitudeDouble];
//2. Add Old
[self plotDotted:_latituDouble :_longitudeDouble];
//4. Assign that coordinate to global
_latituDouble = latitudeDouble;
_longitudeDouble = longitudeDouble;
[self.sampleMap reloadInputViews];
一切正常,用户正在我的地图上移动,删除以前的位置,但是在该用户移动
使用此功能,看看它是否有效
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(_latituDouble, _longitudeDouble);
if (coordinate.latitude == _latituDouble && coordinate.longitude == _longitudeDouble)
{
static NSString *Identifier1 = @"driverView";
MKAnnotationView* myAnnotation = [self.sampleMap dequeueReusableAnnotationViewWithIdentifier:Identifier1];
if (myAnnotation == nil)
{
myAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:Identifier];
//myAnnotation.enabled = YES;
}
myAnnotation.image = [UIImage imageNamed:@"bus_icon.png"];
return myAnnotation;
}
else if (_busOldAnnotation)
{
static NSString *Identifier2 = @"driverView2";
MKAnnotationView* myAnnotation = [self.sampleMap dequeueReusableAnnotationViewWithIdentifier:Identifier2];
if (myAnnotation == nil)
{
myAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:Identifier];
//myAnnotation.enabled = YES;
}
myAnnotation.image = [UIImage imageNamed:@"blue_dot.png"];
return myAnnotation;
}
return nil;
}