ios: Debug custom location not working - ios

In simulator I set debug->location->custom location latitude and longitude.
Then in code I have:
- (void)updateMap{
MKCoordinateSpan span;
span.latitudeDelta = .6;//The amount of north-to-south distance (measured in degrees) to display on the map
span.longitudeDelta = .6;//The amount of east-to-west distance (measured in degrees) to display for the map region
MKCoordinateRegion region;
region.center = _ls.coordinate;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
In a different file I obtain #property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//Get the latitude and longitude location
CLLocation *lastLocation = [locations lastObject];
_latitude = [[NSString alloc] initWithFormat:#"%f", lastLocation.coordinate.latitude];
_longitude = [[NSString alloc] initWithFormat:#"%f", lastLocation.coordinate.longitude];
_coordinate = lastLocation.coordinate;//manager.location.coordinate;
}
When I run the app the simulator keeps showing San Francisco. Any ideas why?

It is the default location for the simulator. To simulate the location use the drop down in the Xcode window to set a location or a "Path" to simulate a movement.

Got it working:
(1)I just had to shut down xcode and simulator and reload the program. Now it loads my location.:)

Related

How to implement geofencing code for apple map ios 9

I have two locations one is the driver and other is the rider.I have lat n long available for both.I want to hit an api when the driver enters in the geofence area of the riders location.
i went through QKGeofenceManager demo project:
using this i can provide lat n long and the radius to find geofence.
But the issue is do i have to update driver location every time in background and what condition should be applied so the the callback is made when the driver enters the geofence area of rider.If the ap is in background how will it handle everything.
Do i have to make any changes in appdelegate
- (NSArray *)geofencesForGeofenceManager:(QKGeofenceManager *)geofenceManager
{
NSArray *fetchedObjects = [self.fetchedResultsController fetchedObjects];
NSMutableArray *geofences = [NSMutableArray arrayWithCapacity:[fetchedObjects count]];
for (NSManagedObject *object in fetchedObjects) {
NSString *identifier = [object valueForKey:#"identifier"];
CLLocationDegrees lat = [[object valueForKey:#"lat"] doubleValue];
CLLocationDegrees lon = [[object valueForKey:#"lon"] doubleValue];
CLLocationDistance radius = [[object valueForKey:#"radius"] doubleValue];
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(lat, lon);
CLCircularRegion *geofence = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:identifier];
[geofences addObject:geofence];
}
return geofences;
}
I found an alternative to achieve this task.
As my delegate method of Didenterlocation was not called,i applied another approach.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power
NSLog(#"%# locations",locations);
float Lat = _locationManager.location.coordinate.latitude;
float Long = _locationManager.location.coordinate.longitude;
NSLog(#"Lat : %f Long : %f",Lat,Long);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(28.58171,77.2915457);
NSLog(#"center check %#",center);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center
radius:500
identifier:#"new region"];
BOOL doesItContainMyPoint = [region containsCoordinate:CLLocationCoordinate2DMake(Lat,Long)];
NSLog(#"success %hhd", doesItContainMyPoint);
}
by keeping track of the current location,whenever the current location coordinates enter into the coordinates of the center region,you can fire a notification that the user has entered this particular region.

iOS Compass App : Location coordinates keeps changing. How to stabilitse it?

I have just completed compass app which will show distance between 2 coordinates.
Here is working code :
....
// created a timer to call locationUpdate method : 5sec
[NSTimer scheduledTimerWithTimeInterval:5 target: self selector: #selector(locationUpdate) userInfo: nil repeats: YES];
....
-(void)locationUpdate {
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
{
//To get the current location Latitude & Longitude.
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;
startPoint = [[CLLocation alloc] initWithLatitude: latitude longitude: longitude ]; //Current Latitude and Longitude
endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude -----------------------------------> Need to come from database.
//To get the distance from the 2 coordinates in feet
CLLocationDistance distInMeter = [startPoint distanceFromLocation:endPoint];
//Lable veiw to update remaining distance.
if(distInMeter > 999.000f) {
self.labelLongLat.text = [NSString stringWithFormat: #"Remainig distance %.2f KM with Lat : %lf LAN %lf", distInMeter / 1000, latitude, longitude ];
}else
self.labelLongLat.text = [NSString stringWithFormat: #"Remainig distance %.2f M Lat : %lf LAN %lf" , distInMeter, latitude,longitude ];
}
My problem is while updating location for each 5sec, the coordinates varies a lot. That will result in Remaining distance calculation. Which is highly unstable!! How can I fix this?
Thanks in Advance,
The delegate method you are using is deprecated. You should use locationManager:didUpdateLocations:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude -----------------------------------> Need to come from database.
//To get the distance from the 2 coordinates in meters
CLLocationDistance distInMeter = [currentLocation distanceFromLocation:endPoint];
//Label view to update remaining distance.
if(distInMeter > 999 ) {
self.labelLongLat.text = [NSString stringWithFormat: #"Remaining distance %.2f Km with Lat : %lf LAN %lf", distInMeter / 1000, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
} else {
self.labelLongLat.text = [NSString stringWithFormat: #"Remaining distance %.2f m Lat : %lf LAN %lf" , distInMeter, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
}
}
The accuracy of your location can be affected by signal quality (tall buildings, indoor location etc). You can examine the horizontalAccuracy property of your location to see how accurate the position is. If the accuracy is low then you can defer updating your label. Beware that you may never get an accurate fix.
One strategy is to wait for an accuracy <20m or after 5 updates -
#property (nonatomic) NSInteger locationUpdates;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
self.locationUpdates++;
CLLocation *currentLocation = [locations lastObject];
if (currentLocation.horizontalAccuracy < 20.0 || self.locationUpdates>5) {
endPoint = [[CLLocation alloc] initWithLatitude: 12.923670 longitude: 77.573496]; //Target Latitude and Longitude -----------------------------------> Need to come from database.
//To get the distance from the 2 coordinates in meters
CLLocationDistance distInMeter = [currentLocation distanceFromLocation:endPoint];
//Label view to update remaining distance.
if(distInMeter > 999 ) {
self.labelLongLat.text = [NSString stringWithFormat: #"Remaining distance %.2f Km with Lat : %lf LAN %lf", distInMeter / 1000, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
} else {
self.labelLongLat.text = [NSString stringWithFormat: #"Remaining distance %.2f m Lat : %lf LAN %lf" , distInMeter, currentLocation.coordinate.latitude, currentLocation.coordinate.longitude ];
}
}
}

Is it possible to add pins to map kit solely based on physical address [duplicate]

This question already has answers here:
iOS - MKMapView place annotation by using address instead of lat / long
(7 answers)
Closed 8 years ago.
Is it possible to add pins to map kit solely based on physical address or is there a way to get long and lat based on physical address
Yes, You can get lat long from the address.
Please refer this answer : iOS - MKMapView place annotation by using address instead of lat / long
Basically it does this :
NSString *location = #"some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
}
];

how to create MKCoordinateRegion

How to create MKCoordinateRegion .
NSString *latitudeString = [locationString substringToIndex:startRange.location];
NSString *longtitudeString = [locationString substringWithRange:NSMakeRange(startRange.location+2,((endRange.location-1)-(startRange.location+2)))];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake((int)latitudeString, (int)longtitudeString);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 500, 500);`
I get the error "Invalid Region center:+392128672.00000000, +392128704.00000000 span:+0.00448287, -0.01195557"
Remove the type casting. Use below code.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([latitudeString doubleValue], [longtitudeString doubleValue]);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 500, 500);
If you store your latitude and longitude in a string like this:
NSString *latitudeString = #"12.2323";
You should convert it to a float, like this:
CGFloat latitude = [latitudeString floatValue];
And after that, you can this in your
CLLocationCoordinate2DMake
method, it should work. And the problem comes from, that the latitude can only between -90 and 90, the longitude between -180 and 180 (degree), and your numbers are way bigger than that.

get current address of user in xcode?

I need to get the exact current address (country,state,city) of user. So I have gone to find latitude and longitude and then find out from it by using reverse geocoding.But unable to get latitude and longitude itself.I m using xcode 4.1 and testing in iphone simulator.
This is the code I m working on:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
NSLog(#"%#", [self deviceLocation]);
}
- (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];
latLabel.text = lat;
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];
longLabel.text = longt;
}
How can I find the latitude and longitude and thereby find the address of the user?
EDIT:
Updated my version to Xcode 4.5. But still couldnot see the location ....?Y is it so?
please download this file
In the zip file I attached, there are 4 files:
LocationGetter.h and .m
PhysicalLocation.h and .m
you need to just import
#import "PhysicalLocation.h"
PhysicalLocation *physicalLocation = [[PhysicalLocation alloc] init];
[physicalLocation getPhysicalLocation];
in getPhysicalLocation at PhysicalLocation.m class
#pragma mark MKReverseGeocoder Delegate Methods
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSLog(#"%#",placemark);
objcntrAppDelegate = (yourappDeleger *)[[UIApplication sharedApplication]delegate];
objcntrAppDelegate.strCountry=placemark.country;
objcntrAppDelegate.strSuburb=placemark.subAdministrativeArea;
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReceivedAddress" object:nil userInfo:nil];
[geocoder autorelease];
}
You get all of you neat lat, lng, current city, country, all that you need. Hope it helps you.
NOTE:- simulator gives incorrect result, you must test on device.
UPDATE
UpdatedDEMO
Change :
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
Now latitude and longitude should be obtained as float not int.
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
float longitude=coordinate.longitude;
float latitude=coordinate.latitude;
Find address of the user by latitude and longitude using CLGeocoder.
EDIT : Refer this link.

Resources