【问题标题】:how to find the current location in iOS如何在 iOS 中找到当前位置
【发布时间】:2014-02-01 06:39:33
【问题描述】:

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

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

.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

我想当按钮按下时我的当前位置显示在地图中

【问题讨论】:

标签: ios objective-c


【解决方案1】:

使用下面这个链接很有希望找到当前位置等,链接是http://www.appcoda.com/how-to-get-current-location-iphone-user/

【讨论】:

  • thk @anbu.karthik 但我想在 MAP 上显示当前位置
【解决方案2】:

第 1 步: 头文件

中的#import &lt;MobileCoreServices/MobileCoreServices.h&gt;

第二步:添加代理CLLocationManagerDelegate

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

第 3 步:将此代码添加到 类文件

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

第四步:获取位置的方法

//------------ 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);
          ------*/
     }];
}

【讨论】:

    【解决方案3】:
    -(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;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多