AGSGeometryEngine projectGeometry

2981
9
09-15-2011 04:01 AM
PaulLohr
Occasional Contributor III
I have not been able to find any samples that use AGSGeometryEngine projectGeometry: toSpatialReference:. Xcode is telling me that it cannot find a method named projectGeometry:toSpatialReference. The warning messages are:

warning: Semantic Issue: Class method '+projectGeometry:toSpatialReference:' 
not found (return type defaults to 'id')

'AGSGeometryEngine' may not respond to '+projectGeometry:toSpatialReference:'


Here is the code that generates the warning:

(please note that myPoint is an AGSPoint...a type of AGSGeometry with it's spatial reference being 4326 (WGS1984))

AGSPoint *myPointReprojected;

myPointReprojected = [AGSGeometryEngine projectGeometry:myPoint 
                                   toSpatialReference:[AGSSpatialReference webMercatorSpatialReference]];


Thank you for even the smallest amount of help,
Paul Lohr
0 Kudos
9 Replies
NimeshJarecha
Esri Regular Contributor
You must initialize geometry engine first. Your code should be..

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


Or

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


Regards,
Nimesh
0 Kudos
PaulLohr
Occasional Contributor III
As always, you knew how to fix it. Thank you, Nimesh.

One thing that seems odd - I had to change:
"AGSPoint *myPointReprojected;"
to
"AGSGeometry *myPointReprojected;"

Before I made this change, I got a warning messaging stating "Incompatible pointer types assigning AGSPoint from AGSGeometry".

I see in the documentation that projectGeometry:toSpatialReference outputs an AGSGeometry object. I thought if I were to input an AGSPoint, the method would return an AGSPoint (AGSPoint inherits from AGSGeometry). Instead it returns an AGSGeometry object.

No complaints here, just glad it works and appreciative of the help.

Paul Lohr
0 Kudos
NimeshJarecha
Esri Regular Contributor
You're welcome, Paul!

The project method supports all geometry types hence the return type is AGSGeometry. If you pass input as AGSPoint then it returns mutable AGSPoint only but as AGSGeometry. You can cast it accordingly.

Regards,
Nimesh
0 Kudos
LucaAlferi1
New Contributor
hi
i use a AGSMapViewLayer with WKID = 102100.
i use a geocode server locator with URL = "http://tasks.arcgis.com/ArcGIS/rest/services/WorldLocator/GeocodeServer" (WKID = 4326)
When i search  an address the mapView go to 0,0 coordinate.
I tried to reproyect the address.candidate point with this code:

AGSGeometry *myPointReprojected;
myPointReprojected = [[AGSGeometryEngine defaultGeometryEngine] projectGeometry:pt toSpatialReference:[AGSSpatialReference spatialReferenceWithWKID:102100]];


where pt is

AGSPoint *pt = addressCandidate.location;

Don't work.
Please Help me
0 Kudos
LucaAlferi1
New Contributor
The complete code is this:
- (void)locator:(AGSLocator *)locator operation:(NSOperation *)op didFindLocationsForAddress:(NSArray *)candidates{
    
    //check and see if we didn't get any results
 if (candidates == nil || [candidates count] == 0)
 {
        //show alert if we didn't get results
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Informazione"
                                                        message:@"Nessun risultato trovato"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
 }
 else
 {
        //use these to calculate extent of results
        double xmin = DBL_MAX;
        double ymin = DBL_MAX;
        double xmax = -DBL_MAX;
        double ymax = -DBL_MAX;
  
  //create the callout template, used when the user displays the callout
  self.calloutTemplate = [[[AGSCalloutTemplate alloc]init] autorelease];
        
        //loop through all candidates/results and add to graphics layer
  for (int i=0; i<[candidates count]; i++)
  {            
   AGSAddressCandidate *addressCandidate = (AGSAddressCandidate *)[candidates objectAtIndex:i];
            
            //get the location from the candidate
            AGSPoint *pt = addressCandidate.location;
            
            //accumulate the min/max
            if (pt.x  < xmin)
                xmin = pt.x;
            
            if (pt.x > xmax)
                xmax = pt.x;
            
            if (pt.y < ymin)
                ymin = pt.y;
            
            if (pt.y > ymax)
                ymax = pt.y;
            AGSGeometry *myPointReprojected;
            myPointReprojected = [[AGSGeometryEngine defaultGeometryEngine] projectGeometry:pt toSpatialReference:[AGSSpatialReference spatialReferenceWithWKID:102100]];
   //create a marker symbol to use in our graphic
            AGSPictureMarkerSymbol *marker = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"BluePushpin.png"];
            marker.xoffset = 9;
            marker.yoffset = -16;
            marker.hotspot = CGPointMake(-9, -11);
            //set the text and detail text based on 'Name' and 'Descr' fields in the attributes
            self.calloutTemplate.titleTemplate = self.searchBar.text;
            //self.calloutTemplate.detailTemplate = @"${Descr}";
   
            //create the graphic
   AGSGraphic *graphic = [[AGSGraphic alloc] initWithGeometry: pt
                symbol:marker 
               attributes:[[addressCandidate.attributes mutableCopy] autorelease]
                                                  infoTemplateDelegate:self.calloutTemplate];
            
            
            //add the graphic to the graphics layer
            
   [self.graphicsLayer addGraphic:graphic];
            
            if ([candidates count] == 1)
            {
                //we have one result, center at that point
                [self.mapView5 centerAtPoint:pt animated:NO];
                
    // set the width of the callout
    self.mapView5.callout.width = 250;
                
                //show the callout
                [self.mapView5 showCalloutAtPoint:(AGSPoint *)graphic.geometry forGraphic:graphic animated:YES];
            }
   
   //release the graphic bb  
   [graphic release];            
  }
        
        //if we have more than one result, zoom to the extent of all results
        int nCount = [candidates count];
        if (nCount > 1)
        {            
            AGSMutableEnvelope *extent = [AGSMutableEnvelope envelopeWithXmin:xmin ymin:ymin xmax:xmax ymax:ymax spatialReference:self.mapView5.spatialReference];
            [extent expandByFactor:1.5];
   [self.mapView5 zoomToEnvelope:extent animated:YES];
        }
 }
    
    //since we've added graphics, make sure to redraw
    [self.graphicsLayer dataChanged];
    
}
0 Kudos
NimeshJarecha
Esri Regular Contributor
If you are using this (http://tasks.arcgis.com/ArcGIS/rest/services/WorldLocator/GeocodeServer) locator service to find addresses then you don't need to project the result geometries. Just use AGSLocator's locationsForAddress:returnFields:outSpatialReference: method and set the desired outSpatialReference.

Hope this helps!

Regards,
Nimesh
0 Kudos
LucaAlferi1
New Contributor
If you are using this (http://tasks.arcgis.com/ArcGIS/rest/services/WorldLocator/GeocodeServer) locator service to find addresses then you don't need to project the result geometries. Just use AGSLocator's locationsForAddress:returnFields:outSpatialReference: method and set the desired outSpatialReference.

Hope this helps!

Regards,
Nimesh


Nimesh you are a great
It does work, but I have another question. I can not do the zoom on the address searched. Any suggestions?
0 Kudos
LucaAlferi1
New Contributor
Nimesh you are a great
It does work, but I have another question. I can not do the zoom on the address searched. Any suggestions?


resolved
[self.mapView zoomToScale: 2000 withCenterPoint: pt animated: YES];
0 Kudos
NimeshJarecha
Esri Regular Contributor
Great! Good to know that everything works for you now! 🙂

Regards,
Nimesh
0 Kudos