我正在做《大书呆子牧场指南》中的作业。我无法让地图显示新位置。我知道我正在使用一个不赞成使用的方法,但是我在使用NSArray时遇到了问题(不赞成使用的方法是locationManager:didUpdateToLocation:fromLocation)。任何指针将不胜感激。
这是我的代码:
WhereamiViewController.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
方法中,您没有在startUpdatingLocation
上调用locationManager
(您仅在调用startUpdatingHeading
)。
(您正在startUpdatingLocation
中调用findLocation
,但是只能从textFieldShouldReturn
中调用。)
仅当您执行didUpdateToLocation
时,才会调用startUpdatingLocation
委托方法。
另外,请确保实际上正在调用initWithNibName
方法。
根据创建此视图控制器的方式,您可能需要将该启动代码移动到initWithCoder:
或viewDidLoad
。