博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2010年8月30日周一_Creating A Map_7.1

Posted on 2010-08-30 17:15  星尘的天空  阅读(273)  评论(0编辑  收藏  举报

/**********************************************/

//Creating A Map

//2010年8月30日

/*********************************************/

Resource Center » Maps

The primary way that you can display geographic(地理(学)的) information in your iPhone application is by using a Map. The iPhone Map control lets you mash up(混合) map layers from different sources.

将地理信息展示在你的iphone应用程序的主要方式是使用一个Map(地图控件)。 Iphone Map控件让你混合来自不同资源的地图图层。

How to Create a Map

The following steps assume you have setup your project to build with the ArcGIS iPhone Library and you have created ViewController.[ch] files and a .XIB resource file which will contain the Map control.

  1. Import the "ArcGIS.h" interface file in your view controller's class interface (.h) file:

如何创建一个Map

下面的步骤假设你已经使用ArcGIS Iphone 类库创佳了你工程,并且你已经创建了ViewController文件和一个XIB 资源文件,它包含这个Map控件。

在你的view controller's class头文件中引入"ArcGIS.h" 接口文件。

                                  #import "ArcGIS.h"

2. Add an AGSMapView instance variable(变量) to your view controller's class interface and add a declared property to access it:

添加一个AGSMapView 实例变量到你的view controller's class接口,添加一个声明的属性来访问它。

 @interface MyViewController : UIViewController

{

                AGSMapView *_mapView;

}

 @property (nonatomic, retain) IBOutlet AGSMapView *mapView;

 @end

The IBOutlet keyword will allow us to connect to an AGSMapView control in Interface Builder(接口生成器). Also, notice that the instance variable is prefixed(前缀的) with an '_', while the @property is not. This will be explained in the next step.

IBOutlet 关键字允许我们连接接口生成器中的AGSMapView控件。还要注意实例变量有一个前缀“_”,而@property 没有。 这将在下一阶段解释。

  1. In the view controller's implementation file (.m), use the @synthesize directive to synthesize(合成) getter and setter methods for the mapView property:

在view controller's 实现文件中,使用@synthesize(综合, 合成) 指导为mapView 属性合成getter and setter方法。

 #import "MyViewController.h"

 @implementation MyViewController

 @synthesize mapView = _mapView;

Notice that(请注意) the mapView property will be represented by the _mapView instance variable. This helps eliminate(排除,消除,根除;淘汰) certain programming errors and is considered a 'best practice'.(最优方法)

请注意:mapView属性将由实例变量_mapView代表。这帮助消除某些编程错误,并且他被认为是最有的方法。

4. Now create a service layer and add it to the map view. This is done in the view controller's viewDidLoad method:

现在,创建一个服务图层,并将它添加到地图试图。 这是在view controller 的viewDidLoad方法中完成的。

 - (void)viewDidLoad {

 [super viewDidLoad];

 AGSTiledMapServiceLayer *tiledLayer =

                                   [[AGSTiledMapServiceLayer alloc]

                                   initWithURL:[NSURL URLWithString:@ "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"]];

  [self.mapView addMapLayer:tiledLayer withName:@"Tiled Layer"];

                                  [tiledLayer release];

}

Make sure to release the instance of AGSTiledMapServiceLayer to avoid memory leaks(v.(使)漏;泄露 n.漏洞,裂缝;泄漏,漏出量).

确保释放AGSTiledMapServiceLayer的实例,以避免内存的泄露。

5. In the dealloc method, you need to set the mapView property to nil:

在dealloc 方法中,你需要设置mapView 属性为nil;

 - (void)dealloc

{

 self.mapView = nil;

 [super dealloc];

}

Now that we've created a map view and added a tiled map service layer, we need to create the actual AGSMapView control in Interface Builder.

请注意:我们已经创建了一个地图视图,并添加了一个Map服务图层,我们需要在Interface Builder中创建真实的AGSMapView控件

6. In Xcode, double click the resource file associated with your view controller in order to open it in Interface Builder. In the Interface Builder Library window, find the UIView component and drag and drop it on top of your view controller's existing view (in the main window). The new UIView should appear under and indented from the existing view. This will become the map view.

 在Xcode,双击和你的view controller关联的资源文件,打开interface builder。 在Interface Builder Library 窗口,找到UIView组件,拖放一个到你的view controller现有的视图的顶部。 新的UIView 就应该出现在下面,缩进已经存在的视图。 这就会变成地图视图。

7. Make sure the new view is selected in the main window and open the Identity(身份) Inspector(检查员;监察员). In the Class drop down type "AGSMapView" and hit enter. This changes the type of the view to "AGSMapView". The name of the view should have changed to "Map View" in the main window as well.

确保在主窗口中选中新的view,打开身份监察员。 在类中下拉AGSMapView,并单击回车键。 这就改变了视图的类型为AGSMapView。 在主窗口中view的名称应该已经改为“MapView”。

8. The last step is to hook our AGSMapView up with the mapView property we created in Xcode. In the main window, select the 'File's Owner' item and then open the Connections Inspector(检查员;监察员) window. Click the empty circle to the right of the 'mapView' item and drag it to the MapView item (our new AGSMapView) in the main window. This links the Interface Builder control to the view controller's mapView property and allows us to access it via the code.

最后的一步是将我们在Xcode中创建的MapView属性与我们的AGSMapView 挂钩。 在主窗口中,选择文件的所有者项,然后打开连接监察员的窗口 单击空圆圈到right of the 'mapView' item,并将它拖放到主窗口中的MapView项(我们的新AGSMapView)。它连接着Interface builder 控件到view controller 的MapView 属性,并允许我们通过代码访问。

9. Now save the resource file in Interface Builder and return to XCode. You can then build and run your application. You should see an ArcGIS iPhone Map control showing a map of the world.

现在:在interface builder 中保存资源文件,然后返回到Xcode。 然后你就可以编译和运行你的应用程序。你应该看到一个ArcGIS iPhone 地图控件显示一个世界地图

Spatial References

The first layer with a valid spatial reference defines the spatial reference for the map. Dynamic ArcGIS Server map and image services as well as feature layers (FeatureLayer) will be reprojected to the map's spatial reference, if necessary. Tiled map services layers will not be reprojected - the spatial reference of the layer and map must match for the layer to display in the map.

Because the spatial reference cannot be changed once it has been defined, the map view's spatialReference property is read-only.

空间参考:

   第一个拥有空间参考的图层为Map定义空间参考。Dynamic ArcGIS Server 地图和图像服务以及feature Layers 将会重新添加到地图的空间参考,如果需要,Tiled map服务不需要重新加载图层空间参考,并且地图必须和在地图上展示的图层想匹配。

因为空间参考一旦定以后就不能在改变,所以地图视图的空间参考属性是只读的。