版权声明:本文为博主原创文章,未经博主允许不得转载。
需要准备工作按照下图引进类库
需要添加
添加的两个字符串为:nslocationwheninuseusagedescription / nslocationalwaysusagedescription
默认定位设置:
设置工作准备完毕上代码:
指示根视图:
[[uinavigationbar appearance] setbartintcolor:[uicolor colorwithred:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
self.window.rootviewcontroller = [mapviewcontroller new];
mapviewcontroller.m//设置需要的属性
#import "mapviewcontroller.h"
#import <mapkit/mapkit.h>
#import "mypoint.h"
#import <corelocation/corelocation.h>
@interface mapviewcontroller ()<mkmapviewdelegate,cllocationmanagerdelegate>
@property (nonatomic, strong) mkmapview *mapview;
//经度
@property (nonatomic, strong) uitextfield *longitudetext;
//纬度
@property (nonatomic, strong) uitextfield *latitudetext;
@property (nonatomic, strong) uilabel *longitudelabel;
@property (nonatomic, strong) uilabel *latitudelabel;
//防止标注的button[
@property (nonatomic, strong) uibutton *button;
//地址输入
@property (nonatomic, strong) uitextfield *destination;
//输入地址查询地图
@property (nonatomic, retain) uibutton *searchbutton;
//可以获取设备当前的经纬度信息
@property (nonatomic, strong) cllocationmanager *locmanager;
@end
@implementation mapviewcontroller
调用:
- (void)viewdidload {
[super viewdidload];
self.view.backgroundcolor = [uicolor whitecolor];
self.locmanager = [[cllocationmanager alloc]init];
//代理
_locmanager.delegate = self;
//定位精度
_locmanager.desiredaccuracy = kcllocationaccuracybest;
//定位频率,10米定位一次
cllocationdistance distance = 10.0;
_locmanager.distancefilter = distance;
//更新位置
[_locmanager requestalwaysauthorization];
[self.locmanager startupdatinglocation];
//查询两个地点之间的距离
[self countdistance];
//调用布置视图
[self configureview];
[self setmapview];
}
//布置视图
- (void)configureview{
//经度
self.longitudelabel = [[uilabel alloc]initwithframe:cgrectmake(0, 20, 40, 30)];
self.longitudelabel.text = @"经度";
self.longitudetext = [[uitextfield alloc]initwithframe:cgrectmake(40, 20, 120, 30)];
self.longitudetext.borderstyle = uitextborderstyleroundedrect;
//纬度
self.latitudelabel = [[uilabel alloc]initwithframe:cgrectmake(0, 50, 40, 30)];
self.latitudelabel.text = @"纬度";
self.latitudetext = [[uitextfield alloc]initwithframe:cgrectmake(40, 50, 120, 30)];
self.latitudetext.borderstyle = uitextborderstyleroundedrect;
//放置标注按钮
self.button = [uibutton buttonwithtype:(uibuttontypesystem)];
self.button.frame = cgrectmake(30, 73, 100, 30);
[self.button settitle:@"放置标注" forstate:(uicontrolstatenormal)];
[self.button addtarget:self action:@selector(annotationaction:) forcontrolevents:(uicontroleventtouchupinside)];
//地址输入
self.destination = [[uitextfield alloc]initwithframe:cgrectmake(200, 26, 100, 30)];
self.destination.borderstyle = uitextborderstyleroundedrect;
//查询按钮
self.searchbutton = [uibutton buttonwithtype:(uibuttontypesystem)];
self.searchbutton.frame = cgrectmake(200, 46, 100, 30);
[self.searchbutton settitle:@"查询" forstate:(uicontrolstatenormal)];
[self.searchbutton addtarget:self action:@selector(detailsearchaction:) forcontrolevents:(uicontroleventtouchupinside)];
[self.view addsubview:self.button];
[self.view addsubview:self.latitudelabel];
[self.view addsubview:self.longitudelabel];
[self.view addsubview:self.longitudetext];
[self.view addsubview:self.latitudetext];
[self.view addsubview:self.searchbutton];
[self.view addsubview:self.destination];
- (void)countdistance{
cllocation *loc1 = [[cllocation alloc]initwithlatitude:34 longitude:113];
cllocation *loc2 = [[cllocation alloc]initwithlatitude:35 longitude:113];
cllocationdistance distance = [loc1 distancefromlocation:loc2];
nslog(@"(%@) 和 (%@)的距离为: %f", loc1, loc2, distance);
#pragma mark - cllocationmanagerdelegate methods
// 此方法会被频繁调用
- (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations
{
// nslog(@"didupdatelocations---%lu", (unsigned long)locations.count);
// 用来表示某个位置的地理信息, 比如经纬度, 海拔等等
cllocation *location = locations.lastobject;
// location.coordinate.latitude 维度
// location.coordinate.longitude 经度
nslog(@"经度 == %f, 维度 == %f", location.coordinate.longitude, location.coordinate.latitude);
self.longitudetext.text = [nsstring stringwithformat:@"%f",location.coordinate.longitude];
self.latitudetext.text = [nsstring stringwithformat:@"%f",location.coordinate.latitude];
// 停止更新位置(不用定位服务时马上停止, 因为非常耗电)
// [manager stopupdatinglocation];
//调出地图
- (void)setmapview{
//创建地图视图,初始化参数
//mkmaptypestandard 显示街道和道路
//mkmaptypesatellite 显示卫星
//mkmaptypehybrid 显示混合地图
self.mapview = [[mkmapview alloc]initwithframe:cgrectmake(0, 100, 320, 460)];
[self.mapview setmaptype:mkmaptypestandard];
//显示用户当前的坐标,打开地图有相应的提示
self.mapview.showsuserlocation = yes;
//设置地图代理
self.mapview.delegate = self;
[self.view addsubview:self.mapview];
#pragma mark 根据输入的经纬度确定位置
//放置标注
//放置标注
- (void)annotationaction:(uibutton *)sender{
//创建cllocation 设置经纬度
cllocationcoordinate2d location = cllocationcoordinate2dmake([[self.latitudetext text]floatvalue], [[self.longitudetext text]floatvalue] );
//创建标题
nsstring *title = [nsstring stringwithformat:@"%f,%f",location.latitude,location.longitude];
mypoint *mypoint = [[mypoint alloc]initwithcoordinate:location andtitle:title];
//添加标注
[self.mapview addannotation:mypoint];
//放大到标注的位置
mkcoordinateregion region = mkcoordinateregionmakewithdistance(location, 0.01, 0.01);
[self.mapview setregion:region];
[self showlocation];
//根据输入的经纬度显示位置
//根据输入的经纬度显示位置
- (void)showlocation{
[self.mapview setregion:region animated:yes];
#pragma mark 根据输入的地址搜寻位置
//根据地址输入搜索地图
//根据地址输入搜索地图
- (void)detailsearchaction:(uibutton *)search{
if (_destination.text == nil || [_destination.text length] == 0) {
return;
}
clgeocoder *geocode = [[clgeocoder alloc]init];
[geocode geocodeaddressstring:_destination.text completionhandler:^(nsarray *placemarks, nserror *error) {
if (error || placemarks.count == 0) {
nslog(@"地址不存在");
}else{
for (clplacemark *placemark in placemarks) {
nslog(@"name=%@ locality=%@ country=%@ postalcode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalcode);
}
clplacemark *firstplacemark = [placemarks firstobject];
cllocationdegrees latitude = firstplacemark.location.coordinate.latitude;
cllocationdegrees longitude = firstplacemark.location.coordinate.longitude;
//显示经纬度
self.latitudetext.text = [nsstring stringwithformat:@"%.2f",latitude];
self.longitudetext.text = [nsstring stringwithformat:@"%.2f",longitude];
[self showlocation];
[self searchdetaillocationaction];
}
}];
//根据地址搜寻位置
//根据地址搜寻位置
- (void)searchdetaillocationaction{
cllocationcoordinate2d location = cllocationcoordinate2dmake([self.latitudetext.text floatvalue], [self.longitudetext.text floatvalue]);
nsstring *title = [nsstring stringwithformat:@"%f,%f",[self.latitudetext.text floatvalue], [self.longitudetext.text floatvalue]];
建一个类:
//.h
#import <foundation/foundation.h>
@interface mypoint : nsobject<mkannotation>
//实现mkannotion协议必须要定义这个属性
@property (nonatomic,readonly)cllocationcoordinate2d coordinate;
//标题
@property (nonatomic,copy)nsstring *title;
//初始化方法
- (id)initwithcoordinate:(cllocationcoordinate2d)c andtitle:(nsstring *)t;
//.m
@implementation mypoint
- (id)initwithcoordinate:(cllocationcoordinate2d)c andtitle:(nsstring *)t{
if (self = [super init]) {
_coordinate = c;
_title = t;
return self;
最终效果:
原文地址:http://blog.csdn.net/qq_31810357/article/details/49847813