1

我有一个 .xib 文件,用户可以在其中输入 UITextField 中某个地点的位置名称,然后单击搜索。然后将加载另一个 .xib 文件,用户将在其中看到带有地图位置的 MapView 对象。

我制作了一个自定义的 init 方法,当用户单击按钮时,我按如下方式传递字符串:

AddLocViewController *locView = [[AddLocViewController alloc] initWithNibName:nil bundle:nil customLoc:textLoc.text];
[self presentModalViewController:locView animated:YES];

在名为 AddLocViewController.m 的新 ViewController 类中,init 方法和 viewDidLoad 如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil customLoc:(NSString*)_loc
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        stringLoc1 = _loc;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];    
    [self showAddress];
}

这里 [self showAddress] 调用一个方法来显示 mapView 对象上的内容,如果我有一个 UITextField 接受输入并单击一个按钮将值设置为 NSString 对象 stringLoc 并在 showAddress 方法中使用它,那么 showAddress 方法可以完美地工作:

- (void) showAddress
{
    MKCoordinateRegion region;
MKCoordinateSpan span;

span.latitudeDelta=0.01f;
span.longitudeDelta=0.01f;

    stringLoc1 = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:stringLoc1]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

double aLatitude = 0.0;
double aLongitude = 0.0;

if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
    aLatitude = [[listItems objectAtIndex:2] doubleValue];
    aLongitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
    [addressField setText: @"Address Location error"];
    return;
}

[addressField resignFirstResponder];

CLLocationCoordinate2D location = {latitude: aLatitude, longitude:aLongitude};

region.span=span;
region.center.latitude = aLatitude;
    region.center.longitude = aLongitude;

    if(addAnnotation != nil) {
    [mapView removeAnnotation:addAnnotation];
    //[addAnnotation release];
    addAnnotation = nil;
}

    addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
    [mapView addAnnotation: addAnnotation];

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}

请帮助我将此处的参数传递给此类并适当地使用 showAddress 方法,因为它现在仅在从类中搜索位置时显示正确的位置,并且在实例化时不显示正确的位置。

4

1 回答 1

1

在这里,您使用地址字段的内容覆盖 stringLoc1:无论其初始值如何,它都会被覆盖。如果 textField 为空,则它被空字符串覆盖。

stringLoc1 = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

我不确定在剩余代码的上下文中最好的解决方案是什么。但这应该有效:

NSString *currentLoc;

if (addressField.text && ([addressField.text length] == 0)) {
  currentLoc = addressFiled.text;
  stringLoc1 = addressField.text; // this line will set the current value as default value for the next call of the method; unless a new init call comes in between.
}
else
  currentLoc = stringLoc1; 


    currentLoc = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [currentLoc  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:currentLoc ]];

我相信有更聪明的方法可以做到这一点。但这应该对您与我们共享的代码视图行进行最小的更改。

于 2012-07-11T07:46:29.630 回答