ios mkmapview 缩放怎么显示中文的地图

IOS自带地图MKMapView - 简书
IOS自带地图MKMapView
首先需要导入MapKit.framework。如果需要加标注并且显示不同的图片。就需要自己定义类实现MKAnnotation协议。在下面方法里面判断是那种类型然后显示不同的图片- (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation{ if([annotationisKindOfClass:[YujuShangdianAnnotationclass]]) {MKAnnotationView*annotationView =[self.mapViewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];if(!annotationView) {annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"CustomAnnotation"] ;annotationView.canShowCallout=NO;annotationView.image= [UIImageimageNamed:@"mapyujushangdian"];}returnannotationV}elseif([annotationisKindOfClass:[YuchuanMaTouAnnotationclass]]) {MKAnnotationView*annotationView =[self.mapViewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];if(!annotationView) {annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"CustomAnnotation"] ;annotationView.canShowCallout=NO;annotationView.image= [UIImageimageNamed:@"mapyuchuanmatou"];}returnannotationV}elseif([annotationisKindOfClass:[YushichangAnnotationclass]]) {MKAnnotationView*annotationView =[self.mapViewdequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];if(!annotationView) {annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"CustomAnnotation"] ;annotationView.canShowCallout=NO;annotationView.image= [UIImageimageNamed:@"mapfangzi"];}returnannotationV}}判断一个Annotation是否在当前地图中可见MKMapRectvisibleMapRect=self.mapView.visibleMapRNSSet*visibleAnnotation=[self.mapViewannotationsInMapRect:visibleMapRect];if([visibleAnnotationcontainsObject:anno]) {}将地图缩放到某个合适的位置 使一些Annotation同时可见MKPointAnnotation*pointann=[[MKPointAnnotationalloc]init];CLLocationCoordinate2Dcoorstart.latitude=40;coorstart.longitude=115;pointann.coordinate=[_mapViewaddAnnotation:pointann];MKPointAnnotation*pointann2=[[MKPointAnnotationalloc]init];CLLocationCoordinate2Dcoorstart2;coorstart2.latitude=40;coorstart2.longitude=116;pointann2.coordinate=coorstart2;[_mapViewaddAnnotation:pointann2];[self.mapViewshowAnnotations:@[pointann,pointann2]animated:YES];控制MapView动画的时间[MKMapViewanimateWithDuration:0.8delay:0.0options:UIViewAnimationOptionCurveEaseInOutanimations:^{[self.mapViewsetCenterCoordinate:anno.coordinateanimated:YES];} completion:^(BOOLfinished) {}];
学习如逆水行舟,不进则退IPhone开发时使用MKMapView在显示地图的同时获取用户当前坐标的方法 - WURD&JAVA - ITeye技术网站
博客分类:
IPhone开发时使用MKMapView在显示地图的同时获取用户当前坐标的方法
最近在公司有一个项目需要在IPhone手机上显示地图并获取到用户当前的位置信息,从网上参考了很多大侠的文章。发现均是同时使用MKMapView和CLLocationManager两个类才实现的。其实只需要使用MKMapView一个类就可以同时实现显示地图并获取到坐标的功能。
按我目前的理解:
MKMapView应该是使用在需要显示地图的程序中。
CLLocationManager应该是使用在不需要显示地图但需要获取用户坐标信息的程序中。
另外,从网上看到过一位同志说MKMapView获取到的坐标精度要高于CLLocationManager获取到的坐标精度,因为没有测试,不敢妄下结论。
程序的核心部分,其实就是使用了MKMapView的didUpdateUserLocation事件,有关MKMapView的使用方法网上有一大堆,请自行搜索参考。以下是相关代码,已经在XCode4.3 IOS5.0的手机上测试通过,供大家参考。
程序头文件:
#import &CoreLocation/CoreLocation.h&
#import &MapKit/MapKit.h&
@interface UnitMapViewController : UIViewController&MKMapViewDelegate&
MKMapView *
程序源文件:
#import "UnitMapViewController.h"
#import "UnitViewController.h"
@interface UnitMapViewController ()
@implementation UnitMapViewController
NSString *userL
NSString *
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
userLatitude=@"";
userlongitude=@"";
map=[[MKMapView alloc] initWithFrame:[self.view bounds]];
map.mapType=MKMapTypeStandard;
map.delegate=self;
map.showsUserLocation=YES;
[self.view addSubview:map];
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSString *lat=[[NSString alloc] initWithFormat:@"%f",userLocation.coordinate.latitude];
NSString *lng=[[NSString alloc] initWithFormat:@"%f",userLocation.coordinate.longitude];
userLatitude=
userlongitude=
MKCoordinateSpan
MKCoordinateRegion
span.latitudeDelta=0.010;
span.longitudeDelta=0.010;
region.span=
region.center=[userLocation coordinate];
[map setRegion:[map regionThatFits:region] animated:YES];
浏览 11773
浏览: 55183 次
来自: 兰州
自动定位给的是地球坐标..中国地图必须用火星坐标.
adler0518 写道你好,我有个疑问。你说的第一条,sle ...
该线程会暂停执行,被调对象进入等待状态============ ...
我照你说的做了,到最后一步把WAR包发布到了JBSOO里面,可 ...
你好,我有个疑问。
你说的第一条,sleep时间满后不一定立即 ...下次自动登录
关注移动互联网和移动APP开发工具、开发框架、测试工具、微信开发、Android源码、Android开源类库以及各种开源组件的IT科技网站
现在的位置:
iOS开发——MKMapView地图控件的性能优化
最近做的项目中都有LBS这个模块,其中又涉及到MKMapView控件的使用,因此MKMapView性能的好坏直接决定了地图加载的速度,直接影响到用户体验。本文重点讲述如何优化MKMapView控件,提高地图的加载速度,希望对朋友们带来帮助。
最近做的项目主要是LBS这块 主打成员定位功能 我们的UI设计是这样的
乍一看上去是挺好挺美观的 不同的人会显示不同的头像 可是当人扎堆的时候 问题就来了
当人多的时候(例如上图所示) 地图滑动起来就能感觉到明显顿卡 那种不流畅感能折磨死人 所以 自然我们要解决这个问题(等等 先不要吐槽为什么不用地图聚合 因为这已经是地图放到最大了 聚合不适合这次的问题讨论)
首先看下我是怎么实现这个annotationView的 由于这个annotationsView是异形的(也就是无法通过设置圆角直接得到) 而且里面的图片还因用户而异 所以解决方案就是使用layer.mask来进行遮罩 代码如下:
@implementation MMAnnotationView
- (instancetype)initWithAnnotation:(id&MKAnnotation&)annotation reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if ( self )
self.frame = CGRectMake(0, 0, TRACK_ANNOTATION_SIZE.width, TRACK_ANNOTATION_SIZE.height);
self.centerOffset = CGPointMake(0, -(TRACK_ANNOTATION_SIZE.height-3)/2);
self.canShowCallout = NO;
self.avatarView = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:self.avatarView];
self.avatarView.contentMode = UIViewContentModeScaleAspectF
CAShapeLayer *shapelayer = [CAShapeLayer layer];
shapelayer.frame = self.
shapelayer.path = self.framePath.CGP
self.avatarView.layer.mask =
self.layer.shadowPath = self.framePath.CGP
self.layer.shadowRadius = 1.0f;
self.layer.shadowColor = [UIColor colorWithHex:0x666666FF].CGC
self.layer.shadowOpacity = 1.0f;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.masksToBounds = NO;
//mask路径
- (UIBezierPath *)framePath
if ( !_framePath )
CGFloat arrowWidth = 14;
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectInset(CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetWidth(self.bounds)), 3,3);
CGPoint p[3] = {
{CGRectGetMidX(self.bounds)-arrowWidth/2, CGRectGetWidth(self.bounds)-6},
{CGRectGetMidX(self.bounds)+arrowWidth/2, CGRectGetWidth(self.bounds)-6},
{CGRectGetMidX(self.bounds), CGRectGetHeight(self.bounds)-4}
CGPathAddRoundedRect(path, NULL, rectangle, 5, 5);
CGPathAddLines(path, NULL, p, 3);
CGPathCloseSubpath(path);
_framePath = [UIBezierPath bezierPathWithCGPath:path];
CGPathRelease(path);
return _frameP
我用代码生成了形状路径 并以此生成了layer的mask和shadowPath,使用时 只要直接用SDWebImage设置头像就行了
[annotationView.avatarView sd_setImageWithURL:[NSURL URLWithString:avatarURL] placeholderImage:placeHolderImage];
接下来用工具分析一下问题出来哪 分析性能当然是选择Instrments(用法在这里就不做介绍了) 打开Core Animation 然后运行程序 滑动地图 可以看到性能分析如下
原来平均帧数只有不到30帧 这离我们的目标60帧差得实在太远
再使用Debug Option来深入分析一下
由于MKMapView的原因 这里我们主要关心这几个选项
Color Blended Layers
Color Misaligned Images
Color Offscreen-Rendered Yellow
分别打开这几个选项 结果如下
Color Blended Layers没有问题 不过这也是正常的 由于使用了mask 没有透明的地方
Color Misaligned Images除了默认头像外全中 这是因为服务器上的图片大小跟显示的大小不一致 导致缩放 而默认头像则是一致的 所以没问题
Color Offscreen-Rendered Yellow全中 由于使用了mask 导致大量的离屏渲染 这也是性能下降的主要原因
问题的原因找到了 那么接下来该如何解决呢?
首先mask是肯定不能用了
其次下载下来的图片我们要预处理成实际大小
那么 直接把下载下来的图片合成为我们要显示的最终结果不就ok了吗? 试试看
- (void)loadAnnotationImageWithURL:(NSString*)url imageView:(UIImageView*)imageView
//将合成后的图片缓存起来
NSString *annoImageURL =
NSString *annoImageCacheURL = [annoImageURL stringByAppendingString:@"cache"];
UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:annoImageCacheURL];
if ( cacheImage )
//LLLog(@"hit cache");
imageView.image = cacheI
//LLLog(@"no cache");
[imageView sd_setImageWithURL:[NSURL URLWithString:annoImageURL]
placeholderImage:placeHolderImage
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (!error)
UIImage *annoImage = [image annotationImage];
imageView.image = annoI
[[SDImageCache sharedImageCache] storeImage:annoImage forKey:annoImageCacheURL];
@implementation UIImage (LJC)
- (UIImage*) annotationImage
static UIView *snapshotView =
static UIImageView *imageView =
if ( !snapshotView )
snapshotView = [UIView new];
snapshotView.frame = CGRectMake(0, 0, TRACK_ANNOTATION_SIZE.width, TRACK_ANNOTATION_SIZE.height);
imageView = [UIImageView new];
[snapshotView addSubview:imageView];
imageView.clipsToBounds = YES;
imageView.frame = snapshotView.
imageView.contentMode = UIViewContentModeScaleAspectF
CGFloat arrowWidth = 14;
CGMutablePathRef path = CGPathCreateMutable();
CGRect rectangle = CGRectInset(CGRectMake(0, 0, CGRectGetWidth(imageView.bounds), CGRectGetWidth(imageView.bounds)), 3,3);
CGPoint p[3] = {
{CGRectGetMidX(imageView.bounds)-arrowWidth/2, CGRectGetWidth(imageView.bounds)-6},
{CGRectGetMidX(imageView.bounds)+arrowWidth/2, CGRectGetWidth(imageView.bounds)-6},
{CGRectGetMidX(imageView.bounds), CGRectGetHeight(imageView.bounds)-4}
CGPathAddRoundedRect(path, NULL, rectangle, 5, 5);
CGPathAddLines(path, NULL, p, 3);
CGPathCloseSubpath(path);
CAShapeLayer *shapelayer = [CAShapeLayer layer];
shapelayer.frame = imageView.
shapelayer.path =
imageView.layer.mask =
snapshotView.layer.shadowPath =
snapshotView.layer.shadowRadius = 1.0f;
snapshotView.layer.shadowColor = [UIColor colorWithHex:0x666666FF].CGC
snapshotView.layer.shadowOpacity = 1.0f;
snapshotView.layer.shadowOffset = CGSizeMake(0, 0);
CGPathRelease(path);
imageView.image =
UIGraphicsBeginImageContextWithOptions(TRACK_ANNOTATION_SIZE, NO, 0);
[snapshotView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *copied = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然后使用的时候 只要简单的如下调用就OK了
[self loadAnnotationImageWithURL:avatarURL imageView:annotationView.avatarView];
看看修改之后的Instruments表现如何
Color Blended Layers全中 这也是无可避免的 因为显示的就是一张带透明度的图 但是由于地图的特殊性(头像的位置变化间隔较长 所以不会经常引发合成 也没有动画) 所以这里也不是问题
Color Misaligned Images没问题了 因为头像已被缩放成了相同大小
Color Offscreen-Rendered Yellow没问题了 因为只是简单的显示了一张图片 而并没有需要离屏渲染的东西了
再来看下帧数情况
Oh-Yeah~ 不光帧数达到了我们的目标60帧(由于还有业务逻辑线程在后台跑 所以没有那么的稳定) 就连平均运行耗时都下降了不少 就算地图上再多显示几十个人 也不成问题了
不光是MKMapView 其实包括UITableView在内的很多地方都可以用文中所说的方法去优化 其核心点就是 合成+缓存 当然 由于合成还是会耗费一部分资源的 所以比较适合头像这种小的资源
关于图形性能优化 可以看下(有对文中提到的Debug Option不太明白的 这里有详细的解释)
【上篇】【下篇】本帖子已过去太久远了,不再提供回复功能。ios开发中百度地图的基本使用一
ios开发中百度地图的基本使用一
最近的项目要求用百度地图,所以自己研究了一下,现在我把自己研究出来的成果给大家分享一下,如果大家有更好的方案可以互相交流哈。好了闲话少说,现在我就给大家介绍一下百度地图的使用。
1.首先要下载百度地图的SDK,打开连接就可以下载了。
2.下载后会有以下几个文件,图中选中的文件是工程中需要的,你只需要按照我的步骤将文件拷贝到工程即可
2.1将inc拷贝到你的工程(这里包含了baidumap的API文件)
2.2.引入mapapi.bundle资源文件
该步骤为可选,mapapi.bundle中存储了定位、默认大头针标注View及路线关键点的资源图片。如果您不需要使用内置的图片显示功能,则可以不添加此bundle文件。您也可以根据具体需求任意替换或删除该bundle中的图片文件。?添加方式:将mapapi.bundle拷贝到您的工程目录,直接将该bundle文件托拽至XCode工程左侧的Groups&Files中即可。
2.3.将静态库引入工程
百度地图API提供了模拟器和真机两中环境所使用的静态库文件,分别存放在libs/Release-iphonesimulator和libs/Release-iphoneos文件夹下(即图中的两个文件下)。,大家可以参照链接中得提示去设置,我个人认为这个链接里面写的较为详细,我也是按照这里面设置的。
2.4引入系统framework
百度地图SDK中提供了定位功能和动画效果,v2.0.0版本开始使用OpenGL渲染,因此您需要在您的Xcode工程中引入CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework。 添加方式:右键点击Xcode工程左侧的Frameworks文件夹,add-&Existing
Frameworks,在弹出窗口中选中这几个framework,点击add即可。
3.申请百度地图的key
在工程中使用百度地图,还需要申请百度地图的key,注意你必须要有百度账号,如果没有可以注册一个。,申请成功后他会自动生成一个key。类似ArFPsfgf9BuCtzs4sYTYrhK8
好了以上就是xcode使用baidumap的完整配置,配置成功后你就可以开始上代码了。
代码编写部分
1.在AppDelegate中
1.1在AppDelegate.h中写如下代码
1.2在AppDelegate.m中写如下代码,箭头所指的地方就是你申请的key,如果没有key地图不会显示
2.在你要显示地图的类中添加如下代码
2.1基本地图的显示
首先导入BMKMapView.h、BMapKit.h,之后在按照下面写代码,只需要下边这几行代码就可以显示地图了,地图的类型可选,地图的使用等级最高为18(MapView.zoomLevel
2.2地图定位
平时我们使用的地图都是打开之后就会自动定位到用户当前的位置,那么我现在带大家来实现这个功能。
2.2.1首先我们要遵守BMKLocationServiceDelegate这个代理,代码如下
2.2.2实现代理方法
写完如上代码运行后地图就会显示你的当前位置,上边的这两个代理方法是一直循环着走的,只要用户的经纬度发生变化这个代理方法就会一直不间歇的走,所以当你不想让他一直走就可以将delegate置为nil。
2.3附近的兴趣点搜索
2.3.1首先遵守BMKMapViewDelegate、BMKPoiSearchDelegate两个代理,代码如下
- (IBAction)nearSearch:(id)sender {
&& & //周边检索
&& &NSArray* array = [NSArrayarrayWithArray:MapView.annotations];
&& & [MapViewremoveAnnotations:array];
&& & searcher = [[BMKPoiSearchalloc]init];
&& & searcher.delegate =self;
&& & //发起检索
&& & BMKNearbySearchOption *option = [[BMKNearbySearchOptionalloc]init];
&& & option.pageIndex =0;& & &//一般默认设置为<span style="color:#
&& & option.pageCapacity =30;&//默认<span style="color:#,最多<span style="color:#
&& & option.location =CLLocationCoordinate2D{userLoc.location.coordinate.latitude,userLoc.location.coordinate.longitude};//检索中心点的经纬度
&& & option.keyword =_nearSearchTF.text;//检索关键字(入美食、商场等)
&& &BOOL flag = [searcherpoiSearchNearBy:option];
&& &if (flag) {
&& & NSLog(@&周边检索发送成功&);
&& & }else{
&& & NSLog(@&周边检索所发送失败&);
2.3.2检索成功之后还要实现BMKPoiSearchDelegate的方法
#pragma mark -- poi检索 --
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList
errorCode:(BMKSearchErrorCode)error
& & if (error ==BMK_SEARCH_NO_ERROR) {
& & & &NSArray *arr = poiResultList.poiInfoList;
& & & &//此处是对搜索的结果进行处理(我这里使用大头针将所有搜索结果显示出来)
& & & &for (BMKPoiInfo *poiInfoin arr) {
& & & & & & //BMKPoiInfo是POI信息类
& & & & & &NSLog(@&%@ %f %f&,poiInfo.name,poiInfo.pt.latitude,poiInfo.pt.longitude);
& & & & & & BMKPointAnnotation* annotation = [[BMKPointAnnotationalloc]init];
& & & & & & annotation.coordinate = poiInfo.pt;
& & & & & & annotation.title = poiInfo.name;
& & & & & & [MapViewaddAnnotation:annotation];
& & & & & & [MapView setCenterCoordinate:poiInfo.ptanimated:YES];//设置地图中心位置
& & elseif (error ==BMK_SEARCH_AMBIGUOUS_KEYWORD){
& & }else{
& & & &NSLog(@&抱歉,未找到结果&);
#pragma mark --
添加大头针,这里实现的是BMKMapViewDelegate --
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id
&BMKAnnotation&)annotation
& &if ([annotationisKindOfClass:[BMKPointAnnotation class]])
& & & &BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@&myAnnotation&];
& & & & newAnnotationView.pinColor =BMKPinAnnotationColorPurple;//设置大头针的颜色
& & & & newAnnotationView.animatesDrop =YES;//设置该标注点动画显示
& & & &return newAnnotationV
& & returnnil;
好了如果你按照我上面的代码写下来,运行时就会有如下效果(我搜索的是附近的美食)
好了各位先写到这里,至于路径搜索我会在下篇中给大家介绍,希望对大家有帮助。
我的热门文章
即使是一小步也想与你分享

我要回帖

更多关于 ios mkmapview 详解 的文章

 

随机推荐