适合放大 iPhone 上的地图
Posted
技术标签:
【中文标题】适合放大 iPhone 上的地图【英文标题】:fit to zoom in maps on iphone 【发布时间】:2011-08-25 06:23:57 【问题描述】:我目前正在研究谷歌地图。使用 GPS,我获取用户的经纬度信息并点击网络服务以获取最近的 5 家可用商店。我可以为它绘制引脚,但问题是如何将 5 个引脚安装到最大缩放比例?我的意思是如何获得跨度的纬度和经度增量以便同时缩放和适合所有引脚?
【问题讨论】:
为了清晰起见,编辑了我的答案以包括示例和注释类 【参考方案1】:试试下面...
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
if([mapView.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(DisplayMap* annotation in mapView.annotations)
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
【讨论】:
【参考方案2】:有一篇关于如何做到这一点的好帖子here
我用来实现这个的完成的代码在这里......
-(void)zoomToFitMapAnnotations:(MKMapView*)mv
if([mv.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MapViewAnnotate* annotation in mv.annotations)
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
region = [mv regionThatFits:region];
[mv setRegion:region animated:YES];
这假设您正在使用与此类似的注释类中的注释对象(存储在数组中)......
MapViewAnnotate.h
@interface MapViewAnnotate : NSObject <MKAnnotation>
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s;
@end
MapViewAnnotate.m
#import "PropertyMapViewAnnotate.h"
@implementation MapViewAnnotate
@synthesize coordinate, title, subtitle;
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s
[super init];
coordinate = c;
[self setTitle:t];
[self setSubtitle:s];
return self;
- (void)dealloc
[title release];
[subtitle release];
[super dealloc];
@end
【讨论】:
以上是关于适合放大 iPhone 上的地图的主要内容,如果未能解决你的问题,请参考以下文章