我正在使用MKMapView
在iPhone中创建地图应用。
我确实在这一点上成功找到了我的current location
和zoom
:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
}
-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1550, 1550);
[self.mapView setRegion:region animated:YES];
}
我想解决的问题是:
我在location
上有一个map
(viewDidLoad
中的此代码):
CLLocationCoordinate2D location = [self getLocationFromAddressString:@"San Francisco, CA, United States"];
// location.latitude = 37.78608;
// location.longitude = -122.407398;
MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Store location" coordinate:location];
[self.mapView addAnnotation:mapAnnotation];
这是mapViewAnnotation:
@implementation MapViewAnnotation
@synthesize title = _title;
@synthesize coordinate = _coordinate;
- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c {
self = [super init];
if(self) {
_title = t;
_coordinate = c;
}
return self;
}
@end
我想为此zoomLevel
和我的mapCenter
提供适当的location
和current location
。
我可以使用MapController.zoomToSpan()
在Android中成功完成此操作。如何在iPhone Map中修复它?
答案 0 :(得分:2)
创建位置数组,然后创建所有AnnotationPins的中心mapview
-(void) RoutscenterMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points
{
CLLocation* currentLocation = [arrLocation objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = (maxLat - minLat) * 2;
region.span.longitudeDelta = (maxLon - minLon) * 2;
[mapView setRegion:region animated:YES];
}
你也可以在数组中添加位置,如下...
CLLocation *temploc=[[CLLocation alloc]initWithLatitude:latitude longitude:longtitude];
[arrLocation addObject:temploc];
之后使用此数组使地图居中
我希望这可以帮助你...
:)
答案 1 :(得分:0)
这是我实现它的方式:
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> 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;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];