根据地址编译出经纬度

这个相比定位简单, 还要引这个头文件, 但是, 不涉及定位, 只是对地址翻译成经纬度, 不用locationManager, info.plist也不需要添加什么

#import 

然后添加输入地址的textField, 显示经纬度的label各一个, 还有一个开始编译的按钮

然后在按钮点击事件里面写这些, 就完事啦

- (void)beginLocate:(UIButton *)button{

    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];

    [myGeocoder geocodeAddressString:_textField.text completionHandler:^(NSArray *placemarks, NSError *error) {
    
        if ([placemarks count] > 0 && error == nil)
        {
            NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
            NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
            NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
        
        
            _labelOfLonitude.text =  [NSString stringWithFormat:@"经度: %lf", firstPlacemark.location.coordinate.longitude];
            _labelOfLatitude.text = [NSString stringWithFormat:@"纬度: %lf", firstPlacemark.location.coordinate.latitude];
        
        
        }
        else if ([placemarks count] == 0 && error == nil)
        {
            NSLog(@"Found no placemarks.");
        }
        else if (error != nil)
        {
            NSLog(@"An error occurred = %@", error);
        }
    }];

}

最后, 感谢http://www.2cto.com/kf/201506/412296.html

你可能感兴趣的:(根据地址编译出经纬度)