1

想象一下,您在地图(视图)上添加注释并附加纬度和经度字符串并将其放入 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
4

2 回答 2

2

将 URL 转换为 asNSMutableString以“编辑”该 URL,然后替换该字符串中出现的地标。然后将字符串转回 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);
于 2012-10-24T19:00:04.720 回答
2

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

- (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
于 2012-10-24T19:55:55.033 回答