1

我在使用 ArcGIS 获取我所在位置的位置服务时遇到问题。这是代码的一部分:

-(void)viewDidLoad {
    [super viewDidLoad];
    [self.activityView startAnimating];
    //self.mapView.layerDelegate = self;
    self.mapView.touchDelegate = self;
    self.mapView.calloutDelegate = self;
    NSURL *mapUrl = [NSURL URLWithString:kTiledMapServiceURL];
    AGSTiledMapServiceLayer *tiledLyr = [AGSTiledMapServiceLayer tiledMapServiceLayerWithURL:mapUrl];
    [self.mapView addMapLayer:tiledLyr withName:@"Tiled Layer"];

        //Create Bus Graphic layer
    AGSGraphicsLayer *busGraphicLayer = [AGSGraphicsLayer graphicsLayer];
    [self.mapView addMapLayer:busGraphicLayer withName:@"BusLayer"];

//Create Graphic layer
    AGSGraphicsLayer *graphicLayer = [AGSGraphicsLayer graphicsLayer];
    [self.mapView addMapLayer:graphicLayer withName:@"GraphicsLayer"];

    //Create Service layer
    AGSGraphicsLayer *serviceLayer = [AGSGraphicsLayer graphicsLayer];
    [self.mapView addMapLayer:serviceLayer withName:@"ServiceLayer"];

    //Create Path layer
    AGSGraphicsLayer *pathLayer = [AGSGraphicsLayer graphicsLayer];
    [self.mapView addMapLayer:pathLayer withName:@"PathLayer"];
 }

- (void) showMarkingOnMap:(Service *) ser
{
    id<AGSLayerView> graphicsLayerView = [self.mapView.mapLayerViews objectForKey:@"ServiceLayer"];
    AGSGraphicsLayer *graphicsLayer = (AGSGraphicsLayer*)graphicsLayerView.agsLayer;
    [graphicsLayer removeAllGraphics];

    // Create a symbols png graphic
    AGSPictureMarkerSymbol *genSymbol = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"pushpin.png"];
    ServiceInfoTemplate *infoTemplate = [[ServiceInfoTemplate alloc] init];
    AGSGraphic *genGraphic;
    AGSPoint *genPt;
    NSMutableDictionary *dic= [[NSMutableDictionary alloc] init];
    [dic setObject:[ser name] forKey:@"NAME"];
    if([ser.location isEqualToString: @"\n"] || (ser.location == nil)){
        [dic setObject:@"" forKey:@"DESC"];
    } else {
        [dic setObject:[ser location] forKey:@"DESC"];
    }
    genPt = [AGSPoint pointWithX:[[ser xcoordinate] floatValue]
                                y:[[ser ycoordinate] floatValue]
                 spatialReference:self.mapView.spatialReference];

    destinationX = [[ser xcoordinate] floatValue];
    destinationY = [[ser ycoordinate] floatValue];

    genGraphic = [[AGSGraphic alloc] initWithGeometry:genPt symbol:genSymbol attributes:dic infoTemplateDelegate:infoTemplate];
    [graphicsLayer addGraphic:genGraphic];

    [graphicsLayer dataChanged];

    [self.mapView zoomWithFactor:0.1 atAnchorPoint:CGPointMake(destinationX, destinationY) animated:NO];
    [self.mapView centerAtPoint:genPt animated:YES];
}

这个方法是我调用当前位置的地方

- (IBAction) directionAction:(id)sender{
    NSString *format = [NSString stringWithFormat:@"http://www....&routeStops=%f,%f;%f,%f&routemode=DRIVE&avoidERP=0"",         
                        self.mapView.gps.currentPoint.x, self.mapView.gps.currentPoint.y, destinationX, destinationY];

}

self.mapView.gps.currentPoint.x会给我返回一个 0 但它应该返回我所在的当前 x 坐标。有人知道它有什么问题吗?

4

1 回答 1

0

我没有从您的代码中看到您在 mapView 上启用了 gps 对象。您需要首先通过将enable 属性设置为 YES 来启用它。然后等待 gps 有一个有效的位置(currentPoint != nil),然后再尝试使用 currentPoint。您可以在 currentPoint 属性上设置一个键值观察器并等待它更改的通知并检查它是否有效然后使用它。

self.mapView.gps.enabled = YES;   // enable the GPS

// add the observer like this:
[self.mapView.gps addObserver:self forKeyPath:@"currentPoint" options:0 context:nil];

然后添加一个方法以在通知发生更改时获取通知:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"currentPoint"]) {
        AGSPoint *point = self.mapView.gps.currentPoint;
        if (point==nil) return;

        // now we have a valid gps location
    }
}

希望能给你一些想法。

于 2012-11-29T02:37:06.623 回答