地图:无法放大当前位置

时间:2013-07-10 15:02:25

标签: ios objective-c location mkmapview cllocationmanager

问题:首次加载地图时,地图不会放大用户的当前位置。

我试图理解我在这里做错了什么。你可以说,我对此很陌生,所以请善待:)

·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

2 个答案:

答案 0 :(得分:0)

我不知道它是否是StackOverflow上的类型,但在你的代码中我看到zoom方法以大写字母开头,你用小写字母调用它。 我建议像这样重命名缩放方法:

-(void) zoomInLocation: ( CLLocation * ) location
{
    shopMapView.region = MKCoordinateRegionMakeWithDistance(  location.coordinate, 800,800 );
}

答案 1 :(得分:0)

我在我写的应用程序中遇到了类似的问题。我的MKMapView开始于非洲海岸的大西洋,纬度和经度为(0,0)。尝试将其插入需要获取用户位置的位置:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


       int timeout = 10000000;

       while (self.mapView.userLocation.location == nil && timeout > 0) {

          sleep(1);
          timeout--;
    }

    //do whatever you need to do once you have the user's location. 
    [self findLocation];
});

等待直到找到用户的位置,然后执行您需要执行的操作。它不在主线程上,因此它不会冻结UI。