.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface SecondViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate,NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
IBOutlet UIView *mapBottomView;
IBOutlet UITextView *locationTextView;
CLLocationManager *locationManager;
MKMapView *mapShowView;
}
- (IBAction)showMapView:(id)sender;
- (IBAction)reverseGeocoder:(id)sender;
@end
==================================================================================
.m
//
// SecondViewController.m
// Map_Location
//
// Created by yhy on 13-1-30.
// Copyright (c) 2013年 yhy. All rights reserved.
//
#import "SecondViewController.h"
#import "MapAnnotation.h"
#define GoogleMapBaseApi @"https://maps.googleapis.com/maps/api"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = 5.0f;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
mapShowView = [[MKMapView alloc] initWithFrame:mapBottomView.bounds];
//显示用户位置
mapShowView.showsUserLocation = YES;
// mapShowView.mapType = MKMapTypeHybrid;
//是否可滑动
mapShowView.scrollEnabled = YES;
//是否可放大缩小
mapShowView.zoomEnabled = YES;
mapShowView.delegate = self;
[mapBottomView addSubview:mapShowView];
//蓝鸥坐标
// latitude = 40.03424
// longitude = 116.317856
// CLLocationDegrees latitude = [@"40.03424" doubleValue];
// CLLocationDegrees longitude = [@"116.317856" doubleValue];
// CLLocation * showLocation = [[CLLocation alloc] initWithLatitude:latitude
// longitude:longitude];
// //根据经纬度范围显示
// mapShowView.region = MKCoordinateRegionMake(showLocation.coordinate, MKCoordinateSpanMake(0.0045f, 0.0045f));
// //根据距离范围显示
mapShowView.region = MKCoordinateRegionMakeWithDistance(showLocation.coordinate, 500, 500);
//
// mapShowView.showsUserLocation = YES;
// mapShowView.mapType = MKMapTypeHybrid;
// mapShowView.scrollEnabled = YES;
// mapShowView.zoomEnabled = YES;
// mapShowView.delegate = self;
// [mapShowView release];
}
- (IBAction)showMapView:(id)sender
{
[locationManager startUpdatingLocation];
}
- (IBAction)reverseGeocoder:(id)sender
{
[self getReverseGeocoderInfo];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * getTouch = [touches anyObject];
//屏幕上当前点击位置的坐标
CGPoint touchPoint = [getTouch locationInView:mapShowView];
//将屏幕上的点转换成地图上的点
CLLocationCoordinate2D location = [mapShowView convertPoint:touchPoint
toCoordinateFromView:mapShowView];
//
locationTextView.text = [NSString stringWithFormat:@"latitude = %f\nlongitude = %f",location.latitude,location.longitude];
}
//定位成功后执行这个代理
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
//美国苹果总部坐标
// latitude = 37.785834
// longitude = -122.406417
//蓝鸥坐标
// latitude = 40.03424
// longitude = 116.317856
//上地地铁坐标
// latitude = 40.03316
// longitude = 116.319738
//上地华联坐标
// latitude = 40.02874
// longitude = 116.312919
if (locations.count)
{
CLLocation * location = (CLLocation *)[locations objectAtIndex:0];
NSLog(@"latitude = %f",location.coordinate.latitude);
NSLog(@"longitude = %f",location.coordinate.longitude);
//根据经纬度范围显示
mapShowView.region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.0045f, 0.0045f));
//根据距离范围显示
mapShowView.region = MKCoordinateRegionMakeWithDistance(location.coordinate, 1000, 1000);
}
[locationManager stopUpdatingLocation];
}
//获取经纬度失败时候调用的代理方法
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"error = %@",error);
}
#pragma mark -------------------------
#pragma mark 范围改变
//移动、缩放地图
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
NSLog(@"mapView region Will ChangeAnimated");
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"mapView region Did ChangeAnimated");
}
#pragma mark -------------------------
#pragma mark 地图加载
//移动、缩放地图重新开始加载地图
//mapView将要开始加载地图时候调用的代理
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
NSLog(@"mapView Will Start Loading Map");
}
//mapView加载完地图时调用的方法
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"mapView Did Finish Loading Map");
//自定义注释(起始点)
MapAnnotation * lanouAnnotation = [[MapAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake([@"40.03424" doubleValue], [@"116.317856" doubleValue])];
lanouAnnotation.title = @"蓝鸥科技";
lanouAnnotation.subtitle = @"北京市海淀区上地信息路上地佳园";
//自定义注释(结束点)
MapAnnotation * shangdiAnnotation = [[MapAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake([@"40.03316" doubleValue], [@"116.319738" doubleValue])];
shangdiAnnotation.title = @"上地地铁站";
shangdiAnnotation.subtitle = @"北京市海淀区上地信息路";
//单个添加
[mapView addAnnotation:lanouAnnotation];
[mapView addAnnotation:shangdiAnnotation];
// //一起添加
// NSArray * annotations = [NSArray arrayWithObjects:lanouAnnotation,shangdiAnnotation, nil];
// [mapView addAnnotations:annotations];
[lanouAnnotation release];
[shangdiAnnotation release];
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
}
#pragma mark -------------------------
#pragma mark 注解视图
//- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
//{
// for (MKPinAnnotationView * mkaView in views){
// MapAnnotation * annotation = (MapAnnotation *)mkaView.annotation;
//
// if ([annotation.title isEqualToString:@"蓝鸥科技"]) {
// mkaView.pinColor = MKPinAnnotationColorGreen;
// }
//
// if ([annotation.title isEqualToString:@"上地地铁站"]) {
// mkaView.pinColor = MKPinAnnotationColorRed;
//
// UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// [rightButton addTarget:self
// action:@selector(showDetailInMapClient)
// forControlEvents:UIControlEventTouchUpInside];
//
// mkaView.rightCalloutAccessoryView = rightButton;
// }
//
// }
//}
//自定义注释view
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
if ([annotation isKindOfClass:[MapAnnotation class]]) {
static NSString * annotationIdentifier = @"annotation identifier";
MKPinAnnotationView * pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView) {
MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease];
annotationView.image = [UIImage imageNamed:@"pin.png"];
annotationView.canShowCallout = YES;
UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetailInMapClient)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}else{
pinView.annotation = annotation;
return pinView;
}
}
return nil;
}
- (void)showDetailInMapClient
{
//蓝鸥到北京体育大学的路线
UIDevice * device = [UIDevice currentDevice];
if ([device.systemVersion floatValue] < 6.0) {
NSString * myLoc = [NSString stringWithFormat:@"%f,%f",[@"40.03424" doubleValue],[@"116.317856" doubleValue]];
NSString * otherLoc =[NSString stringWithFormat:@"%f,%f",[@"39.987873" doubleValue],[@"116.305726" doubleValue]];
NSString * url = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@&daddr=%@&dirflg=t",[myLoc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[otherLoc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}else{
CLLocationCoordinate2D from = CLLocationCoordinate2DMake([@"40.03424" doubleValue],[@"116.317856" doubleValue]);
MKPlacemark * fromMark = [[[MKPlacemark alloc] initWithCoordinate:from
addressDictionary:nil] autorelease];
MKMapItem * fromLocation = [[MKMapItem alloc] initWithPlacemark:fromMark];
fromLocation.name = @"蓝鸥科技";
fromLocation.phoneNumber = @"123456789";
fromLocation.url = [NSURL URLWithString:@"www.lanou3g.com"];
CLLocationCoordinate2D to = CLLocationCoordinate2DMake([@"39.987873" doubleValue],[@"116.305726" doubleValue]);
MKPlacemark * toMark = [[[MKPlacemark alloc] initWithCoordinate:to
addressDictionary:nil] autorelease];
MKMapItem * toLocation = [[MKMapItem alloc] initWithPlacemark:toMark];
toLocation.name = @"北京体育大学";
NSArray * values = [NSArray arrayWithObjects:
MKLaunchOptionsDirectionsModeDriving,
[NSNumber numberWithBool:YES],
[NSNumber numberWithInt:2],
nil];
NSArray * keys = [NSArray arrayWithObjects:
MKLaunchOptionsDirectionsModeKey,
MKLaunchOptionsShowsTrafficKey,
MKLaunchOptionsMapTypeKey,nil];
[MKMapItem openMapsWithItems:[NSArray arrayWithObjects:fromLocation, toLocation, nil]
launchOptions:[NSDictionary dictionaryWithObjects:values
forKeys:keys]];
[fromLocation release];
[toLocation release];
}
}
#pragma mark -------------------------
#pragma mark 逆向地理编码
- (void)getReverseGeocoderInfo
{
NSString * baseUrl = [NSString stringWithFormat:@"%@/geocode/json?latlng=%@,%@&sensor=true&language=zh-CN",GoogleMapBaseApi,@"39.987873",@"116.305726"];
NSLog(@"reverse = %@",baseUrl);
// https://maps.googleapis.com/maps/api/geocode/json?latlng=40.03424,116.317856&sensor=true&language=zh-CN
}
#pragma mark -------------------------
#pragma mark 定位用户location
//获取用户当前地理位置后的代理
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"mapView did Update User Location");
mapView.userLocation.title = @"蓝鸥科技";
mapView.userLocation.subtitle = [NSString stringWithFormat:@"lat = %f,lon = %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude];
}
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[mapBottomView release];
[mapShowView release];
[locationTextView release];
[super dealloc];
}
- (void)viewDidUnload {
[mapBottomView release];
mapBottomView = nil;
[locationTextView release];
locationTextView = nil;
[super viewDidUnload];
}