Muju
Muju

Reputation: 979

How to perform action when user click on myLocation button in Objective C?

I am new in iOS and I am facing problem regarding to get the information of current city of user and I also need user current latitude and longitude when I click on mycurrenLocation button.

enter image description here

As in the image when I click on this current location button I need the information of current latitude and longitude and also the name of current city. Is it is possible? If yes the how to do this?

Thanks in advance!

Upvotes: 2

Views: 1104

Answers (4)

Anbu.Karthik
Anbu.Karthik

Reputation: 82779

if you tapped the My location button the following delegate method will call

  • (BOOL) didTapMyLocationButtonForMapView: (GMSMapView *) mapView

and you get the output as

And you can get location by

(lldb) po mapView.myLocation
<+37.33243033,-122.03088128> +/- 386.93m (speed -1.00 mps / course -1.00) @ 5/19/14, 6:22:28 PM Moscow Standard Time

Upvotes: 3

Jigar
Jigar

Reputation: 1821

check this code

Import CoreLocation framework in .h File

#import <CoreLocation/CoreLocation.h>

.m file

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



- (void)viewDidLoad
{
    [super viewDidLoad];
    [self currentlocation]; 
}


-(void)currentlocation
{

    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

}

Step 5: Get location using this method

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

Sharat Kannan
Sharat Kannan

Reputation: 110

You can use CoreLocation to get the longitude and latitude.

#import <CoreLocation/CoreLocation.h>

CLLocationManager *locationManager;
- (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
}

Then

float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;

And pass the lattitude and logitude into google location api you will get the street name.

Also check This link:

Google current location API

Stackoverflow answer

Upvotes: 0

Misha
Misha

Reputation: 685

You can get location from didUpdateToLocation is called and then use geocoding method to get location

// this delegate is called when location is found
- (void)locationManager:(CLLocationManager *)manager     didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
  // an MKReverseGeocoder is created to find a placemark  coordinates
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
   geoCoder.delegate = self;
   [geoCoder start];
}

// this delegate is called to convert into placemakrk
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
 {
   MKPlacemark * myPlacemark = placemark;
   // we are now retrieving the city name
    NSString *city = [myPlacemark.addressDictionary objectForKey:  (NSString*) kABPersonAddressCityKey];
   }

Upvotes: 0

Related Questions