6

CLLocationManager なしで MKMapView をユーザーの現在の場所にズームするにはどうすればよいですか?の指示を読み、それに従いました。

ただし、現在のユーザーの場所は取得できますが、実際にマップを視覚的に中央に配置することはできません。

たとえば、viewDidLoad 関数では、ユーザーの既知の場所を視覚的に中心にマップを配置するにはどうすればよいでしょうか? 以下に CurrentLocation のコードを貼り付けました (viewDidLoad の XXX コメントを参照)


#import "MapViewController.h"
#import "PlacemarkViewController.h"

@implementation MapViewController

@synthesize mapView, reverseGeocoder, getAddressButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // XXX HERE: How can I cause self.mapView to actually recenter itself at self.mapview.userLocation.location?


    mapView.showsUserLocation = YES;
}

- (void)viewDidUnload
{
    self.mapView = nil;
    self.getAddressButton = nil;
}

- (void)dealloc
{
    [reverseGeocoder release];
    [mapView release];
    [getAddressButton release];

    [super dealloc];
}

- (IBAction)reverseGeocodeCurrentLocation
{
    self.reverseGeocoder =
        [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot obtain address."
                                                        message:errorMessage
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    PlacemarkViewController *placemarkViewController =
        [[PlacemarkViewController alloc] initWithNibName:@"PlacemarkViewController" bundle:nil];
    placemarkViewController.placemark = placemark;
    [self presentModalViewController:placemarkViewController animated:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // we have received our current location, so enable the "Get Current Address" button
    [getAddressButton setEnabled:YES];
}

@end
4

1 に答える 1

26

MKMapView クラスcenterCoordinateのプロパティを単純に使用できるようです-ドキュメントには次のように書かれています:

このプロパティの値を変更すると、現在のズーム レベルを変更せずに、マップの中心が新しい座標になります。また、regionプロパティの値を更新して、現在のズーム レベルを維持するために必要な新しい中心座標と新しいスパン値を反映します。

したがって、基本的には、次のことを行うだけです。

self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
于 2011-02-06T18:02:17.313 に答える