Using longitude and latitude to show location results in a incorrect location

3117
3
12-18-2013 05:44 AM
SomnusLee
New Contributor
Hi, all

I am new to arcgis for iOS, now I encounter a strange problem, and I have looked into other threads but haven't find any solutions.

I want to show a building which longitude is 116.929167 and latitude is 34.727222 on my graphic layer. My base map layer is in a
WGS84 spatial reference.

However, when I first tried the following code, it results in the point being misplaced.
Note: the place is somewhere in Jangsu Province, China, but it was placed in Hunan Province, China.

       
self.mapView.layerDelegate = self;
 
 //create an instance of a tiled map service layer
 AGSTiledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:kTiledMapServiceURL]];
 
 //Add it to the map view
 [self.mapView addMapLayer:tiledLayer withName:@"Tiled Layer"];
    
 //release to avoid memory leaks
    
    //set the callout delegate so we can display callouts
    self.mapView.callout.delegate = self;
    
    //add  graphics layer for the graphics
    self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
    
    //add the graphics layer to the map
    [self.mapView addMapLayer:self.graphicsLayer withName:@"Graphics Layer"];
    
    //zoom to china
    AGSEnvelope *chinaEnv = [AGSEnvelope envelopeWithXmin:78.0000000
                                                    ymin:0.4400000
                                                    xmax:156.0000000
                                                    ymax:75.0000000
                                        spatialReference:[AGSSpatialReference wgs84SpatialReference]];

    [self.mapView zoomToEnvelope:chinaEnv animated:YES];

    self.mapView.callout.customView = nil;

    self.mapView.callout.accessoryButtonHidden = YES;

    double longitude = 116.929167;
    double latitude = 34.727222;

    AGSPoint *graphicPoint = [AGSPoint pointWithX:longitude y:latitude spatialReference:[AGSSpatialReference wgs84SpatialReference]];

    [self.mapView.callout showCalloutAt:graphicPoint screenOffset:CGPointMake(0, 0) animated:YES];


Can anyone tell me why it cannot show the correct location and how to fix this problem?
Thank you very much for your time and answers!
Best Regards.
0 Kudos
3 Replies
Noah-Sager
Esri Regular Contributor
Hi Somnus,

Welcome to Esri Forums! I see that this is your first post, thank you for posting your question here.

I'm not 100% sure what the issue is, but I would suggest watching the web traffic made by the application when the point is added to the map. See if the this can be done with the simulator and Fiddler, using this documentation: 

Capture Traffic from iOS Device
http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigureForiOS

Watch the web requests and make sure the point is being added with the correct parameters. Hope this helps.

-Noah
0 Kudos
SomnusLee
New Contributor
Hi Somnus,

Welcome to Esri Forums! I see that this is your first post, thank you for posting your question here.

I'm not 100% sure what the issue is, but I would suggest watching the web traffic made by the application when the point is added to the map. See if the this can be done with the simulator and Fiddler, using this documentation: 

Capture Traffic from iOS Device
http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigureForiOS

Watch the web requests and make sure the point is being added with the correct parameters. Hope this helps.

-Noah


Hi, Noah

Thanks sincerely for your suggestion.

I found some information in other threads. Here is what I use now:

            //convert longitude and latitude to map point X Y
            double mercatorX = longitude * 0.017453292519943295 * 6378137.0;
            double a = latitude * 0.017453292519943295;
            double mercatorY = 3189068.5 * log((1.0 + sin(a))/(1.0 - sin(a)));
            
            AGSPoint *graphicPoint = [AGSPoint pointWithX:mercatorX y:mercatorY spatialReference:[AGSSpatialReference wgs84SpatialReference]];


After longitude and latitude converted, the building now can be placed in the right location on my map. I don't know why...
0 Kudos
Noah-Sager
Esri Regular Contributor
Hi Sonmus,

You are quite welcome. Thank you for posting your solution. I'm not 100% sure, but it looks like the spatial reference of the point was not being converted properly from geographic to web mercator with your previous code.
0 Kudos