簡體   English   中英

如何在iPhone應用程序中獲取當前位置

[英]how to get current location in iPhone app

現在我的代碼只是打開地圖文件,但我想要當前位置

。H

#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}
#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];
    NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                       if (error){
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }
                       CLPlacemark *placemark = [placemarks objectAtIndex:0];
                       NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);

                   }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

這段代碼將為我打開地圖視圖,但不是我當前的位置,請立即幫助我,我想在我的應用中顯示當前位置

提前致謝

獲取位置后,通過設置地圖視圖的中心

[mapView setCenterCoordinate: currentLocation.coordinate animated:YES];

mapView.showsUserLocation = YES;

    <CoreLocation/CoreLocation.h>

在appdelegate.m中寫以下行

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:       (NSDictionary *)launchOptions{
if (locationManager == nil)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
    locationManager.distanceFilter = 500; // meters
    [locationManager startUpdatingLocation];
}

並添加以下方法

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation    *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
 {
     if(placemarks.count)
     {
         NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
         self.strAddress = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"Street"]];
         if (self.strAddress==(id) [NSNull null] || [self.strAddress length]==0 || [self.strAddress isEqualToString:@""])
         {
             self.strAddress = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"Name"]];
         }
         if ([dictionary valueForKey:@"City"]==(id) [NSNull null] || [[dictionary valueForKey:@"City"] length]==0)
         {
             self.strAddress = [NSString stringWithFormat:@"%@, %@", strAddress, [dictionary valueForKey:@"State"]];
         }
         else if ([dictionary valueForKey:@"State"]==(id) [NSNull null] || [[dictionary valueForKey:@"State"] length]==0)
         {
             NSArray *arr = [dictionary valueForKey:@"FormattedAddressLines"];
             self.strAddress = [arr objectAtIndex:0];
             for (int i=1; i<arr.count; i++)
             {
                 self.strAddress = [NSString stringWithFormat:@"%@, %@", self.strAddress, [arr objectAtIndex:i]];
             }
         }
         else
         {
             self.strAddress = [NSString stringWithFormat:@"%@, %@", strAddress, [dictionary valueForKey:@"City"]];
         }
     }
 }];
return;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM