Select to view content in your preferred language

zoomToEnvelope not working

3385
2
01-20-2011 02:14 PM
BrianKrzys
Occasional Contributor
Example code below displays two layers and attempts to zoom to an envelope defined by a geographic lat-long box, but the zoom doesn't work.  Suspect I am missing something simple with how the envelope is defined?

Thanks ahead of time for any help.

Brian.

  
  //
  // ----- OpenStreetMap base layer
  //
  AGSOpenStreetMapLayer *baseLayer = [[AGSOpenStreetMapLayer alloc] init];
  [self.mapView addMapLayer:baseLayer withName:@"OpenStreetMap"];
  [baseLayer release];
  //
  // ----- US highways from ESRI
  //
  NSURL* url = [NSURL URLWithString: @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"];  
  AGSDynamicMapServiceLayer* highwayLayer = [AGSDynamicMapServiceLayer dynamicMapServiceLayerWithURL: url];
  [self.mapView addMapLayer:highwayLayer withName:@"US Highways"];
  //
  // ----- zoom to approximately Colorado
  //
  AGSSpatialReference *sr = [AGSSpatialReference spatialReferenceWithWKID:4326];
 AGSEnvelope *env = [AGSEnvelope envelopeWithXmin:-110.0 
                                              ymin:35.0 
                                              xmax:-100.0 
                                              ymax:40.0 
                                  spatialReference:sr];
  [self.mapView zoomToEnvelope:env animated:YES];
0 Kudos
2 Replies
NimeshJarecha
Esri Regular Contributor
It is not working because your map (self.mapView.spatialReference) is in WebMercator ( base map - AGSOpenStreetMapLayer's spatial reference) and your envelope is in wgs1984 (wkid:4326). The envelope passed in the zoomToEnvelope must be in map's spatial reference.

Hope this helps!

Regards,
Nimesh
0 Kudos
BrianKrzys
Occasional Contributor
Thanks, that was it, somehow I thought it would convert the coordinates automatically to match the base map.  Working code below.

Cheers, Brian.

  AGSSpatialReference *sr = [AGSSpatialReference spatialReferenceWithWKID:102100];
 AGSEnvelope *env = [AGSEnvelope envelopeWithXmin:-12158332
                                              ymin:4392194
                                              xmax:-11341332
                                              ymax:5006778
                                  spatialReference:sr];
  [self.mapView zoomToEnvelope:env animated:YES];
0 Kudos