programmatically added graphics always display at 0,0

717
5
Jump to solution
07-17-2012 09:43 AM
SpencerKohan
New Contributor
I'm new to ArcGIS, and I'm having trouble getting graphics I've added to the map programmatically to show up at the desired lat/lng coordinates.  Instead they display at (0,0) off the coast of Africa no matter what coordinates are passed in.  Here's the code I'm using to create and place the symbols:

                 graphicsLayer = [[AGSGraphicsLayer alloc] init];         [mapView insertMapLayer:graphicsLayer withName:@"graphics layer" atIndex:1];   AGSGraphic* g;    //Create the AGSSimpleMarker Symbol and set some properties  AGSSimpleMarkerSymbol* myMarkerSymbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol];  myMarkerSymbol.color = [UIColor blueColor];  myMarkerSymbol.style = AGSSimpleMarkerSymbolStyleDiamond;  myMarkerSymbol.outline.color = [UIColor whiteColor];  myMarkerSymbol.outline.width = 3;    //Create an AGSPoint (which inherits from AGSGeometry) that  //defines where the Graphic will be drawn    double lat = [[record objectForKey:@"lat"] doubleValue];  double lng = [[record objectForKey:@"lng"] doubleValue];    NSLog(@"Point: %f, %f", lat, lng);    AGSPoint* myMarkerPoint = [AGSPoint pointWithX:lat                  y:lng                                  spatialReference:mapView.spatialReference];    //Create the Graphic, using the symbol and  //geometry created earlier  g = [AGSGraphic graphicWithGeometry:myMarkerPoint           symbol:myMarkerSymbol              attributes:nil     infoTemplateDelegate:nil];          [graphicsLayer addGraphic:g];  


The map view is already loaded and displaying correctly when this code is executed.  If anyone can shed some light on what I might be missing it would be much appreciated.
0 Kudos
1 Solution

Accepted Solutions
PaulLohr
Occasional Contributor III
Nimesh helped me when my points were showing up at 0,0 as well. Thanks for helping us, Nimesh.

This is the code I used to put the points in the web mercator coordinate system:

NSMutableArray *myMarkerPointArray = [[NSMutableArray alloc] init];  // note that textFileMgmt1.points returns an NSDictionary  for ( NSDictionary *item in textFileMgmt1.points ) {             AGSPoint *myPoint;                          myPoint = [AGSPoint pointWithX:[[item valueForKey:@"dict_x_long"] doubleValue]                                          y:[[item valueForKey:@"dict_y_lat"] doubleValue]                           spatialReference:[AGSSpatialReference wgs84SpatialReference]];                                      AGSGeometry *myPointReprojected;                          AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];                          myPointReprojected = [ge projectGeometry:myPoint toSpatialReference:[AGSSpatialReference webMercatorSpatialReference]];             [myMarkerPointArray addObject:myPointReprojected];  }

View solution in original post

0 Kudos
5 Replies
LukePhilips
New Contributor III
A couple of issues that I see:
AGSPoint* myMarkerPoint = [AGSPoint pointWithX:lat y:lng spatialReference:mapView.spatialReference];
X is longitude, Y is Latitude
Then, what is the spatialReference of your map view? Is that the same SR as the lat long values you are getting? If your basemap is one of the ESRI maps, typically those are SR web mercator, and if your lat/long points are decimal degrees those are typically in SR WGS84. If you set the spatial reference of the point to WGS84 that should do it, but that's only based on the assumption of your error could be.
0 Kudos
SpencerKohan
New Contributor
Thanks for the feedback, I switched the X/Y coordinates and the result is the same.

It looks like the spacial reference wkid=102100, which it looks like is different from WGS84 (wkid=4326) as far as I can google.  I tried setting the spacial reference like so:


 AGSSpatialReference* ref = [AGSSpatialReference spatialReferenceWithWKID:4326];
 AGSPoint* myMarkerPoint = [AGSPoint pointWithX:lng
           y:lat
                   spatialReference:ref];



but I'm seeing the same result.  I'm assuming that's because the SR of the point doesn't match the SR of the map, so I'm thinking I need to convert my lng/lat coordinates to 102100 coordinates, does that sound right?

I've been put on this legacy project which has me jumping into the deep end with GIS so please excuse my ignorance.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Yes, you'll have to project your WGS 1984 geometry into Web Mercator. You got two options to do so....Use AGSGeometryGeographicToWebMercator function or use AGSGeometryEngine's projectGeometry method.

Hope this helps!

Regards,
Nimesh
0 Kudos
PaulLohr
Occasional Contributor III
Nimesh helped me when my points were showing up at 0,0 as well. Thanks for helping us, Nimesh.

This is the code I used to put the points in the web mercator coordinate system:

NSMutableArray *myMarkerPointArray = [[NSMutableArray alloc] init];  // note that textFileMgmt1.points returns an NSDictionary  for ( NSDictionary *item in textFileMgmt1.points ) {             AGSPoint *myPoint;                          myPoint = [AGSPoint pointWithX:[[item valueForKey:@"dict_x_long"] doubleValue]                                          y:[[item valueForKey:@"dict_y_lat"] doubleValue]                           spatialReference:[AGSSpatialReference wgs84SpatialReference]];                                      AGSGeometry *myPointReprojected;                          AGSGeometryEngine *ge = [AGSGeometryEngine defaultGeometryEngine];                          myPointReprojected = [ge projectGeometry:myPoint toSpatialReference:[AGSSpatialReference webMercatorSpatialReference]];             [myMarkerPointArray addObject:myPointReprojected];  }
0 Kudos
SpencerKohan
New Contributor
Thank you this worked perfectly!
0 Kudos