代码之家  ›  专栏  ›  技术社区  ›  Greg Hurlman

如何从地址确定cllocation或mkplacemark?

  •  2
  • Greg Hurlman  · 技术社区  · 15 年前

    我看过一个又一个API文档和演示,演示了如何执行反向地理代码——在给定lat/long位置的情况下获取地址。我需要做相反的事情。我假设这已经解决了问题,因为苹果的Mapkit API完全避免了这个问题。

    1 回复  |  直到 15 年前
        1
  •  6
  •   ennuikiller    15 年前

    一种方法是像这样使用GoogleWebService:

    -(CLLocationCoordinate2D) addressLocation {
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                               [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
        NSLog(@"locationString = %@",locationString);
    
        NSArray *listItems = [locationString componentsSeparatedByString:@","];
    
        double latitude = 0.0;
        double longitude = 0.0;
        if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
            latitude = [[listItems objectAtIndex:2] doubleValue];
            longitude = [[listItems objectAtIndex:3] doubleValue];
    /*      
            NSString *nameString = [NSString stringWithFormat:@"http://ws.geonames.org/findNearestAddress?lat=%f&lng=%f",latitude,longitude];
            NSString *placeName = [NSString stringWithContentsOfURL:[NSURL URLWithString:nameString]];
            NSLog(@"placeName = %@",placeName);
     */
        }
        else {
            //Show error
        }
        CLLocationCoordinate2D location;
        location.latitude = latitude;
        location.longitude = longitude;
    
        return location;
    
    }