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
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;
}
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];
}
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
CLLocation* currentLocation = [locationManager location];
NSString *loc = [NSString stringWithFormat:#"%f,%f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(#"location:%#", loc);
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.
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.
Related
I want this functionality to achieve, that when the user repeats the visit to same place suppose a person moves one meter forward then one meter back. He should get the same lat long value what he gets his previous time. I mean to accuracy for every footstep the user takes with device. How can i achieve this. Currently i am using CLLocationManager but it is giving the difference of 20 to 30 points at last in lat long when i moves at same place again.
Please help. Thanks in advance.
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m
locationManager.distanceFilter = 0.1f;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSDate *locationTimeStamp = newLocation.timestamp;
NSTimeInterval delta = [locationTimeStamp timeIntervalSinceNow];
NSLog(#"The location timestamp interval was %f seconds.", delta);
_latitude.text = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
_longitude.text = [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
_time.text = [NSString stringWithFormat:#"%f",delta];
}
I am working on this walking/running app which calculates distance in meters and records lat-long for further use. Now when I calculate distance I get incorrect distance every time. I have compared it with other running apps and they generally show different distance than my distance.
Here is the code that I am using:
#define kDesiredAccuracy 5.0f
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = kDesiredAccuracy;
_routes = [[NSMutableArray alloc] init];
lastKnownLocation = nil;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// return if accuracy is less than 0 or is greater than desired accuracy.
if (newLocation.horizontalAccuracy < 0)
{
return;
}
if(newLocation.horizontalAccuracy > kDesiredAccuracy)
{
return;
}
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
CLLocationSpeed speed = [newLocation speed];
// return if stale data or user is not moving
if (locationAge > 5.0 || speed <= 0) return;
//return if first location is found
if(lastKnownLocation == nil)
{
lastKnownLocation = newLocation;
return;
}
CLLocationDistance distance = [newLocation distanceFromLocation:(self.pramDistance > 0)?lastKnownLocation:oldLocation];
if(distance > 0)
{
// save distance for future use
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:#"%g", newLocation.coordinate.latitude] forKey:#"latitude"];
[dict setObject:[NSString stringWithFormat:#"%g", newLocation.coordinate.longitude] forKey:#"longtitude"];
[dict setObject:[NSString stringWithFormat:#"%f",distance] forKey:#"distance"];
[_routes addObject:dict];
// add distance to total distance.
self.pramDistance += distance;
}
}
Once user finishes walking/running I draw rout of walk/run on map view. For this purpose I simply draw a ploy line over MKMapView using all the recorded locations.
The map view shows zig-zag line for route and distance is always incorrect. Please suggest me where I am doing wrong and what should I amend to make it work proper?
Here is the comparison (left one is other's app and right one is mine):
Try this,
Import CLLocationManagerDelegate,
CLLocation *currentLocation = self.currentLocation;
float distanceMile = [currentLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:latitude longitude:longitude]]/1609.34;
-(void)postCurrentLocationOfDevice
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
self.currentLocation = [locations objectAtIndex:0];
[self.locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.currentLocation = newLocation;
[self.locationManager stopUpdatingLocation];
}
Though I didn't get much of the help here, I found some help and idea in this question: Corelocation incorrect distances
What I did to fix the problem is:
I stored recent 5 locations in an array.
Once 5 items are stored in the array, I checked for the nearest location.
After getting nearest location I calculated distance between last best location and stored new location in last best location.
P.S.: My code is messy and naming conventions are not that generic, that's why I am not posting my code here.
I want to get user coordinates every X seconds.
I added locations getting in app delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager locationServicesEnabled]){
[self.locationManager startUpdatingLocation];
}
...
Here I handle new coordinates
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *loc = [locations objectAtIndex:0];
NSDate* eventDate = loc.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (howRecent < 10)
{
CLLocation* location = [locations lastObject];
double lat = location.coordinate.latitude;
double lng = location.coordinate.longitude;
NSLog(#"lat:%f lng:%f", lat, lng);
...
But I don't know how to continue getting iPhone coordinates when app is in background.
I suppose that I should add some code in:
- (void)applicationDidEnterBackground:(UIApplication *)application...
But I don't know what to do to keep location getting a live?
In your info.plist:
R.Click -> add a row and choose Required background modes-> and at item 0 choose App registers for location updates
Hello I am using CoreLocation to get latitude and longitude in my map application to get latitude and longitude by using following method
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
[super viewDidLoad];
}
The _ (void)locationManager.... method is not getting call
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
}
I want to call lati & longi on Button click event to URL. But it is not working. it passes 0000.0 lati & longi to my url. Can some one tell me how to rectify this code
I don't see any obvious problem with your init of CLLocationManager. If you are not getting callbacks perhaps it doesn't yet have a location available. You should also implement the error callback and see if it is getting called instead.
I suggest you try one of the CLLocationManager examples provided by Apple and see if you can get the example to run.
I have did this to pass latitude and longitude in to web service URL on button click...
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
NSLog(#"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
if (currentLocation != nil) {
[currentLocation release];
}
currentLocation = newLocation;
[currentLocation retain];
}}
and On click event code I have just pass locationmanager.location.coordinate.latitude.....
NSString *url = [NSString stringWithFormat:#"http://.....Url..../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
U can directly change the CLLocation to NSString
NSString *lat=[NSString stringWithFormat:#"%f", newLocation.latitude];
NSString *lon=[NSString stringWithFormat:#"%f", newLocation.longitude];
The answers don't show it directly, but you should have assigned your controller as the CLLocationManagerDelegate to recieve the callbacks correct?
i need to get the speed of my device (meters per seconds) and this is my code the speed is always 0 i don't understand.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
double gpsSpeed2 = newLocation.speed;
labelm.text = [NSString stringWithFormat:#"%f",gpsSpeed2];
}
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
thanks
I never tried it before but probably its better if you set the distanceFilter in the location manager to a number like 1 meter and calculate the time. then calculate the speed.
You need to do the calculation yourself. Something like this:
if (oldLocation != nil)
{
CLLocationDistance distance = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval timeElapsed = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
// Use distance and timeElapsed to calculate speed
}
else {
// We don't have and old time, so can't calculate speed
}
oldLocation = [newLocation retain];