- 浏览: 569695 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
JYY282:
[i][/i]
Ecshop与Shopex的比较 -
qq247890212:
我也遇见这问题了,真诡异。重新下载个猫换了就好了。 太浪费时间 ...
诡异:ClassNotFoundException: org.springframework.web.filter.CharacterEncoding
From: http://www.cocoachina.com/newbie/basic/2012/0109/3846.html
本文由论坛会员leonbao2010分享
1、概述
插入MapView,设置
Delegate(一般为Controller),
Annotations记录兴趣位置点
AnnotationView用来显示兴趣位置点
annotation是可选的,选中的annotation会显示callout,用来显示信息。
2、设置地图显示类型:
mapView.mapType = MKMapTypeStandard;
mapView.mapType = MKMapTypeSatellite;
mapView.mapType = MKMapTypeHybrid;
3、显示用户位置
设置为可以显示用户位置:
mapView.showsUserLocation = YES;
判断用户当前位置是否可见(只读属性):
userLocationVisible
得到用户位置坐标:当userLocationVisible为YES时
CLLocationCoordinate2D coords = mapView.userLocation.location.coordinate;
4、坐标范围
MKCoordinateRegion用来设置坐标显示范围。
包括两部分:
- Center(CLLocationCoordinate2D struct,包括latitude和longitude),坐标中心
- Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),缩放级别
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000);
以上代码创建一个以center为中心,上下各1000米,左右各1000米得区域,但其是一个矩形,不符合MapView的横纵比例
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
以上代码创建出来一个符合MapView横纵比例的区域
[mapView setRegion:adjustedRegion animated:YES];
以上代码为:最终显示该区域
5、Delegate
使用MapView须符合MKMapViewDelegate协议
5.1、地图加载Delegate
当需要从Google服务器取得新地图时
mapViewWillStartLoadingMap:
当成功地取得地图后
mapViewDidFinishLoadingMap:
当取得地图失败后(建议至少要实现此方法)
mapViewDidFailLoadingMap:withError:
5.2、范围变化Delegate
当手势开始(拖拽,放大,缩小,双击)
mapView:regionWillChangeAnimated:
当手势结束(拖拽,放大,缩小,双击)
mapView:regionDidChangeAnimated:
判断坐标是否在MapView显示范围内:
CLLocationDegrees leftDegrees = mapView.region.center.longitude –(mapView.region.span.longitudeDelta / 2.0);
CLLocationDegrees rightDegrees = mapView.region.center.longitude +(mapView.region.span.longitudeDelta / 2.0);
CLLocationDegrees bottomDegrees = mapView.region.center.latitude –(mapView.region.span.latitudeDelta / 2.0);
CLLocationDegrees topDegrees = self.region.center.latitude +(mapView.region.span.latitudeDelta / 2.0);
if (leftDegrees > rightDegrees) { // Int'l Date Line in View
leftDegrees = -180.0 - leftDegrees;
if (coords.longitude > 0) // coords to West of Date Line
coords.longitude = -180.0 - coords.longitude;
}
If (leftDegrees <= coords.longitude && coords.longitude <=
rightDegrees && bottomDegrees <= coords.latitude &&
coords.latitude <= topDegrees) {
// 坐标在范围内
}
6、Annotation
Annotation包含两部分:Annotation Object和Annotation View
Annotation
Object必须符合协议MKAnnotation,包括两个方法:title和subtitle,分别用于显示注释的标题和子标题。还有
coordinate属性,返回CLLocationCoordinate2D,表示Annotation的位置
然后,需使用mapView:viewForAnnotation: 方法来返回MKAnnotationView或者MKAnnotationView的子类用来显示Annotation(注意:这里显示的不是选中Annotation后的弹出框)
你可以子类化MKAnnotationView,然后再drawRect:方法里面进行自己的绘制动作(这个方法很蠢)
你完全可以实例化一个MKAnnotationView,然后更改它的image属性,这样很简单。
7、添加移除Annotation
添加一个Annotation
[mapView addAnnotation:annotation];
添加一个Annotation数组
[mapView addAnnotations:[NSArray arrayWithObjects:annotation1, annotation2, nil]];
移除一个Annotation
removeAnnotation:
移除一个Annotation数组
removeAnnotations:
移除所有Annotation
[mapView removeAnnotations:mapView.annotations];
8、选中Annotation
一次只能有一个Annotation被选中,选中后会出现CallOut(浮动框)
简单的CallOut显示Title和SubTitle,但你也可以自定义一个UIView作为CallOut(与自定义的TableViewCell一样)
可通过代码选中Annotation:
selectAnnotation:animated:
或者取消选择:
deselectAnnotation:animated:
9、显示Annotation
通过mapView:viewForAnnotation: 方法显示Annotation,每在MapView中加入一个Annotation,就会调用此方法
示例(与tableView:cellForRowAtIndexPath: 很相似)
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
static NSString *placemarkIdentifier = @"my annotation identifier";
if ([annotation isKindOfClass:[MyAnnotation class]]) {
MKAnnotationView *annotationView = [theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
annotationView.image = [UIImage imageNamed:@"blood_orange.png"];
}
else
annotationView.annotation = annotation;
return annotationView;
}
return nil;
}
10、取得真实地址
示例:
初始化MKReverseGeocoder
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinates];
geocoder.delegate = self;
[geocoder start];
如果无法处理坐标,则调用reverseGeocoder:didFailWithError: 方法
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"Error resolving coordinates: %@", [error localizedDescription]);
geocoder.delegate = nil;
[geocoder autorelease];
}
如果成功,则调用reverseGeocoder:didFindPlacemark: 并把信息存储在MKPlacemark 中
didFindPlacemark:(MKPlacemark *)placemark {
NSString *streetAddress = placemark.thoroughfare;
NSString *city = placemark.locality;
NSString *state = placemark.administrativeArea;
NSString *zip = placemark.postalCode;
// Do something with information
geocoder.delegate = nil;
[geocoder autorelease];
}
最后奉献一枚APP源码,由论坛会员qdvictory分享
对于初步接触mapview编程的朋友比较有帮助。
主要知识点:
1.mapview的简单使用。
2.google api的相关调用。
3.mapview大头钉的操作,长按插入大头钉等。
App Store:http://itunes.apple.com/cn/app/telllocation/id467293790?l=en&mt=8
git链接:https://github.com/qdvictory/TellLocation
发表评论
-
Objective-C 与 C++ 的异同
2013-04-02 12:03 1358http://www.cnblogs.com/y041039 ... -
Cocos2D-X是全球知名的开源跨平台手机游戏引擎
2013-01-22 10:05 2710http://www.oschina.net/p/cocos ... -
iOS Keyboard 键盘高度变化 自适应
2013-01-15 15:43 3219[[NSNotificationCenter default ... -
iOS使用自定义字体
2012-11-27 12:11 12075From: http://blog.csdn.net/csy1 ... -
4 款类似 Facebook/Path 切换效果的 iOS 组件
2012-11-27 12:03 2090From: http://blog.csdn.net/lia ... -
Path 2.0的UI界面设计详细介绍
2012-11-27 11:56 1426如Path的创始人Dave Morin ... -
史上最全的App Store邮箱列表
2012-11-27 11:51 1219From: http://roybaby.blog.51cto ... -
iOS从info.plist 获取项目的名称及版本号
2012-11-16 10:54 1575From: http://blog.sina.com.cn/s ... -
MapKit annotation drag and drop with callout info update
2012-10-13 10:38 2362http://hollowout.blogspot ... -
NSArray 或NSDictionary 调用writeToFile方法失败原因
2012-08-31 10:03 4421NSArray 或NSDictionary 调用writeTo ... -
如何让IOS应用从容地崩溃
2012-08-30 15:25 1570From: http://www.cocoachina.com ... -
iOS中判断设备系统版本
2012-08-29 17:17 31668在iOS开发中,经常要考虑系统的向下兼容,如果使用 ... -
iOS 汉字转拼音
2012-08-21 16:42 1423From: http://www.cnblogs.com/v2 ... -
iOS模拟器截图工具
2012-08-17 16:35 1583From: http://magicalboy.com/ios ... -
XCode下的iOS单元测试
2012-08-10 17:47 1120From: http://mobile.51cto.com/ ... -
AFNetworking
2012-08-08 10:54 4620AFNetworking on github: https:/ ... -
Wrapping Conventions
2012-08-01 15:54 779Wrapping Conventions ... -
Core Animation如何使显式动画结束时的值直接作用Layer
2012-08-01 14:51 3746(1)使用隐式动画会直接改变layer的属性值,如: ima ... -
How To Debug Memory Leaks with XCode and Instruments Tutoria
2012-07-31 16:30 1018From: http://www.raywenderlich. ... -
Using Properties in Objective-C Tutorial
2012-07-31 16:27 878From: http://www.raywenderlich. ...
相关推荐
mapKit
IOS MapKit应用
mapkit地图定位,自己编辑当前的位置
Picture Map is presenting your iPhone camera roll content in a new and exciting way!
GeoJSONSerialization, 在 GeoJSON & MapKit形状之间编码&解码 GeoJSONSerialization根据基础类的API约定,GeoJSONSerialization 在 GeoJSON 和 MapKit之间进行编码和解码。用法解码#import <MapKit/MapKit.h&
MapKitShowcase MapKitShowcase是收集Mapkit使用情况的源代码。 -MapKitShowcaseはMapkitの利用方法を集めたソースコードです。
mapkit 上有时候会有很多annotation,如果全部照的原样显示出来未免有些杂乱,而且有的会重合。实例里根据zoom级别来自动调整显示的annotation,如果有重合的,用一个带有annotation计数的圆点图片来代替,这样简洁...
MapKit地图的使用技巧。
ios纯代码实现.用CLLocation与mapkit实现定位与方位搜索的功能。若有不足之处欢迎大家评论指出~
学习demo 博文链接:https://justsee.iteye.com/blog/1770094
地图,大头针,定位编码反编码,苹果地图导航;
Apple Mapkit JS演示应用程序 这是一个节点js应用程序,使用Mapkit JS在浏览器中演示了Apple的Map。 要求 您需要Node js版本6或更高版本 通过此创建和下载Apple Maps的私钥 入门 cp .env-sample .env 在.env文件中...
利用MapKit框架以及CLLocationManagerDelegate,MKMapViewDelegate协议实现地图定位
NULL 博文链接:https://re-reference.iteye.com/blog/1113539
iOS 9 MapKit 地图 导航新增功能
IOS应用源码之Apple的MapKit相关官方Sample三个送上CurrentAddress.zip
IOS应用源码之Apple的MapKit相关官方Sample三个送上WeatherMap.zip
iOS5地图使用MapKit,使用CLGeocoder解析,原来的MKReverseGeocoder过时不可用
用更简单的方式去使用地图(支持 MapKit、高德地图、百度地图)
IOS应用源码之Apple的MapKit相关官方Sample三个送上WorldCities.zip