How to create Latitude-Longitude bounds in iPhone using two NE and SW latitude longitude coordinates and I want to check whether my current latitude longitude is in within my latitude longitude bound.
I created a bound using following latitude longitude coordinates:
double minLng = 7.880571
double maxLng = 8.357336
double minLat = 49.108775
double maxLat = 49.288638
I want to check my other latitude longitude is within this bound or not.
Try like this,
CLLocationManager *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 {
if(newLocation.coordinate.latitude>=49.108775 && newLocation.coordinate.latitude<=49.288638 && newLocation.coordinate.longitude>=7.880571 && newLocation.coordinate.longitude<=8.357336 ) {
// It is in the bounds
}
}
If by current you mean the device's current location, you need to look into the CLLocationManager class and its delegate methods, namely the location property which contains the information about current latitude and longitude. The rest is pretty straightforward.
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 am new to iphone
by using CLLocation i can find latitude and longitude but it is not working in iphonesimulater4.0 working in 3.2.1,please any one tell me how to calculate latitude & longitude in iphone4.0.
Thanks in advance
CLLocationCoordinate2D location =
[[[mapview userLocation] location] coordinate];
//NSLog(#"Location found from Map: %f %f",
location.latitude,location.longitude);
this concept use in directly in ur app delegte and check it ur latitude and long.
#pragma mark locationManager delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
if(abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120)
{
//CLLocation *test = [[CLLocation alloc] initWithLatitude:-33.857034 longitude:151.035929];
currentLocation = newLocation;
coordinates = newLocation.coordinate;
latitude = currentLocation.coordinate.latitude;
longitude = currentLocation.coordinate.longitude;
[locationManager stopUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"%#", [error description]);
}
Location only shows on a real device. You may find the values in the CLLocationManager will work in the Simulator (possibly from your exchange - although it wont be very accurate) but the blue pin on the map will always be in CA!
I am a new iphone developer,please help me to find out latitude and longitude of different locations(present location values, distance 10m form present locatoin)and find address of that locations using that latitude and longituse values.
using mapkit framework you can find the current location along with near by location with their latitude & longitude
CLLocationCoordinate2D location;
location = [mMapView.userLocation coordinate];
if(iLat && iLng) {
location.latitude = [iLat floatValue];
location.longitude = [iLng floatValue];
}
In order to find out lat & long use this link with complete instruction & sample
http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-application
And in order to get location address using latitude & longitude this can be done by MKReverseGeoCoder.... get in detail using following link
http://blog.objectgraph.com/index.php/2009/04/03/iphone-sdk-30-playing-with-map-kit-part-2/
If your new new to Iphone development, you might wanna return that phone and start developing on Android!
Na just kidding, this will work indeed:
CLLocationCoordinate2D location; location = [mMapView.userLocation coordinate];
if(iLat && iLng) {
location.latitude = [iLat floatValue];
location.longitude = [iLng floatValue];
}
What you want to do is called reverse geocoding. On iOS, this is easily done using MapKit's MKReverseGeocoder.
Here's what you need to do:
First, check if location services is enabled:
if ([CLLocationManager locationServicesEnabled])
[self findUserLocation];
Then in your findUserLocation method, instantiate your CLLocationManager and start receiving updates:
- (void)findUserLocation
{
CLLocationManager *locationManager_ = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracy{ChooseYourOptionHere};
[locationManager startUpdatingLocation];
}
You then implement this delegate method, which will get called every time an update is received. Check the current location's accuracy against your desired accuracy (set above), center the map if you are happy with it AND don't forget to stop the location manager from receiving updates:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if ([newLocation horizontalAccuracy] < [manager desiredAccuracy])
{
// Accuracy is good enough, let's reverse geocode it
[manager stopUpdatingLocation];
[self reverseGeocodeWithCoordinate:newLocation.coordinate];
}
// else keep trying...
}
Once you're happy with the location accuracy, you can start the reverse geocoding process:
- (void)reverseGeocodeWithCoordinate:(CLLocationCoordinate*)coordinate
{
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
geocoder.delegate = self;
[geocoder start];
}
And you will receive the response via delegate methods:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
geocoder.delegate = nil;
[geocoder autorelease];
// Reverse geocode failed, do something
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
geocoder.delegate = nil;
[geocoder autorelease];
// Reverse geocode worked, do something...
}
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];