在字符串中查找纬度和经度

时间:2012-10-24 18:51:10

标签: ios ios5 map ios6

想象一下,您正在地图上添加注释(视图)并附加纬度和经度字符串并将其放入URL以获取每个单独的地图注释信息。 我的问题是,一旦我选择要删除的注释,我将如何在用于URL请求的字符串中找到所选的注释纬度和经度。

例如

www.something.com/39.001,29.002;34.0567,-32,0091;56.987,76.435

然后假设您删除了注释34.0567,-32,0091

如何在以下

中更新字符串
www.something.com/39.001,29.002;56.987,76.435

2 个答案:

答案 0 :(得分:2)

将网址转换为NSMutableString以便“修改”网址,然后替换该字符串中地标的匹配项。然后将字符串转回URL:

NSURL *currentURL = [NSURL URLWithString:@"www.something.com/39.001,29.002;34.0567,-32,0091;56.987,76.435"];

NSMutableString *absolute = [NSMutableString stringWithString:[currentURL absoluteString]];
[absolute replaceOccurrencesOfString:@"34.0567,-32,0091;" withString:@"" options:0 range:NSMakeRange(0, [absolute length])];

NSURL *newURL = [NSURL URLWithString:absolute];

NSLog(@"My new URL = %@", newURL.absoluteString);

编辑--->更新的代码,包括已更改地标的索引。

NSString *domain = @"www.something.com/";
NSURL *currentURL = [NSURL URLWithString:@"www.something.com/39.001,29.002;34.0567,-32,0091;56.987,76.435"];

NSMutableString *absolute = [NSMutableString stringWithString:[currentURL absoluteString]];
[absolute replaceOccurrencesOfString:domain withString:@"" options:0 range:NSMakeRange(0, [absolute length])];

NSArray *placemarks = [absolute componentsSeparatedByString:@";"];

NSString *placemarkToRemove = @"34.0567,-32,0091";

NSUInteger index = [placemarks indexOfObject:placemarkToRemove];

[absolute replaceOccurrencesOfString:[placemarkToRemove stringByAppendingString:@";"] withString:@"" options:0 range:NSMakeRange(0, [absolute length])];

NSURL *newURL = [NSURL URLWithString:absolute];

NSLog(@"Placemark Index = %u; My new URL = %@", index, newURL.absoluteString);

答案 1 :(得分:2)

还有另外一种方法。由于您使用带注释的地图,您可以随时获取注释列表,然后将它们传递给构建网址的方法:

- (NSURL *)makeUrlFromAnnotations:(NSArray *)annotations
{
    NSString *baseUrl = @"www.something.com/";
    NSMutableArray *annotationStrings = [[NSMutableArray alloc] initWithCapacity:0];
    for (id <MKAnnotation> annotation in annotations)
    {
     [annotationStrings addObject:
      [NSString stringWithFormat:@"%f,%f",
       annotation.coordinate.latitude,
       annotation.coordinate.longitude]
     ];
    }

    return [NSURL URLWithString:[baseUrl stringByAppendingPathComponent:[annotationStrings componentsJoinedByString:@";"]]];
}

然后,每当你想要一个带坐标的网址时,只需致电:

NSURL *url = [self makeUrlFromAnnotations:self.myMapview.annotations];
//Or whatever property is your mapview