在ios开发iphone的程序中,需要在地图上标记一个或者多个点,同时点击以后可以切换到其他界面
开发步骤说明:
项目框架中加入 Mapkit Framework
在 地图上每一个记号,都是一個MKAnnotationVIEw,也就是UI。而每一個MKAnnotationVIEw都需要有对应的资料 MKAnnotation,这是Protocal,也就是存储每個座坐标所需要用到的资料的地方。因此,我们要先建立一個使用MKAnnotation的类别。
依照iPhone开发者文件的说明。这个Protocal需要声明三个属性和一个初始化方法。三个属性分別是coordinate、Title、subTitle,和一个方法initWithCoords。
开发步骤:
定义一个MKAnnotation.
//// SD4SAnnotation.h// Selfdriving//// Created by 泽宇 徐 on 12-6-5.// copyright (c) 2012年 WenBinGao. All rights reserved.//#import <Foundation/Foundation.h>#import "MapKit/MapKit.h"@interface SD4SAnnotation : NSObject<MKAnnotation>{ CLLocationCoordinate2D coordinate; Nsstring * Title; Nsstring * subTitle; //自己定义的其他信息成员}@property(nonatomic,Readonly) CLLocationCoordinate2D coordinate;@property(nonatomic,retain) Nsstring *Title;@property(nonatomic,retain) Nsstring *subTitle;-(ID) initWithCoordinate:(CLLocationCoordinate2D) temp_coordinate;@end
//// SD4SAnnotation.m// Selfdriving//// Created by 泽宇 徐 on 12-6-5.// copyright (c) 2012年 WenBinGao. All rights reserved.//#import "SD4SAnnotation.h"@implementation SD4SAnnotation@synthesize coordinate;@synthesize Title;@synthesize subTitle;-(ID) initWithCoordinate:(CLLocationCoordinate2D) temp_coordinate{ if ([super init]) { coordinate=temp_coordinate; } return self;}-(voID) dealloc{ [Title release]; [subTitle release]; [super dealloc];}@end
在vIEwController文件中实现显示地图,标记点,点击标记点后跳转的功能
vIEwController.h文件
//// SD4SListVIEwController.h// Selfdriving//// Created by WenBin Gao on 12-5-24.// copyright (c) 2012年 WenBinGao. All rights reserved.//#import <UIKit/UIKit.h>#import "MapKit/MapKit.h"#import "SD4SAnnotation.h"@interface SD4SListVIEwController : SDBaseVIEwController<CLLocationManagerDelegate,MKMapVIEwDelegate>{ IBOutlet MKMapVIEw * mapVIEw; NSMutableArray *mapAnnotations;}@property(nonatomic,retain) MKMapVIEw * mapVIEw; //地图控件@property (nonatomic,retain) NSMutableArray *mapAnnotations; //标记点的数组@end
在vIEwController.m文件中实现
//// SD4SListVIEwController.m// Selfdriving//// Created by WenBin Gao on 12-5-24.// copyright (c) 2012年 WenBinGao. All rights reserved.//#import "SD4SListVIEwController.h"#import "SD4SDetailVIEwController.h"@interface SD4SListVIEwController ()@end@implementation SD4SListVIEwController@synthesize mapVIEw;@synthesize mapAnnotations;- (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; self.mapVIEw.mapType = MKMapTypestandard; // also MKMapTypeSatellite or MKMapTypeHybrID // create a custom navigation bar button and set it to always says "Back" // create out annotations array (in this example only 1) self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:1]; // annotation for the City of shanghai CLLocationCoordinate2D cLLocationCoordinate2D ; cLLocationCoordinate2D.latitude = 31.223078; cLLocationCoordinate2D.longitude = 121.4748; SD4SAnnotation * annotation = [[SD4SAnnotation alloc] initWithCoordinate:cLLocationCoordinate2D]; annotation.Title=@"宝马4S"; annotation.subTitle=@"宝驴他哥"; [self.mapAnnotations insertObject:annotation atIndex:0]; [annotation release]; [self gotolocation]; // finally goto shanghai [self.mapVIEw removeAnnotations:self.mapVIEw.annotations]; // remove any annotations that exist [self.mapVIEw addAnnotations:self.mapAnnotations];}- (voID)gotolocation{ NSLog(@"定位上海"); // start off by default in San Francisco MKCoordinateRegion newRegion; newRegion.center.latitude = 31.223078; newRegion.center.longitude = 121.47480; newRegion.span.latitudeDelta = 0.112872; newRegion.span.longitudeDelta = 0.109863; [self.mapVIEw setRegion:newRegion animated:YES];}- (MKAnnotationVIEw *)mapVIEw:(MKMapVIEw *)theMapVIEw vIEwForAnnotation:(ID <MKAnnotation>)annotation{ // if it's the user location,just return nil. if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // handle our two custom annotations // if ([annotation isKindOfClass:[SD4SAnnotation class]]) { // try to dequeue an existing pin vIEw first static Nsstring* shop4SAnnotationIDentifIEr = @"Shop4SAnnotationIDentifIEr";//4s店 MKPinAnnotationVIEw* pinVIEw = (MKPinAnnotationVIEw *) [mapVIEw dequeueReusableAnnotationVIEwWithIDentifIEr:shop4SAnnotationIDentifIEr]; if (!pinVIEw) { // if an existing pin vIEw was not available,create one MKPinAnnotationVIEw* customPinVIEw = [[[MKPinAnnotationVIEw alloc] initWithAnnotation:annotation reuseIDentifIEr:shop4SAnnotationIDentifIEr] autorelease]; customPinVIEw.pincolor = MKPinAnnotationcolorPurple; customPinVIEw.animatesDrop = YES; customPinVIEw.canShowCallout = YES; // add a detail disclosure button to the callout which will open a new vIEw controller page // // note: you can assign a specific call out accessory vIEw,or as MKMapVIEwDelegate you can implement: // - (voID)mapVIEw:(MKMapVIEw *)mapVIEw annotationVIEw:(MKAnnotationVIEw *)vIEw calloutAccessoryControlTapped:(UIControl *)control; // UIbutton* rightbutton = [UIbutton buttonWithType:UIbuttonTypeDetaildisclosure]; [rightbutton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventtouchUpInsIDe]; customPinVIEw.rightCalloutAccessoryVIEw = rightbutton; return customPinVIEw; } else { pinVIEw.annotation = annotation; } return pinVIEw; } return nil;}- (voID)vIEwDIDUnload{ [super vIEwDIDUnload]; // Release any retained subvIEws of the main vIEw. // e.g. self.myOutlet = nil;}- (BOol)shouldautorotatetoInterfaceOrIEntation:(UIInterfaceOrIEntation)interfaceOrIEntation{ return (interfaceOrIEntation == UIInterfaceOrIEntationPortrait);}-(voID) dealloc{ [self.mapAnnotations release]; [self.mapVIEw release]; [super dealloc];}@end
Annotation 集合中有几个坐标点vIEwForAnnotation方法就会被执行几次。因此每次vIEwForAnnotation被执行,我们都要实例化一个MKAnnotationVIEw。 总结
以上是内存溢出为你收集整理的ios 开发 iphone和ipad程序中使用google地图的方法全部内容,希望文章能够帮你解决ios 开发 iphone和ipad程序中使用google地图的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)