iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone应用程序 – 如何只获取当前位置一次并存储在另一个功能中使用?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述@H_197_4@ 过去两周我一直在努力工作 – 我有一个应用程序,它显示了从起点到目标点的地图上的清晰路线.目的地点是用户地址(由用户在我们设置的设置菜单中定义/输入),如果我硬编码起点,地图,路线和应用程序都可以正常工作.一旦我尝试实现涉及使用clloc或cllOCManager的任何事情,它会崩溃并给我一个线程错误(有3个). 这是当前的视图控制器头&为此执行文件: #import <UIKit/UIKit.h
过去两周我一直在努力工作 – 我有一个应用程序,它显示了从起点到目标点的地图上的清晰路线.目的地点是用户地址(由用户在我们设置的设置菜单中定义/输入),如果我硬编码起点,地图,路线和应用程序都可以正常工作.一旦我尝试实现涉及使用clloc或cllOCManager的任何事情,它会崩溃并给我一个线程错误(有3个).

这是当前的视图控制器头&为此执行文件

#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应用程序 – 如何只获取当前位置一次并存储在另一个功能中使用?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。