0

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

  • http://stackoverflow.com/questions/15265755/how-can-i-get-current-location-on-ios – iPatel Feb 01 '14 at 06:42
  • "how to find the current location in Xcode" - stand the hell up from your computer and look out through the window. That's the real world out there. Maybe you can locate the number plate of your house/flat. (Oh wait. You mean the user's location? Well, then that's not "in Xcode". That's "in your iOS app". Whether you are using Xcode or another IDE is completely irrelevant. This is a question about the program/code, not about the IDE.) –  Feb 01 '14 at 06:50
  • possible duplicate of [iphone app - how to get the current location only once and store that to be used in another function?](http://stackoverflow.com/questions/13491411/iphone-app-how-to-get-the-current-location-only-once-and-store-that-to-be-used) – Bug Feb 01 '14 at 07:43

3 Answers3

1

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/

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

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);
          ------*/
     }];
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
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;
}
M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45
awadh
  • 1
  • 1