iphone MKMapView
来源:互联网 发布:mac好用的软件 编辑:程序博客网 时间:2024/05/21 08:47
目前几乎所有服务行业的产品都考虑到加入地图的功能,这大大提升了软件的易用和直观性。在iphone 中运用地图并不是一件困难的事情,因为SDK中已经提供了地图控件。
下边是我总结iphone地图的用法。
1。
现在.h文件中添加地图头文件
#import <MapKit/MapKit.h>
2。
在.h文件中添加地图委托<MKMapViewDelegate>
3。
在.h文件中声明地图成员变量以及用到的函数
//地图变量
MKMapView *mapView;
//地图自动缩放。用于在设置过MapAnnotation地标后,执行次函数,就会自动的缩放地图到合适的大小。
- (void)zoomToFitMapAnnotations:(MKMapView*)inMapView;
4。
在.m的init函数中创建地图控件
//map
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 44, 320, self.view.frame.size.height -88)];
[mapView setMapType: MKMapTypeStandard];
mapView.delegate=self;
[self.view addSubview:mapView];
5。
在dealloc函数中释放
[mapView release];
。
移除所有地标
[mapView removeAnnotations: [mapView annotations]];
。
#pragma mark -
#pragma mark 地图委托
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)iAnnotation{
DebugLog(@"viewForAnnotation");
if ([iAnnotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([iAnnotation isKindOfClass:[MapAnnotation class]]) // for Golden Gate Bridge
{
MapAnnotation *annotation = iAnnotation;
static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
static NSString *UserIdentifier = @"UserIdentifier";
NSString *Identifier = nil;
if (annotation.tag == -1) {
Identifier = AnnotationIdentifier;
}else {
Identifier = UserIdentifier;
}
MKAnnotationView* pinView = (MKAnnotationView *) [mapViewdequeueReusableAnnotationViewWithIdentifier:Identifier];
//如果不存在的情况就创建
if (!pinView)
{
if ([Identifier isEqualToString:AnnotationIdentifier]) {
//自定义的大头针样式,这样的好处是,点击此按钮,会弹出一个提示框,并且提升框可以响应点击。
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:iAnnotation reuseIdentifier:Identifier]autorelease];
[(MKPinAnnotationView *)pinView setPinColor:MKPinAnnotationColorRed];
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(curPositionButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[pinView setImage:[UIImage imageNamed:@"datouzhen.png"]];
pinView.rightCalloutAccessoryView = button;
/*
//默认的红色大头针,点击只能显示提示框,但不能点击此按钮。
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:iAnnotation reuseIdentifier:Identifier] autorelease];
[(MKPinAnnotationView *)pinView setPinColor:MKPinAnnotationColorRed];
*/
}else {
pinView = [[[MKAnnotationView alloc] initWithAnnotation:iAnnotation reuseIdentifier:Identifier]autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.tag = annotation.tag;
[button addTarget:self action:@selector(annotationButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[pinView setImage:[UIImage imageNamed:@"ann.png"]];
pinView.rightCalloutAccessoryView = button;
}
pinView.canShowCallout =YES;
return pinView;
}
//如果这个已经存在,就需要重新给pinView.annotation设置值。
else
{
pinView.annotation = iAnnotation;
}
return pinView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)viewcalloutAccessoryControlTapped:(UIControl *)control {
}
//选中了红色大头针,会显示提示按钮。点击按钮执行的函数。
- (void)curPositionButtonPressed:(UIButton *)inButton {
//inButton.tag
}
//单击某一个annotation标签的时候,会显示一个提示,提示框上有一个右箭头按钮。点击此按钮的时候,执行此函数。
- (void)annotationButtonPressed:(UIButton *)inButton {
//inButton.tag
}
//插入图钉。并设置图钉的名称和位置
-(void) setPointAnnotation{
[mapView removeAnnotations: [mapView annotations]];
CLLocationCoordinate2D coordinate = {_position.latitude, _position.longitude};
MapAnnotation *annotation = [[[MapAnnotation alloc] initWithCoordinate:coordinate] autorelease];
annotation.tag = -1;
annotation.title = @"提示信息";
[mapView addAnnotation:annotation];
[mapView selectAnnotation:annotation animated:YES];
}
- iphone--MKMapView
- iphone MKMapView
- iphone 地图应用。MKMapView
- iphone开发----计算MKMapView的缩放等级
- MKMapView
- CLLocationManager定位经纬度,MKReverseGeocoder地址解析, MKMapView 地图显示 iphone
- iphone获取 用户坐标详细信息 MKPlacemark userLocation MKMapView
- iphone获取 用户坐标详细信息 MKPlacemark userLocation MKMapView
- CLLocationManager定位经纬度,MKReverseGeocoder地址解析, MKMapView 地图显示 iphone
- iPhone 上利用MKMapView实现简单地图的方法
- [iPhone程式]iPhone開發心得04-Mapkit之使用MKMapView實作Google Map
- iPhone开发之在MkMapView中在指定位置添加大头针
- MKMapView用法
- MKMapView展示
- MKMapView 相关
- MKMapView用法
- MKMapView小结
- MKMapView小结
- 为解压版tomcat添加windows服务
- I2C 设备总线上最小上升时间规定
- DOM html window.showModalDialog的传值和返回值
- C#编程中的66个好习惯,你有多少个
- VS2010 Fixdays
- iphone MKMapView
- 网页编程中文件的上传
- SVN服务器及客户端的简单使用
- MVP和MVC
- 在FlashBuilder里的ActionScript工程中使用Flash CS5中的类 fl.controls库
- 在java程序中调用 .sh文件上,以相对路径
- Qt创建那个XML文档,并且对它实现查找,更新,插入等操作
- 江德斌:淘宝小商城“暴动”是电子商务的拐点
- Vissual Assist X更改缓存路径,减少C盘空间占用