注:本篇文章翻译自:https://developers.arcgis.com/ios/objective-c/guide/iphonesdk-mapnavigation.htm;
地图视图包含了地图范围被定义和改变的选项。值得注意的是,底图(加载到地图中的第一层图层)定义了下列地图属性:
初始化范围
全部范围
空间参考系
其中,初始范围可以被改变,而空间参考不可以改变。
本篇文章主要讨论针对开发者和最终用户的地图范围解决方案。
设置地图范围
可以使用zoomToEnvelope:animated:的方法,通过传递一个表示你想要缩放到区域的envelope几何体来设置地图范围。
<span style="font-size:14px;">-(void)viewDidLoad{ [super viewDidLoad]; //add a layer to the map AGSTiledMapServiceLayer*tiledLayer=[AGSTiledMapServiceLayer tiledMapServiceLayerWithURL:[NSURLURLWithString:@""]];[self.mapViewaddMapLayer:tiledLayerwithName:@"Tiled Layer"]; //zoom to an area AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-124.83145667 ymin:30.49849464 xmax:-113.91375495 ymax:44.69150688 spatialReference:mapView.spatialReference]; [self.mapView zoomToEnvelope:envelope animated:NO];}</span> 获取地图范围
在地图被加载前你可能不知道它的范围,但是你可以利用地图视图的visibleArea属性来获取地图的范围。
<span style="font-size:14px;">AGSPolygon* mapExtent = self.mapView.visibleArea;</span> 其中visibleArea属性返回一个四边形,四个包含坐标的角为地图的四个角,以左上角为起点,以顺时针方向围绕而成。
如果地图没有做任何旋转,,那么由visibleArea四边形所表示的的面积和四边形的面积是一致的。那么你就可以用visibleArea四边形或是它的envelope作为地图的范围。
然而,不过如果地图发生转动后,那么visibleArea四边形和四边形的外包是不同的。外包(envelope)的面积比多边形的面积稍大,使得它完全包含多边形。在这种情况下,使用visibleArea多边形来获取地图的范围。
追踪平移和缩放
地图视图为地图范围的变化提供了两个通知(notifications),AGSMapViewDidEbdPanningNotification和AGSMapViewDidEndZoomingNotification。当地图被平移或是缩放后,这些通知各自被通知。下面的这些代码就是当用户平移或是缩放地图时如何监听这些通知的例子。例子展示了从地图视图的envelope的属性获得的一个新的地图范围提醒。
<span style="font-size:14px;">- (void)mapViewDidLoad:(AGSMapView *)mapView { // register for pan notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToEnvChange:)name:AGSMapViewDidEndPanningNotification object:nil]; // register for zoom notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToEnvChange:)name:AGSMapViewDidEndZoomingNotification object:nil]; …}// The method that should be called when the notification arises- (void)respondToEnvChange: (NSNotification*) notification { //create the string containing the new map extent NSString* NSString* theString = [[NSString alloc] initWithFormat:@"xmin = %f,\nymin =%f,\nxmax = %f,\nymax = %f", mapView.envelope.xmin,mapView.envelope.ymin, mapView.envelope.xmax,mapView.envelope.ymax]; //display the new map extent in a simple alert UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"Finished Panning"message:theString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show];}</span> 改变地图范围
正如前面所述,你可以利用zoomToEnvelop: animated 方法来设定地图范围。你也可以利用centerAtPoint:animated的方法将地图范围设置为以给定的一个点为中心。
<span style="font-size:14px;">AGSPoint *newPoint = [AGSPoint pointWithX:-93.032201 y:49.636213 spatialReference:self.mapView.spatialReference];[self.mapView centerAtPoint:newPoint animated:NO];</span>
用户手势
用户可以在程序运行时执行多种手势操作来改变地图的范围。所有的这些操作都会触发MapDidEndPanning和MapDidEndZooming的通知。
捏住往里缩小地图AGSMapViewDidEndZoomingNotification
捏住往外放大地图AGSMapViewDidEndZoomingNotification
双击放大地图AGSMapViewDidEndZoomingNotification
两只指头单击缩小地图AGSMapViewDidEndZoomingNotification
滑动(任何方向)朝滑动的方向移动地图AGSMapViewDidPanningNotification
两个指头扭转旋转地图
注:启用AGSMapView中的allowRotationByPinching属性才可以启用两个指头一起扭转的手势。 启用Wrap around
世界是圆的,它并不会在日期线终止。然而,许多关于地球的扁平的描述仅是将其延伸至东、西经180度。这使得很难看到跨越日期线的区域或是穿越太平洋的路线。
自己战胜自己是最可贵的胜利。