简体   繁体   English

从文本字段获取值以传递给CLGeocoder

[英]Getting values from text fields to pass to CLGeocoder

I'm trying to display the latitude and longitude values of an address which the user has input into some text fields. 我正在尝试显示用户已在某些文本字段中输入的地址的纬度和经度值。 I would ideally like the user to input the address, then click a button which will find the lat/lon and feed these values into two text fields in the same view. 理想情况下,我希望用户输入地址,然后单击一个按钮,它将找到经纬度并将这些值输入到同一视图中的两个文本字段中。 I'm just not sure how I call the address values of these text fields in my code. 我只是不确定如何在代码中调用这些文本字段的地址值。

Here's what I have so far 这是我到目前为止的

-(IBAction)getLatLon:(id)sender{

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
NSString *addressString = @"%@address, city, county, county, postcode";
[geocoder geocodeAddressString:addressString inRegion:nil completionHandler:^(NSArray* placemarks, NSError* error)
 {
     for (CLPlacemark * aPlacemark in placemarks)
     {                    // Process the placemark.
         NSLog(@"Got Placemark : %@", aPlacemark);     

     }
 }];

} }

It works when I hard code a value you in for example 例如,当我对您的值进行硬编码时,它会起作用

NSString *addressString = @"Belfast";

I know I've probably tried to call the values in all wrong but my head is completely fried at this stage! 我知道我可能曾经尝试过错误地调用这些值,但是在这个阶段我的头已经完全炸了!

My Textfields are named as such 我的文本字段就是这样命名的

addressText.text
cityText.text
countyText.text
countryText.text
postcodeText.text

You need to make use of the stringWithFormat: method. 您需要使用stringWithFormat:方法。 Put in placeholders in your string (%@) for each string you want to use, and then list out the arguments after (are you familiar with printf ?) 为每个要使用的字符串在字符串(%@)中放入占位符,然后在其后列出参数(您对printf熟悉吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM