版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
需要準備工作按照下圖引進類庫

需要添加
添加的兩個字元串為: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