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

私が書いたアプリでも同様の問題がありました。私の 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 に答える