如何在iOS中找到当前位置

时间:2014-02-01 06:39:33

标签: ios objective-c

现在我在模拟器中找到了我当前的位置

按下按钮显示我当前的位置 但我的应用程序位于其他位置

·H

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

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property (nonatomic,retain)MKMapView *mapView;
- (IBAction)myview:(id)sender;
@property (strong, nonatomic) IBOutlet CLLocationManager *locationManger;

@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.
}

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

- (IBAction)myview:(id)sender {
    _locationManger =[[CLLocationManager alloc]init];
    _locationManger.distanceFilter=kCLDistanceFilterNone;
    _locationManger.desiredAccuracy=kCLLocationAccuracyHundredMeters;
    [_locationManger startUpdatingLocation];

    [_mapView setMapType:MKMapTypeStandard];
    [_mapView setZoomEnabled:YES];
    [_mapView setScrollEnabled:YES];

    MKCoordinateRegion region={ {0.0,0.0 },{0.0,0.0}};

    region.center.latitude=_locationManger.location.coordinate.latitude;
    region.center.longitude=_locationManger.location.coordinate.longitude;
    region.span.longitudeDelta=0.007f;

    region.span.latitudeDelta=0.007f;

    [_mapView setRegion:region animated:YES];
    [_mapView setDelegate:sender];



}
@end

我希望当按钮按下当前位置显示在地图

3 个答案:

答案 0 :(得分:1)

使用以下链接,您希望找到当前位置等,链接为http://www.appcoda.com/how-to-get-current-location-iphone-user/

答案 1 :(得分:0)

第一步: #import <MobileCoreServices/MobileCoreServices.h> 标题文件

第2步:添加委托CLLocationManagerDelegate

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

第3步:在类文件中添加此代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self CurrentLocationIdentifier]; // call this method
}

第4步:获取位置的方法

//------------ Current Location Address-----

-(void)CurrentLocationIdentifier
{
    //---- For getting current gps location
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    //------

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if (!(error))
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"\nCurrent Location Detected\n");
             NSLog(@"placemark %@",placemark);
             NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

             NSString *Address = [[NSString alloc]initWithString:locatedAt];
             NSString *Area = [[NSString alloc]initWithString:placemark.locality];
             NSString *Country = [[NSString alloc]initWithString:placemark.country];
             NSString *CountryArea = [NSString stringWithFormat:@"%@, %@", Area,Country];
             NSLog(@"%@",CountryArea);
         }

         else
         {
             NSLog(@"Geocode failed with error %@", error);
             NSLog(@"\nCurrent Location Not Detected\n");
             //return;
             CountryArea = NULL;
         }

         /*---- For more results 
         placemark.region);
         placemark.country);
         placemark.locality); 
         placemark.name);
         placemark.ocean);
         placemark.postalCode);
         placemark.subLocality);
         placemark.location);
          ------*/
     }];
}

答案 2 :(得分:0)

-(void)getLocationCurrentAddresslatitude:(NSString *)lat andlongitude:(NSString *)longitude
{

    NSHTTPURLResponse *response = nil;
    NSString *jsonUrlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?latlng=%@,%@&key=gfhhfhfhfghfghfhghfghfghDyk&result_type=street_address",lat,longitude];

    NSLog(@"%@",jsonUrlString);

    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //-- Get request and response though URL
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //-- JSON Parsing
    NSDictionary * rootDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    NSArray * result = [rootDictionary objectForKey:@"results"];


    NSDictionary *dic=[result objectAtIndex:0];
    NSString *address=[dic objectForKey:@"formatted_address"];
    self.myAddress.text=address;
}