【问题标题】:map: can't zoom in current location地图:无法放大当前位置
【发布时间】:2013-07-10 15:02:24
【问题描述】:

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

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

.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

【问题讨论】:

  • 问题说地图应该缩放到“用户的当前位置”,但 findLocation 中的代码将 location 设置为从 Google api 查询返回的一些值。将 NSLogs 放入 findLocation 以打印返回的值并确认代码正在执行您认为应该执行的操作。还建议在调试器中单步执行该代码。还要确认 findInLocation 和 zoomInLocation 实际执行。在 zoomInLocation 中,NSLog 坐标值。

标签: ios objective-c location mkmapview cllocationmanager


【解决方案1】:

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

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

【讨论】:

  • 不,这只是在这里。
【解决方案2】:

我在编写的应用程序中遇到了类似的问题。我的 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。

【讨论】:

  • 有趣,你把这个逻辑放在你的代码中的什么地方? viewDidLoad?
  • 是的,这就是我拥有它的地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 2016-01-06
相关资源
最近更新 更多