2014-03-05 32 views
0

我正在從大書呆子牧場指南作業。我無法讓我的地圖顯示新的位置。我知道我使用的一個方法已過時,但我在使用的NSArray問題(不推薦使用的方法是的LocationManager:didUpdateToLocation:fromLocation)。任何指針將不勝感激。 這裏是我的代碼: WhereamiViewController.h:地圖將不會更新到新位置的iOS 7

// 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 

回答

0

嘿,你可以從這種方法獲得的最新位置的iOS

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

{ 
location_updated = [locations lastObject];  
NSLog(@"updated coordinate are %@",location_updated); 

} 
+0

謝謝!我讓我的location_updated一個NSString類型。現在是以下行:NSTimeInterval t = [[location_updated timestamp] timeIntervalSinceNow];對於我得到的錯誤消息:「NSString的」不可見@interface聲明選擇「時間戳」 – meghan66

+0

可以使用的NSTimer在谷歌不斷更新位置搜索如何使用的NSTimer – morroko

+0

我也有以下行警告: self foundLocation:location_updated];不兼容的指針類型將'NSString *'發送給類型'CLLocation *'的參數....只是看到了你的答案。謝謝! – meghan66

1

initWithNibName方法,你不是在locationManager調用startUpdatingLocation(你只叫startUpdatingHeading)。

(你正在調用findLocationstartUpdatingLocation但這只是從textFieldShouldReturn調用。)

didUpdateToLocation委託方法只叫你做startUpdatingLocation

此外,確保initWithNibName方法實際上是獲取調用。
取決於如何創建此視圖控制器,你可能需要在啓動代碼移到initWithCoder:viewDidLoad