ios - map 不会更新到 iOS 7 中的新位置

标签 ios objective-c mapkit core-location

我正在做《大 Nerd 牧场指南》中的作业。我无法让我的 map 显示新位置。我知道我正在使用一种已弃用的方法,但我在使用 NSArray 时遇到问题(已弃用的方法是 locationManager:didUpdateToLocation:fromLocation)。任何指针将不胜感激。 这是我的代码: 其中amiViewController.h:

//  WhereamiViewController.h
//  Whereami
//
//  Created by Meghan on 2/28/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
    CLLocationManager *locationManager;

    IBOutlet UITextField *locationTitleField;
    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet MKMapView *worldView;
}

- (void)findLocation;
- (void)foundLocation:(CLLocation *)loc;

@end

WhereamiViewController.m:

//  WhereamiViewController.m
//  Whereami
//
//  Created by Meghan on 2/28/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import "WhereamiViewController.h"
#import "BNRMapPoint.h"

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController

- (void)findLocation
{
    [locationManager startUpdatingLocation];
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
}

- (void)foundLocation:(CLLocation *)loc
{
    CLLocationCoordinate2D coord = [loc coordinate];

    //Create an instance of BNRMapPoint with the current data
    BNRMapPoint *mp = [[BNRMapPoint alloc]initWithCoordinate:coord
                                                       title:[locationTitleField text]];

    //Add it to the map view
    [worldView addAnnotation:mp];

    //Zoom the region to this location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];

    //Reset the UI
    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        //Create location manager object
        locationManager = [[CLLocationManager alloc] init];

        [locationManager setDelegate:self];

        //We want the most accuracy
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        //Tell our manager to start looking for location immediately
        [locationManager startUpdatingHeading];
        [locationManager setDistanceFilter:50];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%@", newLocation);


    //How many seconds ago was this new location created?
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    //CLLocationManagers will return the last found location of the
    //device first, you don't want that data in this case.
    //If this location was made > 3 minutes ago, ignore it
    if (t < -180) {
        //This is cached data, you don't want it. Keep looking.
        return;
    }
    [self foundLocation:newLocation];
}

- (void)locationManager:(CLLocationManager *)manager
        didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    int degrees = (int)locationManager.heading.magneticHeading;
    NSLog(@"from delegate method: %i", degrees);
}

- (void)dealloc
{
    //Tell loc manager to stop sending messages
    [locationManager setDelegate:nil];
}

- (void)viewDidLoad
{
    [worldView setShowsUserLocation:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //This method isn't implemented yet - but will be soon.
    [self findLocation];

    [textField resignFirstResponder];

    return YES;
}

@end

BNRMapPoint.h:

//  BNRMapPoint.h
//  Whereami
//
//  Created by Meghan on 3/4/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BNRMapPoint : NSObject <MKAnnotation>
{

}
//A new designated initializer for instances of BNRMapPoint
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

//This is a required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotation
@property (nonatomic, copy) NSString *title;

@end

BNRMapPoint.m:

//  BNRMapPoint.m
//  Whereami
//
//  Created by Meghan on 3/4/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import "BNRMapPoint.h"

@implementation BNRMapPoint

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
    self = [super init];
    if (self) {
        _coordinate = c;
        [self setTitle:t];
    }
    return self;
}

- (id)init
{
    return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -89.32)
                              title:@"Hometown"];
}

@end

最佳答案

initWithNibName 方法中,您没有在 locationManager 上调用 startUpdatingLocation(您只调用了 startUpdatingHeading).

(您正在 findLocation 中调用 startUpdatingLocation,但它仅从 textFieldShouldReturn 调用。)

didUpdateToLocation 委托(delegate)方法仅在您执行 startUpdatingLocation 时调用。

另外,确保 initWithNibName 方法确实被调用了。
根据创建此 View Controller 的方式,您可能需要将该启动代码移动到 initWithCoder:viewDidLoad

关于ios - map 不会更新到 iOS 7 中的新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22202012/

相关文章:

objective-c - AppleTV UIAlertView 等效项

ios - 聚类标识符设置为 nil 的标记注释聚类

swift - 检查mapView是否已经完成渲染?

swift - 如何通过标题快速旋转标记?

ios - 来自横向时状态栏下的导航栏

iphone - 如何从完成 block 中检索返回值?

ios - 如何在两点之间画一条线 objective-C

ios 创建自定义 UISplitViewController,我的应用程序会被拒绝吗?

android - 在 React Native 中获取设备 ID?

iphone - 知道为什么这些默认值会返回空吗?