您的位置:首页 > 其它

最近做了个地图软件,写一些经验和心得,以及一些问题

2012-02-13 21:16 405 查看

最近做了个地图软件,写一些经验和心得,以及一些问题

管理提醒:本帖被 gagaga 设置为精华(2010-04-08)

这个软件的源码在此:

http://www.cocoachina.com/bbs/read.php?tid=19117

1,看到很多人问如何计算两点之间的距离,其实很简单哎。

准备两个CLLocation的对象,比如要计算某个位置与使用者当前位置的距离,则其中一个CLLocation是userLocation = [locationManager location],locationManager是CLLocationManager的实例,并已执行[locationManager startUpdatingLocation];

然后计算这两个CLLocation的距离(已格式化成12.34 km):

复制代码

[NSString stringWithFormat:@"%0.2f km",[userLocation getDistanceFrom:location]/1000]

2,在处理MKAnnotationView时,都要判断对应annotation是不是MKUserLocation这个显示用户当前位置的蓝点,以避免误操作。

复制代码

[annotation isKindOfClass:[MKUserLocation class]]

3,- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

这个delegate函数一般会在给MKMapView对象添加annotations后马上执行,但执行不会马上结束。如果在它完成之前就调用

[mapView viewForAnnotation:someAnnotation];的话,会返回nil的结果,这时对这个返回的nil做任何操作都不会在屏幕上显示……

4,MKMapView放大缩小时,需要注意的是放大,至少放大2倍以上才会被执行。

复制代码

- (IBAction)doZoomIn:(id)sender{//放大
MKCoordinateRegion region = mMapView.region;
region.span.latitudeDelta=region.span.latitudeDelta * 0.4;
region.span.longitudeDelta=region.span.longitudeDelta * 0.4;
[mapView setRegion:region animated:YES];
}
- (IBAction)doZoomOut:(id)sender{//缩小
MKCoordinateRegion region = mMapView.region;
region.span.latitudeDelta=region.span.latitudeDelta * 1.3;
region.span.longitudeDelta=region.span.longitudeDelta * 1.3;
[mapView setRegion:region animated:YES];
}

5,下面随便列一些其他没提到的代码,供搜索引擎搜索

复制代码

CLLocationCoordinate2D coordinate;
//Location Paris
coordinate.latitude = 48.856660;
coordinate.longitude = 2.350996;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, distance, distance);

MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[mapView setRegion:adjustedRegion animated:animated];

6,问题:

发现MKAnnotationView的Callout View有bug。

我在后台更新subtitle后,若不手动点一下其他MKAnnotationView再点回之前已经显示Calloutview的MKAnnotationView的话,那这个MKAnnotationView的Calloutview里的subtitle就不会被更新……

说清楚点就是:点了某个大头针显示信息后,若这时后台更新了它的subtitle,然后你再点这个大头针会发现它的subtitle显示成了一个空白。这时得点一下其他大头针,再点回来,原来那个大头针的信息才是完整的。

虽然有个notification是MKAnnotationCalloutInfoDidChangeNotification,但SDK手册里说这个已经不能用了。我尝试用了一下也没有任何效果……

不知道还有其他方法没

后面同学回答在此

引用
引用第22楼cjlin于2010-06-02 23:45发表的 :

回一下第6個問題...

如果用戶選上的是mkav,那...

if(mkav.selected)

{

[mkav setSelected:NO];

[mkav setSelected:YES];

}

這樣就會更新subtitle了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: