IOS 请问如何让高德地图定位当前位置移动到当前地位的位置

iOS地图定位使用几个小功能
iOS系统自带的定位服务可以实现很多需求。比如:获取当前经纬度,获取当前位置信息等等。
获取当前经纬度
首先导入#import ,定义CLLocationManager的实例,实现CLLocationManagerDelegate。
@interface ViewController ()
CLLocationManager *_locationM
开始定位的方法:
- (void)startLocating
if([CLLocationManager locationServicesEnabled])
_locationManager = [[CLLocationManager alloc] init];
//设置定位的精度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
_locationManager.distanceFilter = 100.0f;
_locationManager.delegate =
if ([[[UIDevice currentDevice] systemVersion] floatValue] & 8.0)
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
//开始实时定位
[_locationManager startUpdatingLocation];
实现代理方法:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
NSLog(@&Longitude = %f&, manager.location.coordinate.longitude);
NSLog(@&Latitude = %f&, manager.location.coordinate.latitude);
[_locationManager stopUpdatingLocation];
获取当前位置信息
在上面的代理方法中
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
NSLog(@&Longitude = %f&, manager.location.coordinate.longitude);
NSLog(@&Latitude = %f&, manager.location.coordinate.latitude);
[_locationManager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSDictionary *test = [placemark addressDictionary];
Country(国家)
State(城市)
SubLocality(区)
NSLog(@&%@&, [test objectForKey:@&Country&]);
NSLog(@&%@&, [test objectForKey:@&State&]);
NSLog(@&%@&, [test objectForKey:@&SubLocality&]);
NSLog(@&%@&, [test objectForKey:@&Street&]);
这样就很简单获取了当前位置的详细信息。
获取某一个地点的经纬度
- (void)getLongitudeAndLatitudeWithCity:(NSString *)city
//city可以为中文
NSString *oreillyAddress =
CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
[myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] & 0 && error == nil)
NSLog(@&Found %lu placemark(s).&, (unsigned long)[placemarks count]);
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
NSLog(@&Longitude = %f&, firstPlacemark.location.coordinate.longitude);
NSLog(@&Latitude = %f&, firstPlacemark.location.coordinate.latitude);
else if ([placemarks count] == 0 && error == nil)
NSLog(@&Found no placemarks.&);
else if (error != nil)
NSLog(@&An error occurred = %@&, error);
计算两个地点之间的距离
- (double)distanceByLongitude:(double)longitude1 latitude:(double)latitude1 longitude:(double)longitude2 latitude:(double)latitude2{
CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:latitude2 longitude:longitude2];
double distance
= [curLocation distanceFromLocation:otherLocation];//单位是m
首先我们可以用上面的getLongitudeAndLatitudeWithCity方法获取某一个地点的经纬度。比如我们获取北京和上海的经纬度分别为:北京Longitude = 116.405285,Latitude = 39.904989 上海Longitude = 121.472644, Latitude = 31.231706, 那么北京和上海之间的距离就是:
double distance = [self distanceByLongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706];
NSLog(@&Latitude = %f&, distance);
计算的是大概的距离,可能没有那么精准。输入结果为:
distance = 194
代码下载地址: /worldligang/MapDistanceMaster.git
更多iOS技术请关注微信iOS开发
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'当前位置 & &
& 你都知道吗?iOS谷歌地图10个小技巧
你都知道吗?iOS谷歌地图10个小技巧
11:42:36&&作者:ZNXF
编辑:ZNXF &&)
让小伙伴们也看看:
阅读更多:
好文共享:
文章观点支持
当前平均分:0(0 次打分)
[04-04][04-04][04-04][04-04][04-03][04-03][04-03][04-02][04-02][04-02]
登录驱动之家
没有帐号?
用合作网站帐户直接登录iOS版百度地图调起说明
iOS版百度地图调起说明
1 适用环境
移动设备浏览器端应用和移动App应用均可调起iOS版百度地图。
2 协议形式
无论是基于移动浏览器端的应用开发还是基于iOS SDK的应用开发均可采用如下协议调起iOS版百度地图。该协议要正确执行除了要参数填写正确外,还必须要安装iOS百度地图。
protocol://product/[service/]action[?parameters]
//parameters功能参数定义,具体规范见功能协议说明
baidumap://map/direction?origin=中关村&destination=五道口&mode=driving&region=北京
//本示例是通过该URL启动地图app并进入北京市从中关村到五道口的驾车导航路线图
组成说明:
参数名称参数说明是否必选备注
protocol产品线协议头,例如baidumap表示百度地图必选&
product产品线名称,例如如百度地图客户端(map)必选product下可直接跟方法,当然产品线也可增加一个service级别
service具体功能服务名称,如POI检索服务(place)必选&
action方法名称,如POI检索中的详情检索(detail)必选&
parameters方法参数,参考各个功能协议说明的定义必选&
3 功能使用说明
调用该接口可调起IOS百度地图,且在指定坐标点上显示点的名称和内容信息。
baidumap://map/marker
//iOS服务地址
参数名称参数说明是否必选备注
locationlat,lng必选&
title标注点显示标题必选product下可直接跟方法,当然产品线也可增加一个service级别
content标注点显示内容必选&
coord_type坐标类型,可选参数,默认为bd09ll。可选 允许的值为bd09ll、gcj02、wgs84。bd09ll表示百度经纬度坐标, gcj02表示经过国测局坐标,wgs84表示gps获取的坐标。
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选此参数不传值,不保证服务。
//网页应用调起iOS百度地图方式举例
&a href="baidumap://map/marker?location=40..313082&title=我的位置&content=百度奎科大厦&src=yourCompanyName|yourAppName"&地图标点&/a&
调用效果如下:
3.2 地址解析
调用该接口可以在调起百度地图时,当前页面显示地址对应的坐标点。
baidumap://map/geocoder
// iOS服务地址
参数名称参数说明是否必选备注
address地址名称必选&
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选此参数不传值,不保证服务。
//网页应用调起iOS百度地图方式举例
&a href="baidumap://map/geocoder?address=北京市海淀区上地信息路9号奎科科技大厦&src=yourCompanyName|yourAppName"&地理编码&/a&
调起效果如左下图,点击地图进入右图:
3.3 反向地址解析(地址查询)
调用该接口可调起iOS百度地图,经过逆地理编码后,以标注形式显示位置和地址信息。
baidumap://map/geocoder
// iOS服务地址
参数名称参数说明是否必选备注
locationlat,lng必选&
coord_type坐标类型,可选参数,默认为bd09经纬度坐标。可选 允许的值为bd09ll、gcj02、wgs84。bd09ll表示百度经纬度坐标, gcj02表示经过国测局加密的坐标,wgs84表示gps获取的坐标。
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选此参数不传值,不保证服务。
//网页应用调起iOS百度地图方式举例
&a href="baidumap://map/geocoder?location=39.714,116.07&coord_type=gcj02&src=yourCompanyName|yourAppName"&逆地理编码&/a&
调起效果如左下图,单击地图进入右图:
3.4 POI(地点)搜索
baidumap://map/place/search
// iOS服务地址
参数名称参数说明是否必选备注
query关键词必选&
region城市名或县名选择方式:地点搜索限定范围可以由region、bounds和location + radius方式进行,其中bounds优先级最高、region优先级最低(与web服务保持一致)。&
location中心点经纬度,或中心点经纬度和名称描述,注意,名称不参与检索,只负责显示。同上1、39.9761,116.32822、39.9761,116.3282| 中关村&名称&
radius检索半径同上&
bounds视野范围同上lat,lng,lat,lng
coord_type坐标类型,可选参数,默认为bd09经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标,wgs84表示gps获取的坐标。可选&
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选此参数不传值,不保证服务。
&a href="baidumap://map/place/search?query=%E9%A4%90%E9%A6%86&location=31.,121.41117785465&radius=1000&region=上海&src=yourCompanyName|yourAppName"&周边检索&/a&
调起效果左下图,单击地图进入右图。
3.5 POI详情页展示
baidumap://map/place/detail
// iOS服务地址
参数名称参数说明是否必选备注
uidPOI的ID必选&
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选此参数不传值,不保证服务。
//网页应用调起iOS百度地图方式举例
&a href="baidumap://map/place/detail?uid=d3099bdd81c525dbd1f49ee6&src=yourCompanyName|yourAppName"&详情检索&/a&
调起iOS百度地图进行查询效果如下:
3.6 公交、地铁线路查询
baidumap://map/line
// iOS服务地址
参数名称参数说明是否必选备注
region城市名或县名必选&
name线路名称必选&
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选此参数不传值,不保证服务。
//网页应用调起iOS百度地图方式举例
&a href="baidumap://map/line?region=北京&name=518&src=yourCompanyName|yourAppName"&线路检索&/a&
调起效果如左下图,单击地图后进入右下图
3.7 公交、驾车、步行导航
调起iOS百度地图,展示指定导航模式下从起点到终点的路线规划。
baidumap://map/direction
// iOS服务地址
参数名称参数说明是否必选备注
origin起点名称或经纬度,或者可同时提供名称和经纬度,此时经纬度优先级高,将作为导航依据,名称只负责展示。必选1、名称:天安门2、经纬度:39.98871,116.43234。3、名称和经纬度:name:天安门|latlng:39.234
destination终点名称或经纬度,或者可同时提供名称和经纬度,此时经纬度优先级高,将作为导航依据,名称只负责展示。必选同上
mode导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行必选&
region城市名或县名当给定region时,认为起点和终点都在同一城市,除非单独给定起点或终点的城市。&
origin_region起点所在城市或县同上&
destination_region终点所在城市或县同上&
coord_type坐标类型,可选参数,默认为bd09ll。可选 允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标,wgs84表示gps获取的坐标。
zoom展现地图的级别,默认为视觉最优级别。可选&
src调用来源,规则:companyName|appName。必选&
//网页应用调起iOS百度地图方式举例
&a href="baidumap://map/direction?origin=34.,108.&destination=40..360582&mode=driving&src=yourCompanyName|yourAppName"&驾车导航&/a&
调起效果如下:iOS学习之Map,定位,标记位置的使用
iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可。这次要实现的效果如下:
有标注(大头针),定位,地图。
1、添加地图
1.1 新一个Single View app ,选择默认项,创建后,在ViewController.h
@interface ViewController :
UIViewController
MKMapView *
CLLocationManager *locationM
@interface ViewController : UIViewController
MKMapView *
CLLocationManager *locationM
1.2在ViewController.m中添加
- (void)viewDidLoad
map = [[MKMapView alloc]
initWithFrame:[self.view bounds]];
map.showsUserLocation = YES;
map.mapType = MKMapTypeS
[self.view addSubview:map];
[super viewDidLoad];
additional setup after loading the view, typically from a
- (void)viewDidLoad
map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
map.showsUserLocation = YES;
map.mapType = MKMapTypeS
[self.view addSubview:map];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
OMG,看到的是世界地图。怎么定位到指定的位置呢?比如定位回来伟大的祖国首都?
这里map.mapType =MKMapTypeSatellite;我用到是卫星地图,可以使用标准的地图,
map.mapType =MKMapTypeStandard;
注意,如果此时你编译有错误,请拉到博客最后查看 :5、
遇到的问题
2、定位到指定经纬度
CLLocationCoordinate2D coords =
CLLocationCoordinate2DMake(39..397105);
float zoomLevel =
MKCoordinateRegion region =
MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel,
zoomLevel));
[map setRegion:[map regionThatFits:region]
animated:YES];
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39..397105);
float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[map setRegion:[map regionThatFits:region] animated:YES];
这样,就我们就定位的了故宫了。
3、添加标注大头针
3.1 新建一个标注类:CustomAnnotation
按Command+N,继承NSObject。在CustomAnnotation.h
和CustomAnnotation.m文件添加如下代码:
@interface CustomAnnotation : NSObject
CLLocationCoordinate2D
NSString *
NSString *
initWithCoordinate:(CLLocationCoordinate2D)
@property (nonatomic, readonly)
CLLocationCoordinate2D
@property (nonatomic, retain) NSString
@property (nonatomic, retain) NSString
@interface CustomAnnotation : NSObject
CLLocationCoordinate2D
NSString *
NSString *
-(id) initWithCoordinate:(CLLocationCoordinate2D)
@property (nonatomic, readonly) CLLocationCoordinate2D
@property (nonatomic, retain) NSString *
@property (nonatomic, retain) NSString *
#import "CustomAnnotation.h"
@implementation CustomAnnotation
@synthesize coordinate, title,
-(id) initWithCoordinate:(CLLocationCoordinate2D)
if (self = [super init])
coordinate =
#import "CustomAnnotation.h"
@implementation CustomAnnotation
@synthesize coordinate, title,
-(id) initWithCoordinate:(CLLocationCoordinate2D) coords
if (self = [super init]) {
coordinate =
3.1 使用大头针,
新建个方法添加大头针的
-(void)createAnnotationWithCoords:(CLLocationCoordinate2D)
CustomAnnotation *annotation = [[CustomAnnotation alloc]
initWithCoordinate:
annotation.title = @"标题";
annotation.subtitle = @"子标题";
[map addAnnotation:annotation];
-(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:
annotation.title = @"标题";
annotation.subtitle = @"子标题";
[map addAnnotation:annotation];
CLLocationCoordinate2D coords =
CLLocationCoordinate2DMake(39..397105);
float zoomLevel =
MKCoordinateRegion region =
MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel,
zoomLevel));
[map setRegion:[map regionThatFits:region]
animated:YES];
[self createAnnotationWithCoords:coords];
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39..397105);
float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
[map setRegion:[map regionThatFits:region] animated:YES];
[self createAnnotationWithCoords:coords];
这样我们就把大头针定位在故宫了
4、定位到当前位置并获取当前经纬度
前面我们已经添加了locationManager,现在在DidViewLoad里直接调用
locationManager = [[CLLocationManager
alloc] init];
locationManager.delegate =
[locationManager
startUpdatingLocation];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate =
[locationManager startUpdatingLocation];
实现协议方法收到定位成功后的经纬度
- (void)locationManager:(CLLocationManager
*)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];
NSString *strLat = [NSString
stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString
stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat:
%@ Lng: %@", strLat,
- (void)locationManager:(CLLocationManager
*)manager didFailWithError:(NSError *)error {
NSLog(@"locError:%@",
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];
NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat: %@
Lng: %@", strLat, strLng);
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"locError:%@", error);
运行,允许获取当前位置,打印log
23:58:32.237
MapDemo[] Lat: 39.9011 Lng: 116.3000
23:58:32.237 MapDemo[] Lat: 39.9011
Lng: 116.3000
如果不允许:打印出错误日志
23:25:03.109
MapDemo[] locError:Error Domain=kCLErrorDomain
Code=1 "The
operation couldn’t be completed. (kCLErrorDomain error
23:25:03.109 MapDemo[] locError:Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"
定位后,移动到当前位置:
- (void)locationManager:(CLLocationManager
*)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];
NSString *strLat = [NSString
stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString
stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat:
%@ Lng: %@", strLat,
CLLocationCoordinate2D coords =
CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
float zoomLevel =
MKCoordinateRegion region =
MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel,
zoomLevel));
[map setRegion:[map regionThatFits:region]
animated:YES];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];
NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat: %@
Lng: %@", strLat, strLng);
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
float zoomLevel = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));
[map setRegion:[map regionThatFits:region] animated:YES];
定位到了当前位置。
5、会遇到的问题:
运行是发现了编译错误:
Undefined symbols for architecture i386:
"_CLLocationCoordinate2DMake", referenced from:
-[ViewController viewDidLoad] in ViewController.o
-[ViewController
locationManager:didUpdateToLocation:fromLocation:] in
ViewController.o
"_OBJC_CLASS_$_MKMapView", referenced from:
objc-class-ref in ViewController.o
"_OBJC_CLASS_$_CLLocationManager", referenced
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1
(use -v to see invocation)
这是为什么呢?没有添加对应的FrameWork。我使用的是4.3.2版本的XCode,添加方法如下:
选择项目,TARGETS ,点加号,添加两个framework
如何发送IOS模拟器经纬度?
5.0以上的模拟器才能用这个功能,打开模拟:
这样就能发送模拟的当前位置了。
例子代码:
/schelling/YcDemo/tree/master/MapDemo
著作权声明:本文由原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 百度地图 当前位置 的文章

 

随机推荐