Reputation: 3812
I write the below code for getting Latitude and Longitude Values from an Address.I am having 2 Address fields From and To.
-(void)getLatLong:(NSString *)string
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"coordinate = (%f, %f)", coordinate.latitude, coordinate.longitude);
if([string isEqualToString:self.from.text])
{
fromLat = coordinate.latitude;
fromLng = coordinate.longitude;
}
else if([string isEqualToString:self.to.text])
{
toLat = coordinate.latitude;
toLng = coordinate.longitude;
}
}
}];
}
- (void)display
{
NSLog(@"%f",fromLat);
NSLog(@"%f",fromLng);
NSLog(@"%f",toLat);
NSLog(@"%f",toLng);
}
- (IBAction)getLatLongs:(id)sender
{
[self getLatLong:self.from.text];
[self getLatLong:self.to.text];
[self display];
}
After Clicking the Submit button for First time the output is:
2014-03-20 10:15:28.178 Application[746:70b] 0.000000
2014-03-20 10:15:28.179 Application[746:70b] 0.000000
2014-03-20 10:15:28.180 Application[746:70b] 0.000000
2014-03-20 10:15:28.181 Application[746:70b] 0.000000
2014-03-20 10:15:29.019 Application[746:70b] coordinate = (17.387337, 78.480835)
2014-03-20 10:15:29.134 Application[746:70b] coordinate = (16.516667, 80.616667)
After Clicking the Submit button for Second time the output is:
2014-03-20 10:15:28.178 Application[746:70b] 17.387337
2014-03-20 10:15:28.179 Application[746:70b] 78.480835
2014-03-20 10:15:28.180 Application[746:70b] 16.516667
2014-03-20 10:15:28.181 Application[746:70b] 80.616667
2014-03-20 10:15:29.019 Application[746:70b] coordinate = (17.387337, 78.480835)
2014-03-20 10:15:29.134 Application[746:70b] coordinate = (16.516667, 80.616667)
I want to Know Why it is Getting All Zero's for First time if i call display method after Getting LaltLong Method.
Upvotes: 1
Views: 2185
Reputation: 3812
i Solved My Problem Like This It's Working Fine
-(void)getLatLong:(NSString *)string
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"coordinate = (%f, %f)", coordinate.latitude, coordinate.longitude);
if([string isEqualToString:self.from.text])
{
fromLat = coordinate.latitude;
fromLng = coordinate.longitude;
}
else if([string isEqualToString:self.to.text])
{
toLat = coordinate.latitude;
toLng = coordinate.longitude;
}
}
}];
}
- (void)display
{
NSLog(@"%f",fromLat);
NSLog(@"%f",fromLng);
NSLog(@"%f",toLat);
NSLog(@"%f",toLng);
}
- (IBAction)getLatLongs:(id)sender
{
[self getLatLong:self.from.text];
[self getLatLong:self.to.text];
int64_t delayInSeconds = 1.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self display];
});
}
}
Upvotes: 0
Reputation: 1005
when you call [self getLatLong:self.from.text];
It calls [geocoder geocodeAddressString:string completionHandler:^{}];
which is GCD. Which runs on different thread.
Then you instantly calls [self display];
till then values are not received in GCD. thats why you are getting 0 first time.
Next time when you call [self display];
values are already present so it prints as it is.
Use breakpoints in -(void)display
and inside GCD to get more idea.
try to call [self display];
inside GCD.
-(void)getLatLong:(NSString *)string
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"coordinate = (%f, %f)", coordinate.latitude, coordinate.longitude);
if([string isEqualToString:self.from.text])
{
fromLat = coordinate.latitude;
fromLng = coordinate.longitude;
[self display];
}
else if([string isEqualToString:self.to.text])
{
toLat = coordinate.latitude;
toLng = coordinate.longitude;
[self display];
}
}
}];
}
- (void)display
{
NSLog(@"%f",fromLat);
NSLog(@"%f",fromLng);
NSLog(@"%f",toLat);
NSLog(@"%f",toLng);
}
- (IBAction)getLatLongs:(id)sender
{
[self getLatLong:self.from.text];
[self getLatLong:self.to.text];
}
Upvotes: 4