MKMapView显示位置的地图

先声明,这个的东西是根据http://wangjun.easymorse.com/?p=996做的

但是在Xcode4里面做的时候有几个小问题,我这里在我自己做的时候解决了一下,所以也可以参考,毕竟也不是很麻烦的东西)

在这里学习一下iOS中地图的使用,下面是一个最简单的例子,用来展示怎么利用MKMapView实现地图的加载.


下面是实现过程,首先创建一个项目iphone.map

在项目中添加map的Frameworks:(4.2和3.25里面添加Frameworks方法不一样了,我之前的博客有,自己找一下吧,网上也看见有朋友问过个这事)


添加代码:

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
@interface iphone_MapViewController : UIViewController { 
   IBOutlet MKMapView *mapView; 

@property(nonatomic, retain) IBOutlet MKMapView *mapView; 
@end

然后利用IB增加视图并且和代码相关联:(在这里4.2有了一部分区别,但是要说清楚估计要贴不少图,自己找一下吧,就在那个文件下,然后一连就行了)

image 

运行即可。

现在你就可以显示个地图了,啦啦啦啦啦啦啦啦啦啦啦

然后继续做东西

获取自己的经纬度,要使用CLLocationManager,CLLocationManager在CoreLocation.framework中,所以先在工程中添加CoreLocation.framework (这个就同上了)

然后添加相关代码:

添加CLLocationManagerDelegate协议

    @interface iphone_MapViewController : UIViewController
    <CLLocationManagerDelegate>{
        IBOutlet MKMapView *mapView;
    }

实现代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        mapView.showsUserLocation=YES;

        CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
        locationManager.delegate=self;//设置代理
        locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
        locationManager.distanceFilter=1000.0f;//设置距离筛选器
        [locationManager startUpdatingLocation];//启动位置管理器
        MKCoordinateSpan theSpan;
        //地图的范围 越小越精确
        theSpan.latitudeDelta=0.05;
        theSpan.longitudeDelta=0.05;
        MKCoordinateRegion theRegion;
        theRegion.center=[[locationManager location] coordinate];
        theRegion.span=theSpan;
        [mapView setRegion:theRegion];
        [locationManager release];
    }
这时候之前那篇博客就完事了。。。。

但是好多兄弟发现,不是蓝色就是黑色,因为经度和纬度我们没设置。。貌似模拟器是不能获取经纬度的

,这个放到真机是没问题的。

不过Xcode4里面模拟器可以设置位置,比如,美国,香港什么的,我在另外一篇博客会说的,自己找找吧。

好了,打完了,收工。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值