0
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

    //[_mapview setMapType:MKMapTypeStandard];
    CLLocationCoordinate2D center = _mapview.centerCoordinate;
    double lat = center.latitude;
    double lng = center.longitude;
    NSString *str = [NSString stringWithFormat:@"%f",lat];
    NSString *str1 = [NSString stringWithFormat:@"%f",lng];
    _newlat = str;
    _newlong=str1;

    NSLog(@"%@ %@",_newlat,_newlong);
}

我在这个方面得到了很长的纬度值,但在这个委托方法之外没有。当我跳到上一课时,我想要上一课中的这个值。

我确实声明了委托,但每次我拖动地图时它都会被调用,并且我没有在上一课中获得价值。

4

1 回答 1

0

据我了解,您需要设置观察坐标的观察者对象,并在新坐标到来时得到通知。

在您的 CLLocationManagerDelegate 类 .m

-(void) setLocationObserver:(id) observer
{
   self.locationObserver = observer
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

    //[_mapview setMapType:MKMapTypeStandard];
    CLLocationCoordinate2D center = _mapview.centerCoordinate;
    double lat = center.latitude;
    double lng = center.longitude;
    NSString *str = [NSString stringWithFormat:@"%f",lat];
    NSString *str1 = [NSString stringWithFormat:@"%f",lng];
    _newlat = str;
    _newlong=str1;

    if ([self.locationObserver respondsToSelector:@selector(setNewCoords:)
        [self.locationObserver setNewCoords:center];

    NSLog(@"%@ %@",_newlat,_newlong);
}

在你以前的课.m中:

-(void)-(void) setNewCoords:(CLLocationCoordinate2D*) location
{
    self.myLocation = location;
    [self doSeomethingWithNewLocation]; // if you wish
}

-(void) doSomethingWithNewLocation
{
    NSString * _newlat = [NSString stringWithFormat:@"%f",self.myLocation.latitude];
    NSString * _newlong = [NSString stringWithFormat:@"%f",self.myLocation.longitude];
}

粗略的例子,但可能会有所帮助。

于 2012-10-18T10:10:36.200 回答