#import "MapViewController.h"
@implementationMapViewController
-(void)dealloc
{
[_locManagerrelease];
[superdealloc];
}
-(void)viewDidLoad
{
[superviewDidLoad];
self.navigationItem.title =@"雨松MOMO";
myMapView=[[[MKMapViewalloc]initWithFrame:CGRectMake(0,0,320,480)]autorelease];
myMapView.delegate=self;
//在这里先让地图视图隐藏起来,
//等获取当前经纬度完成后在把整个地图显示出来
myMapView.hidden=true;
[self.viewaddSubview:myMapView];
//创建定位管理器,
_locManager=[[CLLocationManageralloc]init];
[_locManagersetDelegate:self];
[_locManagersetDesiredAccuracy:kCLLocationAccuracyBest];
}
-(void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//开始使用手机定位,这是一个回调方法,
//一旦定位完成后程序将进入
//- (void)locationManager:(CLLocationManager *)manager
//didUpdateToLocation:(CLLocation *)newLocation
//fromLocation:(CLLocation *)oldLocation
//方法中
[_locManagerstartUpdatingLocation];
}
//定位成功后将进入此方法
-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation
{
//得到当前定位后的经纬度,当前经纬度是有一定偏移量的,
//使用另一种方法可以很好的解决这个问题
CLLocationCoordinate2D loc =[newLocationcoordinate];
floatlat= loc.latitude;
floatlon=loc.longitude;
//让MapView使用定位功能。
myMapView.showsUserLocation=YES;
//更新地址,
[managerstopUpdatingLocation];
//设置定位后的自定义图标。
MKCircle*circle=[MKCirclecircleWithCenterCoordinate:CLLocationCoordinate2DMake(myMapView.userLocation.location.coordinate.latitude,myMapView.userLocation.location.coordinate.longitude)radius:5000];
//一定要使用addAnnotation 方法把MKCircle加入进视图,
// 否则下面刷新图标的方法是永远不会进入的 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
//切记!!!!
[myMapViewaddAnnotation:circle];
//我们需要通过当前用户的经纬度换成出它现在在地图中的地名
CLGeocoder*geocoder=[[[CLGeocoderalloc]init]autorelease];
[geocoderreverseGeocodeLocation:_locManager.locationcompletionHandler:
^(NSArray*placemarks,NSError*error){
//得到自己当前最近的地名
CLPlacemark*placemark=[placemarksobjectAtIndex:0];
NSString*locatedAt=[[placemark.addressDictionaryvalueForKey:@"FormattedAddressLines"]componentsJoinedByString:@", "];
//locatedAt就是当前我所在的街道名称
//上图中的中国北京市朝阳区慧中北路
[myMapView.userLocationsetTitle:locatedAt];
[myMapView.userLocationsetSubtitle:@"雨松MOMO在这里噢"];
//这里是设置地图的缩放,如果不设置缩放地图就非常的尴尬,
//只能光秃秃的显示中国的大地图,但是我们需要更加精确到当前所在的街道,
//那么就需要设置地图的缩放。
MKCoordinateRegion theRegion ={{0.0,0.0},{0.0,0.0}};
theRegion.center=myMapView.userLocation.location.coordinate;
//缩放的精度。数值越小约精准
theRegion.span.longitudeDelta=0.01f;
theRegion.span.latitudeDelta=0.01f;
//让MapView显示缩放后的地图。
[myMapViewsetRegion:theRegionanimated:YES];
//最后让MapView整体显示, 因为截至到这里,我们已经拿到用户的经纬度,
//并且已经换算出用户当前所在街道的名称。
myMapView.hidden=false;
}];
}
//定位失败后将进入此方法
-(void)locationManager:(CLLocationManager*)managerdidFailWithError:(NSError*)error{
if([errorcode]==kCLErrorDenied)
{
//第一次安装含有定位功能的软件时
//程序将自定提示用户是否让当前App打开定位功能,
//如果这里选择不打开定位功能,
//再次调用定位的方法将会失败,并且进到这里。
//除非用户在设置页面中重新对该软件打开定位服务,
//否则程序将一直进到这里。
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"定位服务已经关闭"
message:@"请您在设置页面中打开本软件的定位服务"
delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alertshow];
[alertrelease];
[managerstopUpdatingHeading];
}
elseif([errorcode]==kCLErrorHeadingFailure)
{
}
}
//在这里我们设置自定义图标来 标志当前我在地图的地方
- (MKAnnotationView*)mapView:(MKMapView*)mapViewviewForAnnotation:(id<MKAnnotation>)annotation;
{
staticNSString*identifier=@"com.xys.momo";
MKAnnotationView*pin=[mapViewdequeueReusableAnnotationViewWithIdentifier:identifier];
if(!pin)
{
pin=[[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:identifier];
//随便加载了一张ICON
//我的icon的大小是48X48 大家可根据仔细的喜好制定自己的icon
pin.image=[UIImageimageNamed:@"0.jpg"];
//在图中我们可以看到图标的上方,有个气泡弹窗里面写着当前用户的位置所在地
//原因是这里需要设置了True
pin.canShowCallout=YES;
//上图气泡的右侧还有一个带箭头的小按钮
//这个按钮就是在这里创建的,不过MOMO目前没有写按钮的响应事件喔。
//细心的朋友可以自己加上。
UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView=btn;
}
pin.annotation=annotation;
returnpin;
}
-(void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return(interfaceOrientation==UIInterfaceOrientationPortrait);
}
@end