
这是当前的视图控制器头&为此执行文件:
#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,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:@"%@,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值,以便路由显示用户当前位置的路径到他们的目的地.该路由不应该随着用户移动而自动更新,我只想在用户当前位置和目的地(如我所做的)上具有注释/地标,然后蓝点(也称为用户)将能够看到自己沿着他们要采取的路线走.我已经尝试了所有的建议在这里 – 有些没有工作,一些只工作一次,然后,它无法再次工作(即使重置模拟器内容!).一些链接是相关的,但非常过时,所以方法不起作用.
而在这里,我正在考虑获取用户当前的位置坐标将是这个功能更容易的任务!
解决方法 .h文件#import <CoreLocation/CoreLocation.h>@property (nonatomic,retain) CLLocationManager *locationManager;
.m文件
- (Nsstring *)deviceLocation { Nsstring *theLocation = [Nsstring stringWithFormat:@"latitude: %f longitude: %f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude]; return theLocation;}- (voID)vIEwDIDLoad{ self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.distanceFilter = kCLdistanceFilterNone; self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [self.locationManager startUpdatingLocation];} 总结 以上是内存溢出为你收集整理的iphone应用程序 – 如何只获取当前位置一次并存储在另一个功能中使用?全部内容,希望文章能够帮你解决iphone应用程序 – 如何只获取当前位置一次并存储在另一个功能中使用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)