iphone 程序内调用谷歌地图

来源:互联网 发布:软件开发流程规范 编辑:程序博客网 时间:2024/06/15 21:34

使用CLLocationManager类,MKMapView并且实现<MKMapViewDelegate,CLLocationManagerDelegate>
//初始化CLLocationManager,CLLocationManager获得当前地理坐标
locmanager=[[CLLocationManageralloc]init];

[locmanager setDelegate:self];
 //设置精确度
[locmanagersetDesiredAccuracy:kCLLocationAccuracyBest];

[locmanagerstartUpdatingLocation];
执行完以后,会自动调用代理方法:

在代理方法:


- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation{
//初始化矩形大小
 CGRect rect=CGRectMake(0,0,320,460);
//设置地图大小为矩形大小
map=[[MKMapView alloc] initWithFrame:rect];

CLLocationCoordinate2Dloc=[newLocation coordinate];
lat=loc.latitude;
lon=loc.longitude;

//coordinate坐标
CLLocationCoordinate2DtheCoordinate;
CLLocationCoordinate2DtheCenter;

//theCoordinate.latitude=lat;
//theCoordinate.longitude=lon;
theCoordinate=loc;
[map setDelegate:self];
//设置地图显示的类型,有卫星地图,道路,等
[map setMapType:MKMapTypeStandard];
//[mapsetMapType:MKMapTypeSatellite];
//区域坐标Region(区域,地域)

MKCoordinateRegiontheRegin;
//theCenter.latitude=lat;
//theCenter.longitude=lon;
theCenter=loc;
theRegin.center=theCenter;

//坐标间距(span:间隔,间距)
MKCoordinateSpantheSpan;

theSpan.latitudeDelta=0.1;
theSpan.longitudeDelta=0.1;
//设置地图显示的区域,
theRegin.span=theSpan;
//[mapsetRegion:theRegin];
[mapregionThatFits:theRegin];
map.showsUserLocation=YES;
[self.viewaddSubview:map];

}


- (MKAnnotationView *)mapView:(MKMapView *)mapViewviewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(@"-------viewForAnnotation-------");
//此类可以显示针一样的图标
MKPinAnnotationView*newAnnotation=[[MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"annotation1"];

//newAnnotation.animatesDrop=YES;
//newAnnotation.animatesDrop=NO;

newAnnotation.pinColor=MKPinAnnotationColorPurple;
//显示标志提示
newAnnotation.canShowCallout=YES;
return newAnnotation;
}


原文地址:http://www.cocoachina.com/bbs/read.php?tid=73570&page=4