Map zooms in on equator or not at all

2453
2
Jump to solution
04-25-2014 07:37 AM
SjorsProvoost
New Contributor II
I'm showing a small map in an iPad app and I'm able to display the user location on it as a blinking blue dot on a map of the world. I would like to zoom in on a specific location, e.g. the city of Utrecht in The Netherlands, at a zoom level of a few hundred meters.

Unfortunately when I call zoomToEnvelope:envelope it drops me in the middle of the ocean, presumably 0 latitude by 0 longitude. If on the other hand I call zoomWithFactor the app doesn't zoom in anywhere. I must be doing something wrong. I actually had it working before, so perhaps I broke something (I'll try diff-ing my way to fixing it).

The spatial reference of AGSMapView *map is as excepted when mapViewDidLoad is called. I'm not getting any error messages. I'm calling zoomToEnvelope on the main thread. What else should be careful about?

//  ViewController.m #import <ArcGIS/ArcGIS.h> @interface ViewController () <AGSMapViewLayerDelegate>  {     IBOutlet AGSMapView *map; } @end  @implementation ViewController  - (void)viewDidLoad {     [super viewDidLoad];     NSString *mapLocation = @"https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";         AGSTiledMapServiceLayer *basemapServiceLayer = [AGSTiledMapServiceLayer tiledMapServiceLayerWithURL:[NSURL URLWithString:mapLocation]];     [map addMapLayer:basemapServiceLayer withName:@"Tiled Layer"];     map.layerDelegate = self;     }  -(void)mapViewDidLoad:(AGSMapView *)mapView {     [map.locationDisplay startDataSource];       if(map.loaded) {         double x = 5.0;         double y = 52.5;         zoomDegrees = 0.005         AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:x - zoomDegrees ymin:y - zoomDegrees xmax:x + zoomDegrees ymax:y + zoomDegrees spatialReference:map.spatialReference];       // Zooms in on the equator:   [map zoomToEnvelope:envelope animated:YES];       // Does nothing at all:         // [map zoomWithFactor:4 atAnchorPoint:CGPointMake(50, 80) animated:YES];     } } @end
0 Kudos
1 Solution

Accepted Solutions
DiveshGoyal
Esri Regular Contributor
You're using the Esri World Topo basemap, so your map is in Web Mercator spatial reference.
But the coordinates you're using in ZoomToEnvelope are in decimal degrees (implying a WGS84 spatial reference). This mis match causes the map to zoom into the wrong place. You should really specify the coordinates in web mercator, or alternatively, you can continue use wgs84 for the coordinates, but then reproject the envelope in web mercator. This topic is also covered in this post -

As for zoomWithFactor , you're specifying a factor of 4, which will try to zoom OUT the map. You're probably already zoomed out all the way, and so this doesn't take any effect. To zoom in, you should specify a fractional value less than 1, for eg, 0.2. This should make the map zoom in.

PS, the map's location display has an autopan mode. If you set this to default, it will zoom the map into the location of the device.
You can change the zoomScale of the location display to specify what target scale to zooms into when it tries to center on the device location.

View solution in original post

0 Kudos
2 Replies
DiveshGoyal
Esri Regular Contributor
You're using the Esri World Topo basemap, so your map is in Web Mercator spatial reference.
But the coordinates you're using in ZoomToEnvelope are in decimal degrees (implying a WGS84 spatial reference). This mis match causes the map to zoom into the wrong place. You should really specify the coordinates in web mercator, or alternatively, you can continue use wgs84 for the coordinates, but then reproject the envelope in web mercator. This topic is also covered in this post -

As for zoomWithFactor , you're specifying a factor of 4, which will try to zoom OUT the map. You're probably already zoomed out all the way, and so this doesn't take any effect. To zoom in, you should specify a fractional value less than 1, for eg, 0.2. This should make the map zoom in.

PS, the map's location display has an autopan mode. If you set this to default, it will zoom the map into the location of the device.
You can change the zoomScale of the location display to specify what target scale to zooms into when it tries to center on the device location.
0 Kudos
SjorsProvoost
New Contributor II
Thanks!

I just had to set the spatial reference of the envelope to [AGSSpatialReference wgs84SpatialReference] (decimal coordinates) and the map automagically converts that to its own spatial reference and zooms in on the correct location.

AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:x - zoomDegrees ymin:y - zoomDegrees xmax:x + zoomDegrees ymax:y + zoomDegrees 
                                     spatialReference:[AGSSpatialReference wgs84SpatialReference]];

0 Kudos