您的位置:首页 > 移动开发 > IOS开发

iOS在地图上画图和导航,同时设定比例

2015-06-24 12:01 344 查看
//

// ViewController.m

// navitomyhome

//

// Created by tianshangrenjian on 15/6/23.

// Copyright © 2015年 tianshangrenjian. All rights reserved.

//

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>

@property (weak,
nonatomic) IBOutlet
MKMapView *map;

@property (strong,
nonatomic) CLPlacemark *destPos;

@property (strong,nonatomic)
CLLocationManager *mgr;

@property (strong,nonatomic)
CLGeocoder *geocoder;

@end

@implementation ViewController

- (CLLocationManager *)mgr
{

if (_mgr==nil) {

_mgr=[[CLLocationManager
alloc]
init];
}

return _mgr;
}
- (CLPlacemark *)destPos
{

if (_destPos==nil) {

_destPos=[[CLPlacemark
alloc] init];
}

return
_destPos;
}
- (CLGeocoder *)geocoder
{

if (_geocoder==nil) {

_geocoder=[[CLGeocoder
alloc] init];
}

return
_geocoder;
}
- (void)locationManager:(nonnull
CLLocationManager *)manager didUpdateLocations:(nonnull
NSArray *)locations
{

}
- (void)drawLineWithPlaceMark:(MKPlacemark*)sourcePlace andDestPlaceMark:(MKPlacemark*)destPlaceMark
{

MKDirectionsRequest *mkRequest=[[MKDirectionsRequest
alloc]
init];

MKMapItem *sourceItem=[[MKMapItem
alloc] initWithPlacemark:sourcePlace];

MKMapItem *destItem=[[MKMapItem
alloc] initWithPlacemark:destPlaceMark];

mkRequest.destination=destItem;
mkRequest.source=sourceItem;

MKDirections *directions = [[MKDirections
alloc]initWithRequest:mkRequest];
[directions
calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *
__nullable response,
NSError * __nullable error) {

for (MKRoute *route
in response.routes) {
[self.map
addOverlay:route.polyline];
}
}];
}
- (nonnull
MKOverlayRenderer *)mapView:(nonnull
MKMapView *)mapView rendererForOverlay:(nonnull
id<MKOverlay>)overlay
{

MKPolylineRenderer *line1=[[MKPolylineRenderer
alloc]initWithOverlay:overlay];
line1.lineWidth=4;

line1.strokeColor=[UIColor
redColor];

// MKOverlayRenderer *lay=[[MKOverlayRenderer alloc] initWithOverlay:overlay];

return line1;
}
- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.map.delegate=self;

CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(34,
114);

MKCoordinateSpan span=MKCoordinateSpanMake(10,
10);

MKCoordinateRegion region=MKCoordinateRegionMake(coordinate, span);
[self.map
setRegion:region];

[self.geocoder
geocodeAddressString:@"武汉"
completionHandler:^(NSArray<CLPlacemark *> *
__nullable placemarks,
NSError * __nullable error) {

__block CLPlacemark *fromPlace=[placemarks
firstObject];
[self.geocoder
geocodeAddressString:@"北京"
completionHandler:^(NSArray<CLPlacemark *> *
__nullable placemarks,
NSError * __nullable error) {

MKPlacemark *mkfromplace=[[MKPlacemark
alloc] initWithPlacemark:fromPlace];
[self
drawLineWithPlaceMark:mkfromplace
andDestPlaceMark:[[MKPlacemark
alloc] initWithPlacemark:[placemarks
firstObject]]];
}];
}];

// [self.mgr startUpdatingLocation];

// [self.geocoder geocodeAddressString:@"玉溪市" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {

// if (error || placemarks.count==0) {

// return ;

// }

// self.destPos=[placemarks firstObject];

// }];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}
- (IBAction)goHome:(id)sender {
MKPlacemark *mkPlacemark=[[MKPlacemark alloc] initWithPlacemark:self.destPos];
MKMapItem *item1=[[MKMapItem alloc ] initWithPlacemark:mkPlacemark];

NSMutableDictionary *dict=[NSMutableDictionary dictionary];

dict[MKLaunchOptionsDirectionsModeKey]=MKLaunchOptionsDirectionsModeDriving;
dict[MKLaunchOptionsShowsTrafficKey]=@YES;

[MKMapItem openMapsWithItems:@[item1] launchOptions:dict];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: