【发布时间】:2014-03-20 04:55:43
【问题描述】:
我编写以下代码从地址获取纬度和经度值。我有 2 个地址字段 From 和 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];
}
第一次点击提交按钮后,输出为:
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)
第二次点击提交按钮后,输出为:
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)
如果我在获取 LaltLong 方法之后调用显示方法,我想知道为什么它第一次得到全零。
【问题讨论】:
标签: ios iphone google-maps ios7 google-geocoder