zoukankan      html  css  js  c++  java
  • iOS CLGeocoder反地理编码获取地理位置

           要得到当前的位置,只需要2步就能完成

          1:判断设备是否支持定位功能,然后创建MKMapView

    if ([CLLocationManager locationServicesEnabled]) {
            myMapView =[[MKMapView alloc] init];
            myMapView.delegate=self;
            myMapView.showsUserLocation=YES;
        }

           2:实现MKMapViewDelegate协议

    -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
        //得到经纬度
        CLLocation *newLocation=userLocation.location;
    
        NSLog(@"locaiton---%f---%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        
        //反地理编码
        CLGeocoder *clGeoCoder=[[CLGeocoder alloc] init];
        CLGeocodeCompletionHandler handle=^(NSArray *placemarks,NSError *error){
            for (CLPlacemark *placeMark in placemarks) {
                
                //获取地理位置名称
                NSDictionary *addressDic=placeMark.addressDictionary;
                NSString *state=[addressDic objectForKey:@"State"];
                NSString *city=[addressDic objectForKey:@"City"];
                NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
                NSString *street=[addressDic objectForKey:@"Street"];
                
                NSLog(@"%@%@%@",state,subLocality,street);
            }
        };
    //执行
        [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
    }

    那么定位当前位置就完成,代码量非常的少,也非常的简单

  • 相关阅读:
    发一个多维数组转xml格式的php函数方法
    php 返回json和jsonp格式
    phpstudy修改端口及网站根目录和访问 localhost 显示目录文件夹
    web后端开发语言Java和PHP的比较
    VSCode 的PHP常用插件及安装
    Nginx服务器的Websockets配置方法
    WebSocket-demo
    前端资源
    HTTPS 升级指南
    HTTP 协议入门
  • 原文地址:https://www.cnblogs.com/boyuanmeng/p/3985729.html
Copyright © 2011-2022 走看看