先倒入两个框架
显示除地图
self.mapview=[[[MKMapView alloc]initWithFrame:self.view.bounds]autorelease];//和试图一样的尺寸
self.view addSubview:_mapview];
//显示指定位置
//用地理位置的名称获取经纬度
CLGeocoder *gen=[[[CLGeocoder alloc]init]autorelease];
geocodeAddressString:@"厦门海世界" completionHandler:^(NSArray *placemarks, NSError *error) {
if(error)
{
NSLog(@"查询失败");
}
else
{
for(CLPlacemark *placemark in
{
NSLog(@"%f,%f",placemark.location.coordinate.latitude,placemark.location.coordinate.longitude);
}
}
}];
//*******
//展示指定经纬度
CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(24.446294, 118.073174);
//显示范围以经纬度做单位
MKCoordinateSpan span=MKCoordinateSpanMake(0.08, 0.08);
[self.mapview setRegion:MKCoordinateRegionMake(coordinate,span) animated:YES];
[self performSelector:@selector(showAnnotation) withObject:self afterDelay:2];
}
-(void)showAnnotation
{ //添加标记
MKPointAnnotation *point=[[[MKPointAnnotation alloc]init]autorelease];
title= @"鼓浪屿";
subtitle=@"欢迎来到美丽的小岛";
point.coordinate=CLLocationCoordinate2DMake(24.446294, 118.073174);//标记经纬度
MKPointAnnotation *point1=[[[MKPointAnnotation alloc]init]autorelease];
title= @"厦门海世界";
subtitle=@"欢迎来到美丽的海地";
point1.coordinate=CLLocationCoordinate2DMake(24.490474,118.110220);
//自定义自己的标记图片,要满足MKAnnotation的协议方法 在mapView viewForAnnotation中海需要在次的写方法在一次的实现动效Celoatainntation时自定义的一个类,简单的在类里添加了 和系统anntation一样的属性 并添加了image属性。
Celoatainntation *ce=[[[Celoatainntation alloc]init]autorelease];
ce.title=@"a a a a ";
subtitle=@"ssss";
image=[UIImage imageNamed:@"return"];
ce.coordinate=CLLocationCoordinate2DMake(24.490474,118.110520);
[self.mapview addAnnotation:ce];
//每一个大头针比较都要嫁到mapview中,mapview时总的控制。
NSArray *points=[NSArray arrayWithObjects:point,point1,ce, nil];
self.mapview addAnnotations:points];
[self.mapview setSelectedAnnotations:points];//显示标记的气泡信息,数组时默认显示数组中的第一个
//定位管理 会跳出提示问是否定位选择yes 的时候会点用下面 didUpdateLocations 协议方法 no的说时调用didFailWithError这个方法
self.locationManger =[[[CLLocationManager alloc]init]autorelease];
_locationManger.delegate=self;
[self.locationManger startUpdatingLocation];
}
#pragma -mark locationManger delegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError
{
}
//允许定位时候调用这个方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
{
if(locations){
self.mapview.showsUserLocation=YES;//开始定位用户的位置(回调 mapview的一个协议方法didUpdateUserLocation)
}
[self.locationManger stopUpdatingLocation];
}
#pragma -mark mapview delegate
//重写方法,自己定义大头针的效果
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{// annotation 会带入两张bu一样的 大头针效果 一个时自定义的 一个时系统自带的。 我们在NSArray *points时候就又加入两种bu一样的效果,所以在下面会又判断
if([annotation isKindOfClass:[MKPointAnnotation class]])
{
static NSString *identify=@"identify";
//应用到的时apple给的大头针的话时要用MKPinAnnotationView;
MKPinAnnotationView *annotationview=(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];
if(!annotationview)
{
MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identify];
}
canShowCallout=YES;//点击标记显示试图气泡
//大头针的动态下落的效果
animatesDrop=YES;
return
}
else
{
static NSString *identifyself=@"identifyself";
MKAnnotationView *annotationview=[mapView dequeueReusableAnnotationViewWithIdentifier:identifyself];
if(!annotationview)
{
MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifyself]autorelease];
}
canShowCallout=YES;
Celoatainntation *ces=(Celoatainntation *)annotation;
image=ces.image;
return
}
return nil;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation
{
self.mapview addAnnotation:userLocation];//显示定位
}
实现地图的位置查询和交通搜索 这里时又新建了一个项目
- (void)viewDidLoad
{
[super viewDidLoad];
//添加地图视图,锁定显示定位范围,添加标记针头,
//添加新功能LocalSearchquset初始化,添加寻找地方名,请求范围,初始化寻找,bing strat
// Do any additional setup after loading the view, typically from a nib.
self.mapview =[[[MKMapView alloc]initWithFrame:self.view.bounds]autorelease];
self.view addSubview:_mapview];
CLLocationCoordinate2D coordinate=CLLocationCoordinate2DMake(24.489084, 118.103338);
//显示范围以经纬度做单位
MKCoordinateSpan span=MKCoordinateSpanMake(0.08, 0.08);
[self.mapview setRegion:MKCoordinateRegionMake(coordinate,span) animated:YES];
//添加标记
MKPointAnnotation *point=[[[MKPointAnnotation alloc]init]autorelease];
title= @"人才中心";
subtitle=@"欢迎来到美丽的小岛";
point.coordinate=CLLocationCoordinate2DMake(24.489084, 118.103338);//标记经纬度
[self.mapview setSelectedAnnotations:[NSArray arrayWithObject:point]];
MKLocalSearchRequest *seachrequest=[[[MKLocalSearchRequest alloc]init]autorelease];
seachrequest.naturalLanguageQuery=@"酒店";
region=MKCoordinateRegionMake(coordinate, span);
MKLocalSearch *locasearch=[[MKLocalSearch alloc]initWithRequest:seachrequest];
startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError
if(error)
{
}
else
{
for(MKMapItem *mapitem in response.mapItems)
{
MKPointAnnotation *pointk=[[[MKPointAnnotation alloc]init]autorelease];
title=mapitem.name;
MKPlacemark *placemark=mapitem.placemark;
//获取经纬度
coordinate=placemark.location.coordinate;
subtitle=[placemark.addressDictionary objectForKey:@"Street"];
_mapview addAnnotation:pointk];
}
}
}];
//查询路线
MKPlacemark *placmark=[[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(24.446294, 118.073174) addressDictionary:nil];
MKMapItem *mapiten=[[[MKMapItem alloc]initWithPlacemark:placmark]autorelease];
name=@"地方名";
[MKMapItem openMapsWithItems:[NSArray arrayWithObject:mapiten] launchOptions:[NSDictionary dictionaryWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}