跟视图控制器:
.h为:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface RootViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>
@end
#import "RootViewController.h"
#import "ZGD_MyAnnotation.h"
#import "ZGD_MyViewController.h"
@interface RootViewController ()
@property (strong, nonatomic) MKMapView *mapView;
@property (retain, nonatomic) MKPinAnnotationView *annotationView;
@property (nonatomic) CLLocationCoordinate2D locationCoordinate2D;//当期那位置经纬度
@property (nonatomic) CLLocationCoordinate2D companyCooedinate2D;//公司位置经纬度
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.companyCooedinate2D = CLLocationCoordinate2DMake(42.6879, 116.19766);
self.locationCoordinate2D = CLLocationCoordinate2DMake(41.670, 116.2367);
//[dubai]创建地图视图--
self.mapView = [[MKMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:self.mapView];
self.mapView.delegate = self;
//[dubai]显示我的位置的按钮
UIButton *myLocationBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
myLocationBtn.frame = CGRectMake(30, 60, 80, 40);
[myLocationBtn setTitle:@"我的位置" forState:(UIControlStateNormal)];
[myLocationBtn addTarget:self action:@selector(clickMyLocationBtnACtion) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:myLocationBtn];
//[dubai]公司的位置
UIButton *companyBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
companyBtn.frame = CGRectMake(130, 60, 80, 40);
[companyBtn setTitle:@"公司的地址" forState:(UIControlStateNormal)];
[companyBtn addTarget:self action:@selector(clickCompanybtnAction) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:companyBtn];
//[dubai]导航路线规划
UIButton *lineBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
lineBtn.frame = CGRectMake(230, 60, 80, 40);
[lineBtn setTitle:@"路线" forState:(UIControlStateNormal)];
[lineBtn addTarget:self action:@selector(clickLineBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:lineBtn];
}
- (void)clickMyLocationBtnACtion
{
self.mapView.showsUserLocation = YES;
// MKCoordinateRegion region;
// region.span.latitudeDelta = 0.001;
// region.span.longitudeDelta = 0.001;
// region.center = self.companyCooedinate2D;
//设置显示位置(动画)
// [self.mapView setRegion:region animated:YES];
}
- (void)clickCompanybtnAction
{
ZGD_MyAnnotation *an = [[ZGD_MyAnnotation alloc] init];
an.coordinate = self.companyCooedinate2D;
an.annotationTitle = @"西湖美景";
an.annmtationSubtitle = @"欢迎光临";
[self.mapView addAnnotation:an];//做白哦添加到地图上
//设置显示范围
MKCoordinateRegion region;
region.span.latitudeDelta = 0.001;
region.span.longitudeDelta = 0.001;
region.center = self.companyCooedinate2D;
//设置显示位置(动画)
[self.mapView setRegion:region animated:YES];
[self.mapView regionThatFits:region];
self.mapView.showsUserLocation = NO;
}
- (void)clickLineBtnAction
{
CLLocationCoordinate2D fromeCoordinate = self.locationCoordinate2D;//起始位置为当前的初始位置
CLLocationCoordinate2D toCoordinate = self.companyCooedinate2D;
MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromeCoordinate addressDictionary:nil];
MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toCoordinate addressDictionary:nil];
MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
MKMapItem *toItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = fromItem;
request.destination = toItem;
request.requestsAlternateRoutes = YES;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:
^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
if (!self.locationCoordinate2D.longitude) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先给起始位置,也就是当前设备位置(点击我的位置)" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
}
}
else {
MKRoute *route = response.routes[0];
[self.mapView addOverlay:route.polyline];
}
}];
}
#pragma mark -
#pragma mark - MKMapViewDelegate
#pragma mark - 当前位置的代理
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D location = [userLocation coordinate];
NSLog(@"%f,%f",location.latitude,location.longitude);
self.locationCoordinate2D = location;
//放大地图到设备所在位置,并设置显示的区域的大小,单位是米。
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 200, 200);
[self.mapView setRegion:region animated:YES];
}
#pragma mark - 大头针的代理方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// 如果是当前位置,无需更改
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
if ([annotation isKindOfClass:[ZGD_MyAnnotation class]])
{
NSString *annotationViewId=@"meipinAnnotationView";
_annotationView = (MKPinAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewId];
if (_annotationView==nil){
// MKAnnotationView是使用自定义的图片.MKPinAnnotationView是定义系统默认的大头针
_annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewId];
_annotationView.canShowCallout = YES;
UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hello.png"]];
leftView.frame = CGRectMake(5, 5, 104, 30);
_annotationView.leftCalloutAccessoryView = leftView;
_annotationView.animatesDrop = YES;
// 设置右按钮
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
_annotationView.rightCalloutAccessoryView = rightButton;
}
}
return _annotationView;
}
// user tapped the disclosure button in the callout
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
// 点击大头针触发的操作
id <MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[ZGD_MyAnnotation class]])
{
ZGD_MyViewController *nextVC = [[ZGD_MyViewController alloc] init];
[self.navigationController pushViewController:nextVC animated:YES];
}
}
#pragma mark - 导航的代理方法
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView
rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.lineWidth = 5.0; // 线宽
renderer.strokeColor = [UIColor purpleColor];
return renderer;
}
- (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers
{
}
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface ZGD_MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *annotationTitle;
@property (nonatomic, copy) NSString *annmtationSubtitle;
- (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate;
@end
#import "ZGD_MyAnnotation.h"
@implementation ZGD_MyAnnotation
- (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate
{
if (self = [super init]) {
_coordinate = coordinate;
}
return self;
}
-(NSString *)title
{
return _annotationTitle;
}
-(NSString *)subtitle
{
return _annmtationSubtitle;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"ancun";
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.ancun.com"]]];
[self.view addSubview:webView];
}