2012-08-03 114 views
1

我在定位服務中遇到問題,它獲取我在哪裏使用ArcGIS的位置。下面是部分代碼: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座標我在哪裏。 任何人都知道它有什麼問題嗎?

+0

你應該嘗試包括一些更相關的代碼。地圖視圖如何設置,它依賴於什麼?有沒有任何初始化方法你沒有調用等?編輯 – Jasarien 2012-08-03 08:21:21

+0

會不夠? – sihao 2012-08-03 08:37:00

+0

什麼是self.mapView.gps?什麼數據類型是x?您正在嘗試記錄一個對象(使用%@),但座標通常表示爲雙打。 – sosborn 2012-08-03 08:38:06

回答

0

我從代碼中看不到您啓用了mapView上的gps對象。您需要先將enable property設置爲YES來啓用它。然後在嘗試使用currentPoint之前,等待GPS獲取有效位置(currentPoint!= nil)。您可以在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]; 

然後添加一個方法來得到通知時,它changess:

- (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 
    } 
} 

希望給你一些想法。