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
// 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
相关文章推荐
- iOS[问题解决] Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed.
- NSArray ,NSSet, NSDictionary
- iOS在地图上画图和导航
- 深度解析iOS应用程序的生命周期
- ios播放本地声音文件
- 斯坦福大学公开课:iOS8开发 第一课:课务,iOS8概述学习笔记
- iostat 等运维命令用法
- iOS发邮件
- ios8模拟器切换输入中文
- 简单动画效果
- iOS写短信发送
- 网易新闻iOS版使用的18个开源组件
- iOS拨打电话
- nagios监控-多用户管理
- nagios 监控网页
- iOS 9适配系列教程:后台定位
- iOS开发-清理缓存功能的实现
- ios中陀螺仪CoreMotion的用法
- iOS深拷贝和浅拷贝
- iOS 多线程GCD