ArcGIS iphone SDK开发教程(二)第一个iphone程序

来源:互联网 发布:免费手机定位追踪软件 编辑:程序博客网 时间:2024/04/30 08:27

http://www.giser.net/?p=12

开发环境搭建好之后,我们就开始我们的第一个iphone的ArcGIS应用程序开发。
1 打开xcode,创建一个工程,在创建工程的对话框的左边选择用户模板,然后选择View-based ArcGIS Mapping Application,然后点击choose。
2 选择模板后,输入工程名,我们取名为MyFirstMapApp,点击保存即可。
3 工程创建好之后,使用build工具build一下,确保工程能正确编译通过,在模板中默认加入了ArcGIS iPhone SDK(AGSiPhone.a)的库以及ArcGIS iPhone SDK依赖的库,在xcode的环境中,如果引用的库不存在,则显示为红色,这时候需要手工选择库的位置.
4 在工程能够编译通过之后,我们开始把ArcGIS地图加入到我们的视图中。
4.1 打开MyFirstAppViewController.h文件,输入下面黑体中标示的代码,其他代码为默认就有的代码

  1. #import
  2. #import "AGSiPhone.h"
  3. #define kTiledMapServiceURL
  4. @"http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"
  5. @interface MyFirstMapAppViewController : UIViewController {
  6. AGSMapView *_mapView;
  7. }
  8. @property (nonatomic, retain) IBOutlet AGSMapView *mapView;
  9. @end

复制代码

上面的代码主要的定义地图服务的url,并在视图控制器中定义地图视图MapView, 使用IBOutlet定义,这表示对象在Interface Builder中创建的。一旦将对象链接到一个出口,就可以像访问任何自己创建的对象一样访问它。
4.2 打开MyFirstAppViewController.m文件,输入下面黑体标示的代码,其他代码为默认就有的代码

  1. #import "MyFirstMapAppViewController.h"
  2. @implementation MyFirstMapAppViewController
  3. @synthesize mapView = _mapView;
  4. // Implement viewDidLoad to do additional setup after loading
  5. // view, typically from a nib.
  6. - (void)viewDidLoad {
  7.     [super viewDidLoad];
  8. AGSTiledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL
  9. URLWithString:kTiledMapServiceURL]];
  10. [self.mapView addMapLayer:tiledLayer withName"Tiled Layer"];
  11. [tiledLayer release];
  12. }
  13. - (void)dealloc {
  14.     self.mapView = nil;
  15.     [super dealloc];
  16. }
  17. @end

复制代码

在上面的代码中@synthesize定义属性mapView的getters 和setters,在viewDidLoad的函数中创建AGSTiledMapServiceLayer并加载到mapview中。最后在 dealloc 函数中释放mapView,因为在iphone的开发中,内存是非常稀缺的资源,因此要非常精确的做好内存管理工作。在objective c中使用引用计数的方式进行内存管理,所以当将tiledLayer加到mapview中之后,一定要释放tiledLayer的引用计数,即掉用 [tiledLayer release]。
5 代码写好之后,我们要创建map的可视化组件,将map加载到iphone的界面中,我们可以使用interface builder来进行。
5.1 在xcode的工程的resource目录下找到MyFirstMapAppViewController.xib,并双击,可以看到如下界面2-1:
2-1.png
在图上我们可以看到模板已经构建好了UIView对象,下面我们就在UIview下面创建一个subview来显示我们的地图。
5.2在tool->library下面打开library 窗口,这个窗口里面包含所有的iphone可视化组件,我们在其中选择UIView(可以通过条件过滤窗口进行过滤),如图2-2,图2-3:
2-2.png
2-3.png
5.3 选择UIView并把其拖动到打开的MyFirstMapAppViewController.xib的窗口中的View的下面,作为一个子视图如图2-4。
2-4.png
5.4 我们需要给这个子视图一个类型,即我们前面代码中创建的AGSMapView。选择Tools -> Identity Inspector打开Identity Inspector,在Class栏输入AGSMapView,如图2-5。
2-5.png
在MyFirstMapAppViewController.xib中显示如图2-6:
2-6.png
5.5 最后一步就是把我们在代码中创建的mapview和界面绑定到一起。在MyFirstMapAppViewController.xib的窗口中选择 File's Owner并打开Connections Inspector(Tools -> Connections Inspector),在Inspector中应该已经存在mapview,这是我们使用outlet定义的,如图2-7:
2-7.png
在Connections Inspector 窗口中点击右边的小圆圈,并把拖动到MyFirstMapAppViewController.xib窗口的mapview上 如图2-8,这样就把界面元素和代码中mapview进行了绑定。
2-8.png
绑定后的结果如图2-9。
2-9.png
5.6 保存工程,并返回到xcode,然后编译运行。如图2-10:
2-10.png
5.7 让你iphone支持屏幕旋转的话,将MyFirstAppViewController.m文件中下面的代码反注释掉既可。

  1. -(BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation {
  2.     // Return YES for supported orientations
  3.     return (interfaceOrientation != UIInterfaceOrientationPortrait);
  4. }

复制代码

横屏的效果如图2-11:
2-11.png
上面既是第一ArcGIS iphone 地图程序,我使用test命名了工程名,编译并部署到了我的ipod touch中,放2张截图。
2-12.PNG
2-13.PNG
参考资料:http://help.arcgis.com/en/arcgis ... 00pw00000047000000/

原创粉丝点击