user1926201
user1926201

Reputation: 113

How to get device current latitude and longitude for IPhone device

I'm developing an app were I would like to get device current latitude and longitude for IPhone. Is it possible to obtain this information without using GPS.

Thanks

Upvotes: 0

Views: 3503

Answers (5)

mohonish
mohonish

Reputation: 1396

Using Swift

import CoreLocation

Get current location

let locationManager = CLLocationManager()
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest

//get the most recently retrieved user location
if let currentLocation = locationManager.location {
    let longitude = currentLocation.coordinate.longitude
    let latitude = currentLocation.coordinate.latitude
    //work with the received data
}

Note that locationManager.location is an optional and hence should be safely unwrapped.

Upvotes: 0

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

you can use CoreLocation framework

The Core Location framework has one important class CLLocationManager.

The CLLocationManager class handles sending out updates to a delegate anytime the location is changed. it uses the delegate CLLocationManagerDelegate protocol.

you can use it like

#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController 
  <CLLocationManagerDelegate>
{
  CLLocationManager *locationManager;

}

set up locationManager like this in viewDidLoad

- (void)viewDidLoad {
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
  locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
  [locationManager startUpdatingLocation];
}

implement the below delegate mehod for getting changed location

 - (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
  NSInteger degrees = newLocation.coordinate.latitude;
  double decimal = fabs(newLocation.coordinate.latitude - degrees);
  int minutes = decimal * 60;
  double seconds = decimal * 3600 - minutes * 60;
  NSString *lattitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                   degrees, minutes, seconds];
  lattitudeLabel.text = lattitudeStr;
  degrees = newLocation.coordinate.longitude;
  decimal = fabs(newLocation.coordinate.longitude - degrees);
  minutes = decimal * 60;
  seconds = decimal * 3600 - minutes * 60;
  NSString *longitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
  longitudeLabel.text = longitudeStr;
}

Upvotes: 3

progrmr
progrmr

Reputation: 77173

You can get current location without GPS by using CLLocationManager if the device has WiFi or cellular data service. The location will not be as accurate as you would get from GPS but you get a result faster than from GPS.

You can request a non-GPS location from CLLocationManager by setting the desiredAccuracy property to kCLLocationAccuracyKilometer.

Upvotes: 0

Nikita P
Nikita P

Reputation: 4246

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
CLLocation* currentLocation = [locationManager location];

NSString *loc = [NSString stringWithFormat:@"%f,%f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(@"location:%@", loc);

Upvotes: 0

Ankur Arya
Ankur Arya

Reputation: 4723

You can use CLLocationManager to get current lat and long. create an iVar of CLLocationManager and use it.

Here is the code:

- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);

currentLat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
currentLong = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
}

Upvotes: 1

Related Questions