`
zjjzmw1
  • 浏览: 1356184 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

ios 自带的定位。

    博客分类:
  • iOS
阅读更多

//定位的按钮。

- (IBAction)dingWeiAction:(id)sender {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchFocusImageItems) object:nil];
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(64.2323,32.3455) addressDictionary:nil] autorelease]];
    toLocation.name = self.title;
    [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
                   launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
    [toLocation release];

}

 

就这一个方法就行了。当然要导入两个文件。#import "MapViewController.h"
#import <MapKit/MapKit.h>

 

其中MapViewController 是自己写的。

MapViewController.h :

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MapViewController : UIViewController

@property (assign ,nonatomic) CLLocationCoordinate2D to;

@property (retain,nonatomic) NSString *shopName;
@end

 

MapViewController.m

#import "MapViewController.h"
#import <MapKit/MapKit.h>
@interface MapViewController ()

@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc] initWithCoordinate:self.to addressDictionary:nil] autorelease]];
   
   
    toLocation.name = self.title;
    [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
                   launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil]
                                 
                                 
                                                             forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
    [toLocation release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

 

2
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics