简单的显示Google地图
添加MapKit.Framework框架
HomeViewController.h code:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface HomeViewController : UIViewController{
MKMapView *map;
}
@end
HomeViewController.m code:
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
[self.view addSubview:map];
}
- (void)dealloc{
[map release];
[super dealloc];
}
@end

锁定位置
添加CoreLocation.Framework
HomeViewController.h code:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface HomeViewController : UIViewController{
MKMapView *map;
}
@end
HomeViewController.m code:
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
//设置地图显示模式
//map.mapType = mkmaptype;//设置地图为卫星显示模式
//map.mapType = MKMapTypeStandard;//显示一个标准的地图,包括街道和公路。它是一个默认值。
map.mapType = MKMapTypeHybrid;//显示一个混合前两种情况的视图,也就是说在卫星视图上包含公路和街道的文字信息
//设置地图显示当前设备所在的地理位置
map.showsUserLocation = YES;//显示用户的位置
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(32.97386, 112.54938);
float zoomLevel = 0.002;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));//设置位置和范围
[map setRegion:[map regionThatFits:region] animated:YES];
[self.view addSubview:map];
}
- (void)dealloc{
[map release];
[super dealloc];
}
@end
地图上的路径
HomeViewController.m code:
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
//设置地图显示模式
map.mapType = MKMapTypeSatellite;//设置地图为卫星显示模式
//map.mapType = MKMapTypeStandard;//显示一个标准的地图,包括街道和公路。它是一个默认值。
//map.mapType = MKMapTypeHybrid;//显示一个混合前两种情况的视图,也就是说在卫星视图上包含公路和街道的文字信息
//设置地图显示当前设备所在的地理位置
map.showsUserLocation = YES;//显示用户的位置
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.992616, 116.389632);
float zoomLevel = 0.002;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));//设置位置和范围
[map setRegion:[map regionThatFits:region] animated:YES];
[self.view addSubview:map];
map.delegate = self;
[self createRoute];
}
//定义5个点
- (void)createRoute{
CLLocationCoordinate2D routeCoords[5];
routeCoords[0] = CLLocationCoordinate2DMake(39.849227, 116.4083);
routeCoords[1] = CLLocationCoordinate2DMake(39.949227, 116.395555);
routeCoords[2] = CLLocationCoordinate2DMake(39.949227, 116.393623);
routeCoords[3] = CLLocationCoordinate2DMake(39.949227, 116.401091);
routeCoords[4] = CLLocationCoordinate2DMake(39.949227, 116.400833);
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:routeCoords count:5];
[map addOverlay:routeLine];
[routeLine release];
}
//协议中的方法,
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
MKPolylineView *polyView = [[MKPolylineView alloc] initWithOverlay:overlay];
polyView.strokeColor = [UIColor redColor];
polyView.lineWidth = 5.0;
return [polyView autorelease];
}
- (void)dealloc{
[map release];
[super dealloc];
}
@end

添加标记“大头针”
添加一个NSObject类,命名为CustomAnnotation
CustomAnnotation.h code
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CustomAnnotation : NSObject
<MKAnnotation>{
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;//显示大头针的坐标
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@end
CustomAnnotation.m code
#import "CustomAnnotation.h"
@implementation CustomAnnotation
@synthesize coordinate, title, subtitle;
- (id)initWithCoordinate:(CLLocationCoordinate2D) coords{
if (self = [super init]) {
coordinate = coords;
}
return self;
}
- (void)dealloc{
[title release];
[subtitle release];
[super dealloc];
}
@end
修改HomeViewController.m代码
#import "HomeViewController.h"
#import "CustomAnnotation.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
//设置地图显示模式
map.mapType = MKMapTypeSatellite;//设置地图为卫星显示模式
//map.mapType = MKMapTypeStandard;//显示一个标准的地图,包括街道和公路。它是一个默认值。
//map.mapType = MKMapTypeHybrid;//显示一个混合前两种情况的视图,也就是说在卫星视图上包含公路和街道的文字信息
//设置地图显示当前设备所在的地理位置
map.showsUserLocation = YES;//显示用户的位置
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.992616, 116.389632);
float zoomLevel = 0.002;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));//设置位置和范围
[map setRegion:[map regionThatFits:region] animated:YES];
[self.view addSubview:map];
map.delegate = self;
//[self createRoute];
[self createAnnotationWithCoords:coords];
}
//定义5个点
- (void)createRoute{
CLLocationCoordinate2D routeCoords[5];
routeCoords[0] = CLLocationCoordinate2DMake(39.849227, 116.4083);
routeCoords[1] = CLLocationCoordinate2DMake(39.949227, 116.395555);
routeCoords[2] = CLLocationCoordinate2DMake(39.949227, 116.393623);
routeCoords[3] = CLLocationCoordinate2DMake(39.949227, 116.401091);
routeCoords[4] = CLLocationCoordinate2DMake(39.949227, 116.400833);
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:routeCoords count:5];
[map addOverlay:routeLine];
[routeLine release];
}
//协议中的方法,
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
MKPolylineView *polyView = [[MKPolylineView alloc] initWithOverlay:overlay];
polyView.strokeColor = [UIColor redColor];
polyView.lineWidth = 5.0;
return [polyView autorelease];
}
- (void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords{
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:coords];
annotation.title = @"标题";
annotation.subtitle = @"子标题";
[map addAnnotation:annotation];
[annotation release];
}
- (void)dealloc{
[map release];
[super dealloc];
}
@end