我有10 - 15 lat一次在地圖上顯示。我應該如何設置區域,以便我可以看到地圖上的所有註釋。在MKMapView設置區域
2
A
回答
2
嘗試像這可能是它可以幫助你,
MKCoordinateSpan span = MKCoordinateSpanMake(52.0f,80.0001f);
CLLocationCoordinate2D coordinate = {8.9999, 18.2812};
MKCoordinateRegion region = {coordinate, span};
MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
NSLog(@"Fit Region %f %f", regionThatFits.center.latitude, regionThatFits.center.longitude);
[self.mapView setRegion:regionThatFits animated:YES];
分別代替你的座標和所有的值。
+0
@Amit:接受對您有用的清理社區的答案。 – Balu 2013-05-09 05:23:36
0
對於顯示所有註釋使用此方法,我在與您的問題相同的問題中使用此方法。它爲我工作。
-(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(MYAnnotation* annotation in mapView.annotations)// here MYAnnotation is custom annotation call.
{
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);
}
NSLog(@"A%f, B%f, C%f, D%f,", topLeftCoord.latitude, topLeftCoord.longitude, bottomRightCoord.latitude, bottomRightCoord.longitude);
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];
}
調用此方法設置後的所有地方(譯註)在地圖上查看,例如像..
[self zoomToFitMapAnnotations:self.mapView];
4
create class as below. create two class: ViewController is class of UIViewController and MapViewAnnotation is class of NSObject. I have created that two class as below. bind mapview in XIB of ViewController and set delegate.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MapViewAnnotation.h"
@interface ViewController : UIViewController<MKMapViewDelegate>{
IBOutlet MKMapView* mapView;
NSMutableArray *arrayLocation;
}
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayLocation = [[NSMutableArray alloc]init];
NSMutableArray *arrAnnotations = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"32.774125" forKey:@"latitude"];
[dict setValue:@"-117.240658" forKey:@"longitude"];
[dict setValue:@"Bars & Restaurants 1" forKey:@"title"];
[arrayLocation addObject:dict];
dict = nil;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"32.784526" forKey:@"latitude"];
[dict setValue:@"-117.240985" forKey:@"longitude"];
[dict setValue:@"Bars & Restaurants 2" forKey:@"title"];
[arrayLocation addObject:dict];
dict = nil;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"32.773477" forKey:@"latitude"];
[dict setValue:@"-117.241144" forKey:@"longitude"];
[dict setValue:@"Bars & Restaurants 3" forKey:@"title"];
[arrayLocation addObject:dict];
dict = nil;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"32.775301" forKey:@"latitude"];
[dict setValue:@"-117.238893" forKey:@"longitude"];
[dict setValue:@"Bars & Restaurants 4" forKey:@"title"];
[arrayLocation addObject:dict];
dict = nil;
for(int i=0;i<[arrayLocation count];i++)
{
CLLocationCoordinate2D location;
location.latitude = [[[arrayLocation objectAtIndex:i] objectForKey:@"latitude"] doubleValue];
location.longitude = [[[arrayLocation objectAtIndex:i] objectForKey:@"longitude"] doubleValue];
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[[arrayLocation objectAtIndex:i] objectForKey:@"title"] Coordinate:location andIndex:i];
[arrAnnotations addObject:newAnnotation];
}
[mapView addAnnotations:arrAnnotations];
mapView.region = [MapViewAnnotation regionForAnnotations:arrAnnotations];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
if (pin == nil)
{
pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"];
}
else
{
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = NO;
pin.canShowCallout=TRUE;
UIButton *btn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
MapViewAnnotation *temp = (MapViewAnnotation *)pin.annotation;
btn.tag=temp.index;
pin.rightCalloutAccessoryView=btn;
[btn addTarget:self action:@selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
return pin;
}
@end
Implementation of MapViewAnnotation class
#import <MapKit/MapKit.h>
@interface MapViewAnnotation : NSObject <MKAnnotation , MKMapViewDelegate>{
NSString *title;
int index;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readwrite) int index;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
- (id)initWithTitle:(NSString *)ttl Coordinate:(CLLocationCoordinate2D)c2d andIndex:(int)intIndex;
+(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations ;
@end
#import "MapViewAnnotation.h"
@implementation MapViewAnnotation
@synthesize title, coordinate,index;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
self = [super init];
title = ttl;
coordinate = c2d;
return self;
}
- (id)initWithTitle:(NSString *)ttl Coordinate:(CLLocationCoordinate2D)c2d andIndex:(int)intIndex
{
self = [super init];
title = ttl;
coordinate = c2d;
index = intIndex;
return self;
}
+(MKCoordinateRegion) regionForAnnotations:(NSArray*) annotations
{
NSAssert(annotations!=nil, @"annotations was nil");
NSAssert([annotations count]!=0, @"annotations was empty");
double minLat=360.0f, maxLat=-360.0f;
double minLon=360.0f, maxLon=-360.0f;
for (id<MKAnnotation> vu in annotations) {
if (vu.coordinate.latitude < minLat) minLat = vu.coordinate.latitude;
if (vu.coordinate.latitude > maxLat) maxLat = vu.coordinate.latitude;
if (vu.coordinate.longitude < minLon) minLon = vu.coordinate.longitude;
if (vu.coordinate.longitude > maxLon) maxLon = vu.coordinate.longitude;
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat+maxLat)/2.0, (minLon+maxLon)/2.0);
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat-minLat, maxLon-minLon);
MKCoordinateRegion region = MKCoordinateRegionMake (center, span);
return region;
}
@end
0
如果有人正在尋找斯威夫特3解決方案,我已經寫了這快速擴展,都非常好,我:)
public extension MKMapView {
public static func visibleRect(for coords: [CLLocationCoordinate2D]) -> MKMapRect {
return coords.reduce(MKMapRectNull) { outRect, coord in
let point = MKMapPointForCoordinate(coord)
let rect = MKMapRectMake(point.x, point.y, 0.1, 0.1)
let union = MKMapRectUnion(rect, outRect)
return union
}
}
public func fitCoordinates(_ coords: [CLLocationCoordinate2D],
animated: Bool = true,
insets: UIEdgeInsets = UIEdgeInsets(top: 25, left: 25, bottom: 25, right: 25)) {
let rect = MKMapView.visibleRect(for: coords)
self.setVisibleMapRect(rect, edgePadding: insets, animated: animated)
}
}
它允許快速完成:
map.fitCoordinates([userLocation, location1, location2])
希望這有助於任何人!
要點在這裏:https://gist.github.com/freak4pc/1748322a1b8bba23b6de3f47cb00c3ea
相關問題
- 1. MKMapView設置區域
- 2. 在3.2.3/OS4中破解的MKMapView - 無法設置區域
- 3. MKMapView:如何根據精度設置區域跨度?
- 4. MKMapview中的設置區域已經關閉了1.6倍
- 5. 將MKMapView區域設置爲以兩個註釋爲中心
- 6. MKMapView的visibleMapRect屬性沒有正確設置地圖區域?
- 7. MKMapView不改變區域
- 8. MKMapView不縮放到區域
- 9. 區域設置在Excel
- 10. Sharepoint - 區域設置
- 11. Sharepoint區域設置
- 12. MKMapView設置userLocation
- 13. 黑莓設置剪輯區域/區域
- 14. Rails i18n區域設置和區域
- 15. Java DecimalFormatter - 區域設置DE和區域設置US
- 16. Xcode 7 UI測試目標區域設置和區域設置
- 17. java區域設置getDisplayLanguage缺少一些區域設置
- 18. SSRS CreateSubscription - 設置區域設置
- 19. 如何設置全局區域設置?
- 20. 如何設置error.po的區域設置?
- 21. 訪問CultureInfo設置(區域設置)?
- 22. 設置區域設置 - 俄語
- 23. 使用DefaultHttpClient設置區域設置?
- 24. Facebook獲取區域設置
- 25. 檢查區域設置
- 26. 設置可用區域
- 27. 區域設置鏈接
- 28. Android區域設置更改
- 29. RMMapView地圖設置區域
- 30. 閱讀區域設置
http://stackoverflow.com/questions/5040175/iphone-mkmapview-set-map-region-to-show-all-pins-on-map – BhushanVU 2013-04-04 06:46:42
你爲什麼不接受答案?這是一件體面的事情。 – wuf810 2014-08-23 22:06:12