问题:首次加载地图时,地图不会放大用户的当前位置。
我试图了解我在这里做错了什么。如您所知,我对此很陌生,所以请善待:)
。H
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController<MKMapViewDelegate>
@property (retain, nonatomic) IBOutlet MKMapView *shopMapView;
@property (retain, nonatomic) CLLocationManager *locationManager;
@end
.m
#import "MapViewController.h"
#import "ShopModel.h"
#import "ShopAnnotation.h"
#import "ShopAnnotationView.h"
#import "CoordinatingController.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize shopMapView;
@synthesize locationManager;
#pragma mark - Helper methods -
-(void) resetAllAnnotations
{
// code for resetting all annotations on the map
}
-(void) findLocation
{
NSString *city = [ [ ShopModel sharedInstance ] userSelectedCity ];
if ( city == nil )
return;
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc ] init ];
NSError *error = nil;
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[ city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding: NSUTF8StringEncoding error:&error ];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude;
double longitude;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
// errors
}
CLLocation *location = [[ CLLocation alloc ] initWithLatitude: latitude longitude: longitude ];
[ self performSelectorOnMainThread: @selector( zoomInLocation: ) withObject: location waitUntilDone: NO ];
[ location release ];
[ pool release ];
}
-(void) zoomInLocation: ( CLLocation * ) location
{
shopMapView.region = MKCoordinateRegionMakeWithDistance( location.coordinate, 800,800 );
}
#pragma mark - MapViewDelegate -
-(MKAnnotationView*) mapView:(MKMapView *)shopMapView viewForAnnotation:(id<MKAnnotation>)annotation
{
ShopAnnotationView *view = ( ShopAnnotationView *) [ shopMapView dequeueReusableAnnotationViewWithIdentifier: @"Test" ];
if ( view == NULL )
{
view = [ [[ ShopAnnotationView alloc ] initWithAnnotation: annotation reuseIdentifier: @"Test" ] autorelease ];
[ view setCanShowCallout: YES ];
view.rightCalloutAccessoryView = [ UIButton buttonWithType: UIButtonTypeDetailDisclosure ];
}
return view;
}
-(void) mapView:(MKMapView *)shopMapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[ [ CoordinatingController sharedInstance ] requestViewChangeByObject: view ];
}
-(void) changeCity
{
[ self performSelectorInBackground: @selector( findLocation ) withObject: nil ];
}
#pragma mark - ViewController life cycle -
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = NSLocalizedString( @"Map", @"Map" );
[ self performSelectorInBackground: @selector( findLocation ) withObject: nil ];
[ self resetAllAnnotations ];
[ [ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector( resetAllAnnotations ) name: @"AllAnnotationsUpdated" object: nil ];
[ [ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector( changeCity ) name: @"CityChanged" object: nil ];
}
- (void)viewDidUnload
{
[self setMapView:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[ [ NSNotificationCenter defaultCenter ] removeObserver: self ];
[shopMapView release];
[super dealloc];
}
@end