iOS_城市定位

技术点-利用CoreLoaction框架,实现城市定位功能。



原理:

利用苹果官方提供的CoreLocation框架实现城市定位的功能;

该框架主要包括两个比较有用的类:定位类CLLocationManager、地理编码类ClGeoCoder。



代码:

 1 //  Copyright © 2016年 刘勇虎. All rights reserved.
 2 //
 3
 4 #import "ViewController.h"
 5 #import <CoreLocation/CoreLocation.h>
 6
 7 @interface ViewController ()<CLLocationManagerDelegate>
 8 @property(strong,nonatomic)CLLocationManager *locationMananger;
 9 @property(strong,nonatomic)CLLocation *startPoint;
10 @property(strong,nonatomic)CLGeocoder *geoCode;
11 @property (weak, nonatomic) IBOutlet UILabel *cityName;
12
13 @end
14
15 @implementation ViewController
16 //    开始定位
17 - (IBAction)startLocationing:(UIButton *)sender {
18         [_locationMananger startUpdatingLocation];
19 }
20
21 - (void)viewDidLoad {
22     [super viewDidLoad];
23     // Do any additional setup after loading the view, typically from a nib.
24
25
26     _geoCode = [[CLGeocoder alloc]init];
27     self.locationMananger = [[CLLocationManager alloc]init];
28     _locationMananger.delegate = self;
29     _locationMananger.desiredAccuracy = kCLLocationAccuracyBest;
30     //    向系统申请权限
31     [_locationMananger requestWhenInUseAuthorization];
32
33
34 }
35
36 #pragma mark -- location delegate --
37 //更新定位
38 - (void)locationManager:(CLLocationManager *)manager
39     didUpdateToLocation:(CLLocation *)newLocation
40            fromLocation:(CLLocation *)oldLocation {
41
42 //    反地理编码
43 [_geoCode reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
44     if (placemarks && placemarks.count > 0) {
45
46         CLPlacemark *newPlaceMark = [placemarks firstObject];
47         _cityName.text = newPlaceMark.locality;
48         [_locationMananger stopUpdatingLocation];
49     }
50 }];
51 }
52 //定位失败后回调
53 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
54     NSLog(@"error == %@",error);
55 }

修改info.Plist文件配置:

时间: 2024-08-04 21:06:26

iOS_城市定位的相关文章

php 通过 ip地址 进行城市定位

ip城市定位新浪接口: /** * 新浪ip 地址获取城市信息 */ //根据ip 地址获取所在城市信息 function getIPLoc_sina($queryIP){ $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryIP; $ch = curl_init($url); curl_setopt($ch,CURLOPT_ENCODING ,'utf8'); curl_setopt

北京城市定位高精尖 土地资源将优先解决交通等

https://code.google.com/p/yanyuyi101/issues/detail?id=391 https://code.google.com/p/yanyuyi101/issues/detail?id=392 https://code.google.com/p/yanyuyi101/issues/detail?id=393 https://code.google.com/p/yanyuyi101/issues/detail?id=394 https://code.googl

android 开源项目(城市定位)

import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.lfg.Utils.MyVolley; import org.lfg.Utils.StringRequestUTF; import org.lfg.cityselect_activity.CityHomeActivity; import org.lf

android使用自定控件实现城市列表展示并且实现当前城市定位

自定义控件,点击控件,展示城市列表,使用百度地图定位当前城市,并且展示当前城市 代码类 package com.example.test0504; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import androi

城市定位

jsp如何取得城市名,一般可根据IP地址快速定位,直接上代码,代码区: css随意 js部分: 需要导入的js文件 效果图

百度定位+精确定位+模糊城市定位

百度定位相信大家都会使用,作为一个一年经验的安卓经验的新人,我也就不多说什么了.今天要给大家带来的是一个模糊定位,一个小小的需求,就是根据自己的定位地点的经纬度,解析旁边城市的经纬度,得到一个距离最近的城市.详细的和大家描述一下. 比如点 深圳(x1,y1)–>(地点名称)(纬度,经度) 北京(x2,y2)–>(地点名称)(纬度,经度) 如果我现在的定位地点是广州(x3,y3). 那么广州和深圳的距离是[(x3-x1)(x3-x1)]开方,同理广州和北京的距离是[(x3-x2)(x3-x2)]

[android] 百度地图开发 (两).所在地的城市定位和城市POI搜索

一个. 百度地图城市位置和POI搜索知识       上一篇文章"百度地图开发(一)"中讲述了怎样申请百度APIKey及解决显示空白网格的问题.该篇文章主要讲述怎样定位城市位置.定位自己的位置和进行城市兴趣点POI(Point of Interest)搜索.那么怎样在百度地图上定位某一个位置呢?       通过类GeoPoint能够定义经纬度,它存放着纬度值和经度值,通过getLastKnownLocation()方法能够获取Location对象,再定位经纬度设置其为地图中心就可以显

高性能的城市定位API接口

如果不需要精准的定位,还有一种通过IP地址获取当前城市的方法,采用新浪的api接口. <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script> <script> var city = remote_ip_info['city']; alert(city) </script> 原文地址:https://www.cnblogs.co

微信小程序(同城小程序)_总结一(实现城市定位)

一.前言 城市切换 城市搜索 二.基本内容 1.豆瓣接口文档 https://douban-api-docs.zce.me/ 2.城市切换用到的接口 获取城市列表 https://douban.uieee.com/v2/loc/list     3.获取和处理城市    3.1.index页面: (1)一开始加载程序在index页面中调用全局的getUserInfo() 和getLocation()方法获取到用户当前的信息 // pages/location/index/index.js var