1

我正在使用谷歌 API 来获取地理坐标。到目前为止我一直在使用的代码如下

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {

NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
                    [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSStringEncodingConversionAllowLossy error:nil];
NSArray *items = [locationStr componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;

//NSLog(@"items array %@", items);

if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
    latitude = [[items objectAtIndex:2] doubleValue];
    longitude = [[items objectAtIndex:3] doubleValue];
}
else {
    //NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
}

CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}

我希望CLGeocoder代替 google API 从地址字符串中获取位置坐标。

我一直在使用CLGeocoder从在线示例中看到的。我发现使用CLGeocoder. 我尝试在CLGeocoder不改变我以前的代码的情况下使用 ,这样我就不必对现有代码进行大的更改。

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
    self.placemarksArray = placemarks;
    CLPlacemark *placeInfo = [placemarks objectAtIndex:0];
    NSString *latitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.latitude ];
    NSString *longitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.longitude ];
    NSLog(@"latitudeString %@", latitudeString);
    NSLog(@"longitudeString %@", longitudeString);

    _latitudes = placeInfo.location.coordinate.latitude;
    _longitudes = placeInfo.location.coordinate.longitude;
}];
location.latitude = _latitudes;
location.longitude = _longitudes;
return location;

但实际情况是,地图视图在块执行之前被加载。在这种情况下,不会返回位置的确切值。

我的问题是:

  1. 是否可以使用CLGeocoder,而无需对我以前的代码进行太多更改。或者我是否需要改变整个结构。
  2. 从我尝试过的第二个代码块开始,我做错了什么,我该如何纠正?

编辑:上面的方法是从调用viewDidLoad

CLLocationCoordinate2D mapCenter = [self getLocationFromAddressString:fullAddress];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            [annotationPoint setCoordinate:mapCenter];
            [annotationPointsArray addObject:annotationPoint];
addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName]autorelease];

 [mapView addAnnotation:addAnnotation];

CLLocationCoordinate2D这在从 getGeoLocation 方法获取纬度和经度值后执行。

4

3 回答 3

1

CLGeocoder异步发生(它在一个块中完成的事实放弃了这一点),因此_latitudes_longitudes没有在方法本身返回时设置。因此,很可能,是的,需要对您的代码进行一些轻微的重组。我们很难给出建议,因为我们没有看到调用这个例程的代码。最重要的是,您必须重构该代码,以便location您可以调用所需completionHandler的任何内容,而不是返回 .geocodeAddressString

查看您修改后的代码,您似乎viewDidLoad正在获取位置坐标并创建注释。只需在地理编码块内添加注释即可。

因此:

-(CLLocationCoordinate2D) geocodeAddressString:(NSString*)addressStr title:(NSString *)title subtitle:(NSString *)subtitle
{
    CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
    [geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
        self.placemarksArray = placemarks;
        CLPlacemark *placeInfo = [placemarks objectAtIndex:0];

        id<MKAnnotation> annotation = [[[MyAddressAnnotation alloc] initWithCoordinate:placeInfo.location.coordinate 
                                                                                 title:title
                                                                              SubTitle:subtitle] autorelease];

        [self.mapView addAnnotation:addAnnotation];
    }];
}

然后,viewDidLoad将其称为如下:

[self geocodeAddressString:fullAddress title:firstName subtitle:lastName]; 
于 2013-03-19T18:49:20.737 回答
0

没什么神奇的,但你必须在你的街区里戳你的地图视图:

self.mapView.centerCoordinate = placeInfo.location.coordinate //or equivalent of this, like latitudes and longitudes change

如果要避免地图视图被绘制两次,则需要拦截 MKMapview 的事件尤其是mapViewWillStartLoadingMap),在代码中实现它们,并放置适当的标志以避免绘制地图,直到尚未执行反向地理编码块.

于 2013-03-19T18:40:25.537 回答
0

试试这个答案。

调用方法有一些延迟(即 2 秒)。

[self performSelector:@selector(geoCodeUsingAddress:) withObject:userAddress afterDelay:2.0];

调用GetCurrentLocation函数

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)addressStr
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
       self.placemarksArray = placemarks;
       CLPlacemark *placeInfo = [placemarks objectAtIndex:0];

       NSString *latitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.latitude ];
       NSString *longitudeString = [NSString stringWithFormat:@"%f",placeInfo.location.coordinate.longitude ];

       NSLog(@"latitudeString %@", latitudeString);
       NSLog(@"longitudeString %@", longitudeString);

       _latitudes = placeInfo.location.coordinate.latitude;
       _longitudes = placeInfo.location.coordinate.longitude;
    }];

    location.latitude = _latitudes;
    location.longitude = _longitudes;

    return location;
}

希望对您有所帮助。

谢谢。

于 2013-03-20T04:10:27.727 回答