Select to view content in your preferred language

AGSPoints / AGSGraphics displaying at 0,0

900
1
08-31-2011 12:34 AM
PaulLohr
Frequent Contributor
Xcode 3.2
iOS SDK 4.2
ArcGIS for iOS SDK 2.0

I have lines in a comma-separated text file that I am reading into AGSPoints then putting the AGSPoints into AGSGraphics. The lines contain X and Y values. I push each AGSGraphic into an AGSGraphicsLayer. The problem is the AGSGraphic items are displaying at 0,0 on the map. Stepping through the code, the correct X and Y values are in myGraphicArray and are in myGraphicsLayer. Here is the code. Thank you for any help.

MapView.h

#import <UIKit/UIKit.h>
#import "ArcGIS.h"

@interface MapView : UIViewController {
    AGSMapView *_mapView;
    AGSSimpleMarkerSymbol *myMarkerSymbol;
    AGSGraphicsLayer *myGraphicsLayer;
}

@property (nonatomic, retain) IBOutlet AGSMapView *mapView;

- (void)loadPointsOnMap;

@end


MapView.m

#import "MapView.h"
#import "GPS1AppDelegate.h"
#import "TextFileMgmt.h"

@implementation MapView

@synthesize mapView = _mapView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        myMarkerSymbol = [[AGSSimpleMarkerSymbol alloc] init];
        myMarkerSymbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol];
        myMarkerSymbol.color = [UIColor blueColor];
  
  // create a graphics layer
  myGraphicsLayer = [[AGSGraphicsLayer alloc] init];
  myGraphicsLayer = [AGSGraphicsLayer graphicsLayer];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)viewDidLoad
{
    // get extent of points in current file including user's location
    // zoom to the new extent
    [super viewDidLoad];
    AGSTiledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc] 
                                           initWithURL:[NSURL URLWithString:@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"]];
    [self.mapView addMapLayer:tiledLayer withName:@"Aerial Basemap"];
    [tiledLayer release];
    
    NSLog(@"mapView: %@", self.mapView.spatialReference);
    
    // call local method to place points on map.
    [self loadPointsOnMap];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)loadPointsOnMap
{
    // alloc and init an instance of the TextFileMgmt class.
    TextFileMgmt *textFileMgmt1 = [[TextFileMgmt alloc] init];
    
    // run the parseTextFile method.
    [textFileMgmt1 parseTextFile];
    
    // now the points array should be filled with dictionary entries.
        
    // feed each point into an AGSPoint object array.
    NSMutableArray *myMarkerPointArray = [[NSMutableArray alloc] init];
    
    NSLog(@"textFileMgmt1 dict_x_long = %@", [textFileMgmt1.points valueForKey:@"dict_x_long"]);
    
    for ( NSDictionary *item in textFileMgmt1.points )
        {
            AGSPoint *myPoint = [[AGSPoint alloc] init];
            
            myPoint = [AGSPoint pointWithX:[[item valueForKey:@"dict_x_long"] doubleValue]
                                         y:[[item valueForKey:@"dict_y_lat"] doubleValue]
                          spatialReference:self.mapView.spatialReference];
            
            [myMarkerPointArray addObject:myPoint];
            [myPoint release];
        }
    NSLog(@"myMarkerPointArray contains: %@", myMarkerPointArray);
    // once this for loop completes, we have an NSMutableArray holding AGSPoint instances.
    
    // feed each AGSPoint from the myMarkerPoint array into an AGSGraphic object array.
    NSMutableArray *myGraphicArray = [[NSMutableArray alloc] init];
    
    for ( AGSPoint *item in myMarkerPointArray )
        {
            AGSGraphic *myGraphic = [[AGSGraphic alloc] init];
            myGraphic = [AGSGraphic graphicWithGeometry:item 
                                                 symbol:myMarkerSymbol 
                                             attributes:nil 
                                   infoTemplateDelegate:nil];
            
            [myGraphicArray addObject:myGraphic];
            [myGraphic release];
        }
    NSLog(@"myGraphicArray contains: %@", myGraphicArray);
    // once this for loop completes, we have an array of AGSGraphic instances.
    
    // send the AGSGraphic array to an AGSGraphicsLayer.
    
    for ( AGSGraphic *item in myGraphicArray )
        {
            NSLog(@"item contains: %@", item);
            // myGraphicsLayer was created in the init method.
            [myGraphicsLayer addGraphic:item];
        }
    // our content should be ready for the map so add the layer.
    NSLog(@"myGraphicsLayer: %@", myGraphicsLayer);
    [self.mapView addMapLayer:myGraphicsLayer withName:@"Graphics Layer"];
    [myGraphicsLayer dataChanged];
    [myGraphicsLayer release];
}


- (void)dealloc 
{
    self.mapView = nil;
    [super dealloc];
}

@end
0 Kudos
1 Reply
PaulLohr
Frequent Contributor
With some help, I found that AGSGraphics showing up at 0,0 is usually caused by a mismatch of coordinate systems. If your first added map layer (I call it the base or background layer) is in WGS84, then your overlaid AGSGraphics need to be in WGS84. The other way to handle the mismatch is convert the data to match the map's coordinate system. Like this:

AGSGeometry *myPointReprojected;
            
            AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];
            
            myPointReprojected = [ge projectGeometry:myPoint toSpatialReference:
[AGSSpatialReference webMercatorSpatialReference]];


Note that myPoint was instantiated by the AGSPoint class.
0 Kudos