【注意】由于OpenStreetMap此类教程在国内不太多,本文是本人花费多日通过一些国外网站论坛总结所得,如要转载,请保留文章出处,尊重一下作者,谢谢。
开端
本文皆在指引大家在自己的项目里嵌入OpenStreetMap,并可以在地图上添加覆盖物、折线等常用地图功能。
http://wiki.openstreetmap.org/wiki/Apple_iOS
ios版openstreetmap和android版类似,也是没有封装好的地图库,从以下截图可以看出,实现起来大体分为3种方式:
1、 route-me 是一个开源的ios地图应用,它可以作为地图库被其它应用导入xcodeproj引用,从而使用mapview,它封装了OpenStreetMap,命名为:RMMapview。
2、 OSM in Apple MapKit 这种方式只需要导入2个OSM的类,然后就可以使用OpenStreetMap,不过需要使用apple原生的UIMapView,ios6之前,apple的自带地图组件是google Map,现在地图组件用的是高德的Amap;这种方式的原理就是在原生地图初始化时,添加一个OSM的图层,即拿openstreetmap覆盖掉googlemap/Amap。
3、 其它的几种途径都是第三方对OSM的封装,有自己的API和SDK,以下是相关的网站链接:
http://www.mapbox.com/tour/#section-ios-sdk
http://developer.mapquest.com/web/products/featured/apple-ios-maps-api
http://cloudmade.com/
http://wiki.openstreetmap.org/wiki/OSM_in_MapKit
官网链接下载这四个文件,原则上是,有了他们,就可以使用openStreetMap了
xib上添加一个MapView组件(这个MapView是apple自带的地图组件,ios6之前使用的是google map,ios6之后用的是高德地图)
选中项目-Tragets-Build Phases,添加:MapKit.framework
ViewController.h
ViewController.h #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import "TileOverlay.h" @interface ViewController : UIViewController<MKMapViewDelegate> @property (retain, nonatomic) IBOutlet MKMapView *mMapView; @property(retain,nonatomic) TileOverlay *mTileOverlay; @endViewController.m
#import "ViewController.h" #import "TileOverlayView.h" @interface ViewController () @end @implementation ViewController @synthesize mTileOverlay; - (void)viewDidLoad { [super viewDidLoad]; self.mMapView.delegate =self; mTileOverlay = [[TileOverlay alloc] initOverlay]; [self.mMapView addOverlay:mTileOverlay];效果图:
//画线 CLLocationCoordinate2D coords[5]; coords[0] = CLLocationCoordinate2DMake(39.972465, 116.395645); coords[1] = CLLocationCoordinate2DMake(39.949459, 116.46176); coords[2] = CLLocationCoordinate2DMake(39.857356, 116.449687); coords[3] = CLLocationCoordinate2DMake(39.872863, 116.362875); coords[4] = CLLocationCoordinate2DMake(39.911836, 116.333554); MKPolyline *lines = [MKPolyline polylineWithCoordinates:coords count:5]; [self.mMapView addOverlay:lines]; MKCoordinateSpan span ={0.03,0.04};//MKCoordinateSpanMake(coords[4].latitude, coords[4].longitude); MKCoordinateRegion regon = MKCoordinateRegionMake(coords[4], span); [self.mMapView setRegion:regon animated:YES]; [self.mMapView setCenterCoordinate:coords[4] animated:YES]; //marker MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; [annotation setCoordinate:coords[0]]; annotation.title = @"I`m from chinese"; annotation.subtitle = @"I love my country"; [self.mMapView addAnnotation:annotation];
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{ if ([overlay isKindOfClass:[MKPolyline class]]) { MKPolylineView *line = [[MKPolylineView alloc] initWithOverlay:overlay]; line.strokeColor = [UIColor colorWithRed:9.0/255.0 green:94.0/255.0 blue:201.0/255.0 alpha:1.0]; line.lineWidth = 3.5f; return line; }else{ TileOverlayView *view = [[TileOverlayView alloc] initWithOverlay:overlay]; view.tileAlpha = 1.0; return view; } } -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ static NSString *annotationIdentifier = @"annotationidentifier"; MKPinAnnotationView *annotationview = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; if (annotationview == nil) { annotationview = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; annotationview.pinColor = MKPinAnnotationColorGreen; annotationview.canShowCallout = YES; annotationview.draggable = YES; } return annotationview; }