私は過去 2 週間、これに取り組んできました。出発点から目的地までのルートを地図上に表示するアプリがあります。目的地はユーザーの住所 (設定した設定メニューでユーザーが定義/入力したとおり) であり、開始点をハードコーディングすると、マップ、ルート、およびアプリケーションはすべて正常に機能します。cllocation または cllocationmanager の使用を伴うものを実装しようとすると、クラッシュしてスレッド エラーが発生します (3 つあります)。
このための現在のView Controllerヘッダーと実装ファイルは次のとおりです。
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MapView.h"
#import "Place.h"
#import <CoreLocation/CoreLocation.h>
@interface HomeMapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>{
IBOutlet UILabel *HomeAddressLabel;
}
@property (weak, nonatomic) IBOutlet MKMapView *MapView;
@property (strong, nonatomic) IBOutlet CLLocationManager *location;
@end
実装ファイル:
#import "HomeMapViewController.h"
@interface HomeMapViewController ()
@end
@implementation HomeMapViewController
@synthesize MapView;
@synthesize location;
double lat = 37.331706;
double lon = -122.030598;
- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
//Retrieve saved address before view loads
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *HomeAddress = [defaults objectForKey:@"HomeAddress"];
NSString *HomeCity = [defaults objectForKey:@"HomeCity"];
NSString *HomeProvince = [defaults objectForKey:@"HomeProvince"];
NSString *HomeZip = [defaults objectForKey:@"HomeZip"];
NSString *HomeCountry = [defaults objectForKey:@"HomeCountry"];
NSString *HomeAddressFull = [NSString stringWithFormat:@"%@, %@, %@, %@, %@", HomeAddress, HomeCity, HomeProvince, HomeZip, HomeCountry];
//Set address label to saved address values
HomeAddressLabel.text = HomeAddressFull;
//Map Route Code
MapView* mapView = [[MapView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//Shows user location
[self.MapView setShowsUserLocation:YES];
//set which mapview to show
[self.MapView addSubview:mapView];
self.location = [[CLLocationManager alloc]init];
location.delegate = self;
location.desiredAccuracy=kCLLocationAccuracyBest;
location.distanceFilter = kCLLocationAccuracyBestForNavigation;//constant update of device location
[location startUpdatingLocation];
Place* start = [[Place alloc] init];
start.name = @"Start Location";
start.description = @"You started here.";
//^should be changed to current location once that is implemented
//start.latitude = 37.331706;
//start.longitude = -122.030598;
start.latitude = lat;
start.longitude = lon;
//start.latitude = location.location.coordinate.latitude;
//start.longitude = location.location.coordinate.longitude;
//Geocoder Code
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:HomeAddressFull completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
return;
}
for (id object in placemarks) {
CLPlacemark *placemark = object;
Place* destination = [[Place alloc] init];
destination.name = @"Destination";
destination.description = [NSString stringWithFormat:@"%@, %@, %@", HomeAddress, HomeCity, HomeProvince];
destination.latitude = placemark.location.coordinate.latitude;
destination.longitude = placemark.location.coordinate.longitude;
[mapView showRouteFrom:start toestination];
}
}];
[super viewDidLoad];
}
- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation{
[manager stopUpdatingLocation];
}
//************NEW METHOD
-(void)locationManagerCLLocationManager *)manager didFailWithErrorNSError *)error{
NSString *msg = @"Error obtraining location:";
UIAlertView *alert;
alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:msg delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
- (void)viewDidUnload
{
[self setMapView:nil];
HomeAddressLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
私は基本的に、ユーザーの現在の位置座標を取得し、それらを start.latitude および start.longitude の値として保存して、ルートがユーザーの現在の位置からの経路を表示する方法について、誰かが光を当てるのを手伝ってくれるかどうか尋ねたいと思います.彼らの目的地へ。ユーザーの移動に応じてルートが更新されることは想定されていません。ユーザーの現在の場所と目的地に注釈/目印を付けたいだけです(私が持っているように)。その後、青いポイント(別名ユーザー)ができるようになります進むべきルートに沿って自分自身が移動するのを見てください。ここですべての提案を試しましたが、まったく機能しなかったものもあれば、一度だけ機能したものもあれば、その後再び機能しなかったものもあります (シミュレーターのコンテンツをリセットした後でも!)。一部のリンクは関連していますが、非常に古くなっているため、そこにある方法は機能しません。
そして、ここで私は、ユーザーの現在の位置座標を取得することが、この関数のより簡単なタスクになると考えていました!