我正在使用github 上的HGMovingAnnotation
and代码在. 当我从 HG 项目运行示例项目时,一切正常。HGMovingAnnotationView
MKAnnotation
MKmap
我已经更改了原始 HG 项目,允许我手动将新坐标推送到HGMapPath
,然后将注释移动到我想要的位置。
我已经放置了一个按钮,用于测试,运行手动过程,一切正常。注释在屏幕上移动。问题是,当我现在尝试使用来自实时 socket.io 连接的数据调用此手动方法时,地图注释不会移动。
此外,当地图首次加载时,注释不会显示,直到我稍微移动地图。手动移动注释也是如此,它不会显示来自数据流的移动,直到我缩放地图。但是,如果我采用按钮方式,避免 io 流,注释移动而无需缩放或平移地图?
放置视图注释
if(doubleLat && doubleLng) {
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(doubleLat, doubleLng);
//Create path object
self.assignedAmbPath = [[HGMapPath alloc] initWithCoordinate:coordinate];
HGMovingAnnotation *movingObject = [[HGMovingAnnotation alloc] initWithMapPath:self.assignedAmbPath];
self.movingAssignedAmbObject = movingObject;
// add the annotation to the map
[self.mapView addAnnotation:movingObject];
// zoom the map around the moving object
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
MKCoordinateRegion region = MKCoordinateRegionMake(MKCoordinateForMapPoint(self.movingAssignedAmbObject.currentLocation), span);
[self.mapView setRegion:region animated:YES];
// start moving the object
[movingObject start];
}
有效的代码
- (IBAction)testMoveBtnPressed:(id)sender {
//TODO: move x and y
DLog(@"============== Test move button was pressed ================ ");
NSLog(@"");
int randn = (random() % 15)+15;
float pscale = (float)randn / 10000;
double lat = 39.9813855 + pscale;
double lng = -75.1502155 + pscale;
for (id<MKAnnotation> annotation in self.mapView.annotations){
MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
if (![annotation isKindOfClass:[PhoneAnnotation class]]){
// Process annotation view
[((HGMovingAnnotation *)annotation) trackToNewPosition:CLLocationCoordinate2DMake(lat, lng)];
}
}
}
不起作用的代码
{
//TODO: move thing to new location
double doubleLat = [lat doubleValue];
double doubleLng = [lng doubleValue];
// NSLog(@"--------------- Jason it is ------------- Latitude being passed in is %f", doubleLat);
// NSLog(@"--------------- Jason it is ------------- Longitude being passed in is %f", doubleLng);
//
// [self.movingAssignedAmbObject trackToNewPosition:CLLocationCoordinate2DMake(doubleLat, doubleLng)];
for (id<MKAnnotation> annotation in self.mapView.annotations){
MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
if (![annotation isKindOfClass:[PhoneAnnotation class]]){
// Process annotation view
[((HGMovingAnnotation *)annotation) trackToNewPosition:CLLocationCoordinate2DMake(doubleLat, doubleLng)];
}
}
}