user3239699
user3239699

Reputation:

how to find the current location in iOS

now i find the my current location in simulator

when press button show my current location but my app located other locations

.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

i want when button press my current location show in map

Upvotes: 0

Views: 2166

Answers (3)

awadh
awadh

Reputation: 1

-(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;
}

Upvotes: 0

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11247

Step 1: #import <MobileCoreServices/MobileCoreServices.h> in header file

Step 2: Add delegate CLLocationManagerDelegate

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

Step 3: Add this code in class file

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

Step 4: Method to get location

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

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82756

use this following link it is very hopeful for you to find the current Location and etc, the link is http://www.appcoda.com/how-to-get-current-location-iphone-user/

Upvotes: 1

Related Questions