2

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!

Hasya
  • 9,792
  • 4
  • 31
  • 46
Muju
  • 884
  • 20
  • 54
  • 1
    Start from here : https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html after getting latitude and longitude use `reverseGeoCoding` to get information about location like, city. – Sunny Mar 01 '17 at 12:53

4 Answers4

3

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
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

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

Community
  • 1
  • 1
Sharat Kannan
  • 90
  • 2
  • 9
0

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];
   }
Misha
  • 685
  • 8
  • 20
0

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);
          ------*/
     }];
}
Jigar
  • 1,801
  • 1
  • 14
  • 29