ios地图定位 分类: ios开发 2014-12-24 20:51 142人阅读 评论(0) 收藏
这两天抽时间学习了一下IOS下谷歌地图的API 现在很多APP中都会使用谷歌的地图。 个人觉得开发起来还是非常的便利的。废话不多说啦,赶快进入今天的正题。如下所示 这是MOMO的手机,这个项目我是在iPhone上调试的,这正是我的手机,模拟器上我没有试过,模拟器肯定是能打开谷歌地图的,但是好像不能定位地点。大家仔细看我下面的代码描述,其实很简单 真的很简单。本来今天晚上不像写这篇博文的,只是今天的北京雨下的太大了,困住了我回家的路,既然困在了公司那么当然要学习一下啦哈哈哈哈哈哈哈哈哈~~~
OK下面是代码片段。
创建一个工程,如下图所示,先将CoreLocation.framework 和 MapKit.framework 引入工程中,前者是负责定位的,后者是负责地图的。
AppDelegate.h 入口类,没什么好说的我就不解释了。
AppDelegate.m
主要的东东都写在MapViewController中,请大家仔细看这里。
MapViewController.h
MapViewController.m 注意看这个类噢。
最后是本文的源码下载:http://vdisk.weibo.com/s/acdN7
雨松MOMO祝大家学习愉快、工作愉快、生活愉快、互相学习与进步,加油~ 话说北京这会应该不下雨了吧??雨停了回家睡觉。 嚯嚯!
———————————-华丽的分割线——————————–
以上方法我在IOS6中使用发现了一点小问题,IOS6使用CLLocationManager定位的时候发现有时候定位到的经纬度是0.0000 所以地图界面中就是一个白屏。那么我将解决的办法贴出来。
用这个方法来接受当前地图经纬度信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
-
(void)mapView:(MKMapView
*)mapView
didUpdateUserLocation:(MKUserLocation
*)userLocation
{
//更新地址,
[_locManager
stopUpdatingLocation];
//设置定位后的自定义图标。
MKCircle*
circle
=
[MKCircle
circleWithCenterCoordinate:CLLocationCoordinate2DMake(myMapView.userLocation.location.coordinate.latitude,
myMapView.userLocation.location.coordinate.longitude)
radius:5000];
[myMapView
addAnnotation:circle];
NSLog(@"%f",myMapView.userLocation.location.coordinate.latitude);
//我们需要通过当前用户的经纬度换成出它现在在地图中的地名
CLGeocoder
*geocoder
=
[[[CLGeocoder
alloc]
init]
autorelease];
[geocoder
reverseGeocodeLocation:
_locManager.location
completionHandler:
^(NSArray
*placemarks,
NSError *error)
{
//得到自己当前最近的地名
CLPlacemark *placemark
=
[placemarks
objectAtIndex:0];
NSString *locatedAt
=
[[placemark.addressDictionary
valueForKey:@"FormattedAddressLines"]
componentsJoinedByString:@",
"];
//locatedAt就是当前我所在的街道名称
//上图中的中国北京市朝阳区慧中北路
[myMapView.userLocation
setTitle:locatedAt];
[myMapView.userLocation
setSubtitle:@"你在这里噢"];
nowLatitude
=
myMapView.userLocation.location.coordinate.latitude;
nowLongitude
=
myMapView.userLocation.location.coordinate.longitude;
//这里是设置地图的缩放,如果不设置缩放地图就非常的尴尬,
//只能光秃秃的显示中国的大地图,但是我们需要更加精确到当前所在的街道,
//那么就需要设置地图的缩放。
MKCoordinateRegion
theRegion
=
{
{0.0,
0.0
},
{
0.0,
0.0
}
};
theRegion.center=
myMapView.userLocation.location.coordinate;
//theRegion.center.latitude = targetLatitude;
//theRegion.center.longitude = argetLongitude;
//缩放的精度。数值越小约精准
theRegion.span.longitudeDelta
=
0.01f;
theRegion.span.latitudeDelta
=
0.01f;
//让MapView显示缩放后的地图。
[myMapView
setRegion:theRegion
animated:YES];
//最后让MapView整体显示, 因为截至到这里,我们已经拿到用户的经纬度,
//并且已经换算出用户当前所在街道的名称。
//myMapView.hidden = false;
}];
}
|