0

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

我试图了解我在这里做错了什么。如您所知,我对此很陌生,所以请善待:)

。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
4

2 回答 2

0

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

-(void) zoomInLocation: ( CLLocation * ) location
{
    shopMapView.region = MKCoordinateRegionMakeWithDistance(  location.coordinate, 800,800 );
}
于 2013-07-10T15:41:36.837 回答
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。

于 2013-07-10T21:39:05.247 回答