如何将mapView的userLocation设置为我创建的MKUserLocation?

问题描述:

我试图做一些关于提高iPhone定位精度的编码。 我从我的iPhone硬件中获得了所有需要的信息(例如纬度,经度,deviceMotion,Gyro ..),并需要在mapView上显示更准确的计算结果。 但之后我发现mapView的userLocation属性是只读的,所以它可能意味着即使使用setCoordinate方法也不能更改它,所以很伤心=是否有解决方案可以处理它或者有另一种方法在地图视图中显示该位置? 谢谢!如何将mapView的userLocation设置为我创建的MKUserLocation?

你只需要创建CLLocationCoordinate2DMake的CLLocationCoordinate2D并在那里设置经度。 然后将您的位置创建为新的MKAnotation(例如yourPositionAnotation),并将您的位置从CLLocationCoordinate2D设置为坐标。然后做这样的事情:

// create coordinate 
CLLocationCoordinate2D yourPos = CLLocationCoordinate2DMake(lat, lon); 
// create MKAnnotation 
MKAnnotationView *yourAnnotation = [[MKAnnotationView alloc] init]; 

yourAnnotation.coordinate = yourPos; 

// remove all old annotations 
[self.myMapView removeAnnotations:self.myMapView.annotations] 
// add your annotation 
[self.myMapView addAnnotation:yourAnnotation]; 

最后不要忘记发布注释。

+1

你的想法很棒!我认为使用Core Graphic在地图上绘制一个点也是可行的,但是它比你的编码要多得多,谢谢! – 2012-02-02 11:32:29